lib/submodules/ably-ruby/spec/acceptance/realtime/client_spec.rb in ably-rest-1.0.6 vs lib/submodules/ably-ruby/spec/acceptance/realtime/client_spec.rb in ably-rest-1.1.0

- old
+ new

@@ -10,10 +10,11 @@ let(:client_options) { default_options } let(:connection) { subject.connection } let(:auth_params) { subject.auth.auth_params_sync } subject { auto_close Ably::Realtime::Client.new(client_options) } + let(:sub_client) { auto_close Ably::Realtime::Client.new(client_options) } context 'initialization' do context 'basic auth' do it 'is enabled by default with a provided :key option' do connection.on(:connected) do @@ -22,10 +23,26 @@ expect(subject.auth.current_token_details).to be_nil stop_reactor end end + context 'with an invalid API key' do + let(:custom_logger_object) { TestLogger.new } + let(:client) { Ably::Realtime::Client.new(client_options.merge(key: 'app.key:secret', logger: custom_logger_object)) } + + it 'logs an entry with a help href url matching the code #TI5' do + client.connect + client.connection.once(:failed) do + expect(custom_logger_object.logs.find do |severity, message| + next unless %w(fatal error).include?(severity.to_s) + message.match(%r{https://help.ably.io/error/40400}) + end).to_not be_nil + stop_reactor + end + end + end + context ':tls option' do context 'set to false to force a plain-text connection' do let(:client_options) { default_options.merge(tls: false, log_level: :none) } it 'fails to connect because a private key cannot be sent over a non-secure connection' do @@ -275,9 +292,141 @@ expect(third_page.items.length).to eql(2) stop_reactor end end end + end + end + end + end + + context '#publish (#TBC)' do + let(:channel_name) { random_str } + let(:channel) { subject.channel(channel_name) } + let(:sub_channel) { sub_client.channel(channel_name) } + let(:event_name) { random_str } + let(:data) { random_str } + let(:extras) { { 'push' => { 'notification' => { 'title' => 'Testing' } } } } + let(:message) { Ably::Models::Message.new(name: event_name, data: data) } + + specify 'publishing a message implicity connects and publishes the message successfully on the provided channel' do + sub_channel.attach do + sub_channel.subscribe do |msg| + expect(msg.name).to eql(event_name) + expect(msg.data).to eql(data) + stop_reactor + end + end + subject.publish channel_name, event_name, data + end + + specify 'publishing does not result in a channel being created' do + subject.publish channel_name, event_name, data + subject.channels.fetch(channel_name) do + # Block called if channel does not exist + EventMachine.add_timer(1) do + subject.channels.fetch(channel_name) do + # Block called if channel does not exist + stop_reactor + end + end + end + end + + context 'with extras' do + let(:channel_name) { "pushenabled:#{random_str}" } + + specify 'publishing supports extras' do + sub_channel.attach do + sub_channel.subscribe do |msg| + expect(msg.extras).to eql(extras) + stop_reactor + end + end + subject.publish channel_name, event_name, {}, extras: extras + end + end + + specify 'publishing supports an array of Message objects' do + sub_channel.attach do + sub_channel.subscribe do |msg| + expect(msg.name).to eql(event_name) + expect(msg.data).to eql(data) + stop_reactor + end + end + subject.publish channel_name, [message] + end + + specify 'publishing supports an array of Hash objects' do + sub_channel.attach do + sub_channel.subscribe do |msg| + expect(msg.name).to eql(event_name) + expect(msg.data).to eql(data) + stop_reactor + end + end + subject.publish channel_name, [name: event_name, data: data] + end + + specify 'publishing on a closed connection fails' do + subject.connection.once(:connected) do + subject.connection.once(:closed) do + subject.publish(channel_name, name: event_name).errback do |error| + expect(error).to be_kind_of(Ably::Exceptions::MessageQueueingDisabled) + stop_reactor + end + end + connection.close + end + end + + context 'queue_messages ClientOption' do + context 'when true' do + subject { auto_close Ably::Realtime::Client.new(client_options.merge(auto_connect: false)) } + + it 'will queue messages whilst connecting and publish once connected' do + sub_channel.attach do + sub_channel.subscribe do |msg| + expect(msg.name).to eql(event_name) + stop_reactor + end + subject.connection.once(:connecting) do + subject.publish channel_name, event_name + end + subject.connection.connect + end + end + end + + context 'when false' do + subject { auto_close Ably::Realtime::Client.new(client_options.merge(auto_connect: false, queue_messages: false)) } + + it 'will reject messages on an initializing connection' do + sub_channel.attach do + subject.connection.once(:connecting) do + subject.publish(channel_name, event_name).errback do |error| + expect(error).to be_kind_of(Ably::Exceptions::MessageQueueingDisabled) + stop_reactor + end + end + subject.connection.connect + end + end + end + end + + context 'with more than allowed messages in a single publish' do + let(:channel_name) { random_str } + + it 'rejects the publish' do + messages = (Ably::Realtime::Connection::MAX_PROTOCOL_MESSAGE_BATCH_SIZE + 1).times.map do + { name: 'foo' } + end + + subject.publish(channel_name, messages).errback do |error| + expect(error).to be_kind_of(Ably::Exceptions::InvalidRequest) + stop_reactor end end end end end