spec/unit/models/presence_message_spec.rb in ably-0.1.6 vs spec/unit/models/presence_message_spec.rb in ably-0.2.0
- old
+ new
@@ -6,11 +6,11 @@
subject { Ably::Models::PresenceMessage }
let(:protocol_message_timestamp) { as_since_epoch(Time.now) }
let(:protocol_message) { Ably::Models::ProtocolMessage.new(action: 1, timestamp: protocol_message_timestamp) }
- it_behaves_like 'a model', with_simple_attributes: %w(client_id member_id client_data) do
+ it_behaves_like 'a model', with_simple_attributes: %w(client_id member_id data encoding) do
let(:model_args) { [protocol_message] }
end
context '#timestamp' do
let(:model) { subject.new({}, protocol_message) }
@@ -60,10 +60,23 @@
it 'raises an exception' do
expect { model.to_json }.to raise_error KeyError, /cannot generate a valid Hash/
end
end
+
+ context 'with binary data' do
+ let(:data) { MessagePack.pack(SecureRandom.hex(32)) }
+ let(:model) { subject.new({ action: 'enter', data: data }, protocol_message) }
+
+ it 'encodes as Base64 so that it can be converted to UTF-8 automatically by JSON#dump' do
+ expect(json_object["data"]).to eql(::Base64.encode64(data))
+ end
+
+ it 'adds Base64 encoding' do
+ expect(json_object["encoding"]).to eql('base64')
+ end
+ end
end
context 'from REST request with embedded fields' do
let(:id) { SecureRandom.hex }
let(:message_time) { Time.now + 60 }
@@ -98,18 +111,18 @@
let(:presence_0_payload) { SecureRandom.hex(8) }
let(:presence_0_json) do
{
client_id: 'zero',
- client_data: presence_0_payload
+ data: presence_0_payload
}
end
let(:presence_1_payload) { SecureRandom.hex(8) }
let(:presence_1_json) do
{
client_id: 'one',
- client_data: presence_1_payload
+ data: presence_1_payload
}
end
let(:protocol_message_id) { SecureRandom.hex }
let(:protocol_message) do
@@ -131,11 +144,11 @@
expect(presence_0.id).to eql("#{protocol_message_id}:0")
expect(presence_1.id).to eql("#{protocol_message_id}:1")
end
it 'should not modify the data payload' do
- expect(presence_0.client_data).to eql(presence_0_payload)
- expect(presence_1.client_data).to eql(presence_1_payload)
+ expect(presence_0.data).to eql(presence_0_payload)
+ expect(presence_1.data).to eql(presence_1_payload)
end
end
context 'PresenceMessage conversion method' do
let(:json) { { client_id: 'test' } }