spec/acceptance/realtime/message_spec.rb in ably-0.8.12 vs spec/acceptance/realtime/message_spec.rb in ably-0.8.13

- old
+ new

@@ -481,15 +481,17 @@ [MessagePack.pack({ 'key' => SecureRandom.hex }), 'ã unicode', { 'key' => SecureRandom.hex }].each do |payload| payload_description = "#{payload.class}#{" #{payload.encoding}" if payload.kind_of?(String)}" it "delivers a #{payload_description} payload to the receiver" do - encrypted_channel_client1.publish 'example', payload - encrypted_channel_client2.subscribe do |message| - expect(message.data).to eql(payload) - expect(message.encoding).to be_nil - stop_reactor + encrypted_channel_client2.attach do + encrypted_channel_client1.publish 'example', payload + encrypted_channel_client2.subscribe do |message| + expect(message.data).to eql(payload) + expect(message.encoding).to be_nil + stop_reactor + end end end end end @@ -518,15 +520,17 @@ let(:unencrypted_channel_client2) { other_client.channel(channel_name) } let(:payload) { MessagePack.pack({ 'key' => random_str }) } it 'delivers the message but still encrypted with a value in the #encoding attribute' do - encrypted_channel_client1.publish 'example', payload - unencrypted_channel_client2.subscribe do |message| - expect(message.data).to_not eql(payload) - expect(message.encoding).to match(/^cipher\+aes-256-cbc/) - stop_reactor + unencrypted_channel_client2.attach do + encrypted_channel_client1.publish 'example', payload + unencrypted_channel_client2.subscribe do |message| + expect(message.data).to_not eql(payload) + expect(message.encoding).to match(/^cipher\+aes-256-cbc/) + stop_reactor + end end end it 'emits a Cipher error on the channel' do unencrypted_channel_client2.attach do @@ -725,9 +729,127 @@ raise 'Message delivery should not happen' end deferrable.errback do stop_reactor end + end + end + end + end + end + + context 'message encoding interoperability' do + let(:client_options) { { key: api_key, environment: environment, protocol: :json } } + let(:channel_name) { "subscribe_send_text-#{random_str}" } + + fixtures_path = File.expand_path('../../../../lib/submodules/ably-common/test-resources/messages-encoding.json', __FILE__) + + context 'over a JSON transport' do + let(:realtime_client) do + auto_close Ably::Realtime::Client.new(client_options) + end + let(:rest_client) do + Ably::Rest::Client.new(client_options) + end + let(:realtime_channel) { realtime_client.channels.get(channel_name) } + + JSON.parse(File.read(fixtures_path))['messages'].each do |encoding_spec| + context "when decoding #{encoding_spec['expectedType']}" do + it 'ensures that client libraries have compatible encoding and decoding using common fixtures' do + realtime_channel.attach do + realtime_channel.subscribe do |message| + if encoding_spec['expectedHexValue'] + expect(message.data.unpack('H*').first).to eql(encoding_spec['expectedHexValue']) + else + expect(message.data).to eql(encoding_spec['expectedValue']) + end + stop_reactor + end + + raw_message = { "data" => encoding_spec['data'], "encoding" => encoding_spec['encoding'] } + rest_client.post("/channels/#{channel_name}/messages", JSON.dump(raw_message)) + end + end + end + + context "when encoding #{encoding_spec['expectedType']}" do + it 'ensures that client libraries have compatible encoding and decoding using common fixtures' do + data = if encoding_spec['expectedHexValue'] + encoding_spec['expectedHexValue'].scan(/../).map { |x| x.hex }.pack('c*') + else + encoding_spec['expectedValue'] + end + + realtime_channel.publish("event", data) do + response = rest_client.get("/channels/#{channel_name}/messages") + message = response.body[0] + expect(message['encoding']).to eql(encoding_spec['encoding']) + if message['encoding'] == 'json' + expect(JSON.parse(encoding_spec['data'])).to eql(JSON.parse(message['data'])) + else + expect(encoding_spec['data']).to eql(message['data']) + end + stop_reactor + end + end + end + end + end + + context 'over a MsgPack transport' do + JSON.parse(File.read(fixtures_path))['messages'].each do |encoding_spec| + context "when publishing a #{encoding_spec['expectedType']} using JSON protocol" do + let(:rest_publish_client) do + Ably::Rest::Client.new(client_options.merge(protocol: :json)) + end + let(:realtime_subscribe_client) do + Ably::Realtime::Client.new(client_options.merge(protocol: :msgpack)) + end + let(:realtime_subscribe_channel) { realtime_subscribe_client.channels.get(channel_name) } + + it 'receives the message over MsgPack and the data matches' do + expect(realtime_subscribe_client).to be_protocol_binary + + realtime_subscribe_channel.attach do + realtime_subscribe_channel.subscribe do |message| + if encoding_spec['expectedHexValue'] + expect(message.data.unpack('H*').first).to eql(encoding_spec['expectedHexValue']) + else + expect(message.data).to eql(encoding_spec['expectedValue']) + end + stop_reactor + end + + raw_message = { "data" => encoding_spec['data'], "encoding" => encoding_spec['encoding'] } + rest_publish_client.post("/channels/#{channel_name}/messages", JSON.dump(raw_message)) + end + end + end + + context "when retrieving a #{encoding_spec['expectedType']} using JSON protocol" do + let(:rest_publish_client) do + Ably::Rest::Client.new(client_options.merge(protocol: :msgpack)) + end + let(:rest_retrieve_client) do + Ably::Rest::Client.new(client_options.merge(protocol: :json)) + end + let(:rest_publish_channel) { rest_publish_client.channels.get(channel_name) } + + it 'is compatible with a publishes using MsgPack' do + expect(rest_publish_client).to be_protocol_binary + + data = if encoding_spec['expectedHexValue'] + encoding_spec['expectedHexValue'].scan(/../).map { |x| x.hex }.pack('c*') + else + encoding_spec['expectedValue'] + end + rest_publish_channel.publish "event", data + + response = rest_retrieve_client.get("/channels/#{channel_name}/messages") + message = response.body[0] + expect(message['encoding']).to eql(encoding_spec['encoding']) + expect(encoding_spec['data']).to eql(message['data']) + stop_reactor end end end end end