lib/submodules/ably-ruby/spec/acceptance/rest/client_spec.rb in ably-rest-0.9.3 vs lib/submodules/ably-ruby/spec/acceptance/rest/client_spec.rb in ably-rest-1.0.0

- old
+ new

@@ -49,10 +49,18 @@ it 'uses token authentication' do expect(client.auth).to be_using_token_auth end end + context 'with a non string :client_id' do + let(:client) { Ably::Rest::Client.new(client_options.merge(key: api_key, client_id: 1)) } + + it 'raises an ArgumentError' do + expect { client.auth }.to raise_error ArgumentError, /client_id.*String/ + end + end + context 'with an invalid wildcard "*" :client_id' do it 'raises an exception' do expect { Ably::Rest::Client.new(client_options.merge(key: api_key, client_id: '*')) }.to raise_error ArgumentError end end @@ -68,10 +76,25 @@ it 'uses token authentication' do expect(client.auth).to be_using_token_auth end end + context 'with :default_token_params' do + let(:client) do + Ably::Rest::Client.new(client_options.merge( + default_token_params: { client_id: 'bob' }, + use_token_auth: true, + key: api_key + )) + end + + it 'overides the default token params (#TO3j11)' do + client.auth.authorize + expect(client.auth.client_id).to eql('bob') + end + end + context 'with an :auth_callback Proc (clientId provided in library options instead of as a token_request param)' do let(:client) { Ably::Rest::Client.new(client_options.merge(client_id: client_id, auth_callback: Proc.new { token_request })) } let(:token_request) { client.auth.create_token_request({}, key_name: key_name, key_secret: key_secret) } it 'correctly sets the clientId on the token' do @@ -270,20 +293,20 @@ let(:publish_block) { proc { client.channel('test').publish('event', 'data') } } context 'configured' do let(:client_options) { default_options.merge(key: api_key, environment: 'production') } - it 'should make connection attempts to A.ably-realtime.com, B.ably-realtime.com, C.ably-realtime.com, D.ably-realtime.com, E.ably-realtime.com' do + it 'should make connection attempts to A.ably-realtime.com, B.ably-realtime.com, C.ably-realtime.com, D.ably-realtime.com, E.ably-realtime.com (#RSC15a)' do hosts = [] 5.times do hosts << client.fallback_connection.host end expect(hosts).to match_array(%w(A.ably-realtime.com B.ably-realtime.com C.ably-realtime.com D.ably-realtime.com E.ably-realtime.com)) end end - context 'when environment is NOT production' do + context 'when environment is NOT production (#RSC15b)' do let(:client_options) { default_options.merge(environment: 'sandbox', key: api_key) } let!(:default_host_request_stub) do stub_request(:post, "https://#{environment}-#{Ably::Rest::Client::DOMAIN}#{path}").to_return do raise Faraday::TimeoutError.new('timeout error message') end @@ -302,11 +325,12 @@ let(:client_options) do default_options.merge( environment: nil, key: api_key, http_max_retry_duration: max_retry_duration, - http_max_retry_count: max_retry_count + http_max_retry_count: max_retry_count, + log_level: :error ) end before do stub_const 'Ably::FALLBACK_HOSTS', custom_hosts @@ -325,11 +349,11 @@ stub_request(:post, "https://#{Ably::Rest::Client::DOMAIN}#{path}").to_return do raise Faraday::TimeoutError.new('timeout error message') end end - it "tries fallback hosts #{http_defaults.fetch(:max_retry_count)} times" do + it "tries fallback hosts #{http_defaults.fetch(:max_retry_count)} times (#RSC15b, #RSC15b)" do expect { publish_block.call }.to raise_error Ably::Exceptions::ConnectionError, /ssl error message/ expect(default_host_request_stub).to have_been_requested expect(first_fallback_request_stub).to have_been_requested expect(second_fallback_request_stub).to have_been_requested end @@ -364,10 +388,47 @@ expect(first_fallback_request_stub).to have_been_requested expect(second_fallback_request_stub).to have_been_requested end end + context 'and first request to primary endpoint fails' do + let(:client_options) do + default_options.merge( + environment: nil, + key: api_key, + http_max_retry_duration: max_retry_duration, + http_max_retry_count: max_retry_count, + log_level: :error + ) + end + let(:requests) { [] } + let!(:default_host_request_stub) do + stub_request(:post, "https://#{Ably::Rest::Client::DOMAIN}#{path}").to_return do + requests << true + if requests.count == 1 + raise Faraday::ConnectionFailed.new('connection failure error message') + else + { + headers: { 'Content-Type' => 'application/json' }, + status: 200, + body: {}.to_json + } + end + end + end + + it "tries a fallback host, and for the next request tries the primary endpoint again (#RSC15e)" do + expect { publish_block.call }.to raise_error Ably::Exceptions::ConnectionError, /ssl error message/ + expect(default_host_request_stub).to have_been_requested + expect(first_fallback_request_stub).to have_been_requested + expect(requests.count).to eql(1) + + publish_block.call + expect(requests.count).to eql(2) + end + end + context 'and basic authentication fails' do let(:status) { 401 } let!(:default_host_request_stub) do stub_request(:post, "https://#{Ably::Rest::Client::DOMAIN}#{path}").to_return( headers: { 'Content-Type' => 'application/json' }, @@ -402,11 +463,11 @@ end let!(:default_host_request_stub) do stub_request(:post, "https://#{Ably::Rest::Client::DOMAIN}#{path}").to_return(&fallback_block) end - it 'attempts the fallback hosts as this is an authentication failure' do + it 'attempts the fallback hosts as this is an authentication failure (#RSC15d)' do expect { publish_block.call }.to raise_error(Ably::Exceptions::ServerError) expect(default_host_request_stub).to have_been_requested expect(first_fallback_request_stub).to have_been_requested expect(second_fallback_request_stub).to have_been_requested end @@ -448,22 +509,22 @@ let!(:second_fallback_request_stub) do stub_request(:post, "https://#{custom_hosts[1]}#{path}").to_return(&fallback_block) end let(:client_options) { - production_options.merge(fallback_hosts: custom_hosts) + production_options.merge(fallback_hosts: custom_hosts, log_level: :error) } - it 'attempts the fallback hosts as this is an authentication failure (#RSC15b, #TO3k6)' do + it 'attempts the fallback hosts as this is an authentication failure (#RSC15b, #RSC15a, #TO3k6)' do expect { publish_block.call }.to raise_error(Ably::Exceptions::ServerError) expect(default_host_request_stub).to have_been_requested expect(first_fallback_request_stub).to have_been_requested expect(second_fallback_request_stub).to have_been_requested end end - context 'with an empty array of fallback hosts provided (#RSC15b, #TO3k6)' do + context 'with an empty array of fallback hosts provided (#RSC15b, #RSC15a, #TO3k6)' do let(:client_options) { production_options.merge(fallback_hosts: []) } it 'does not attempt the fallback hosts as this is an authentication failure' do @@ -483,11 +544,11 @@ @web_server.shutdown end context 'and timing out the primary host' do before do - @web_server = WEBrick::HTTPServer.new(:Port => port, :SSLEnable => false) + @web_server = WEBrick::HTTPServer.new(:Port => port, :SSLEnable => false, :AccessLog => [], Logger: WEBrick::Log.new("/dev/null")) @web_server.mount_proc "/channels/#{channel_name}/publish" do |req, res| if req.header["host"].first.include?(primary_host) @primary_host_requested = true sleep request_timeout + 0.5 else @@ -514,16 +575,17 @@ fallback_hosts: fallbacks, token: 'fake.token', port: port, tls: false, http_request_timeout: request_timeout, - max_retry_duration: request_timeout * 3 + max_retry_duration: request_timeout * 3, + log_level: :error ) end let(:fail_fallback_request_count) { 1 } - it 'tries one of the fallback hosts' do + it 'tries one of the fallback hosts (#RSC15d)' do client.channel(channel_name).publish('event', 'data') expect(@primary_host_requested).to be_truthy expect(@fallback_request_count).to eql(2) end end @@ -535,26 +597,27 @@ fallback_hosts: fallbacks, token: 'fake.token', port: port, tls: false, http_request_timeout: request_timeout, - max_retry_duration: request_timeout / 2 + max_retry_duration: request_timeout / 2, + log_level: :error ) end let(:fail_fallback_request_count) { 0 } - it 'tries one of the fallback hosts' do + it 'tries one of the fallback hosts (#RSC15d)' do client.channel(channel_name).publish('event', 'data') expect(@primary_host_requested).to be_truthy expect(@fallback_request_count).to eql(1) end end end context 'and failing the primary host' do before do - @web_server = WEBrick::HTTPServer.new(:Port => port, :SSLEnable => false) + @web_server = WEBrick::HTTPServer.new(:Port => port, :SSLEnable => false, :AccessLog => [], Logger: WEBrick::Log.new("/dev/null")) @web_server.mount_proc "/channels/#{channel_name}/publish" do |req, res| if req.header["host"].first.include?(primary_host) @primary_host_requested = true res.status = 500 else @@ -578,11 +641,12 @@ default_options.merge( rest_host: primary_host, fallback_hosts: fallbacks, token: 'fake.token', port: port, - tls: false + tls: false, + log_level: :error ) end let(:fail_fallback_request_count) { 1 } it 'tries one of the fallback hosts' do @@ -630,14 +694,14 @@ let!(:second_fallback_request_stub) do stub_request(:post, "https://#{custom_hosts[1]}#{path}").to_return(&fallback_block) end let(:client_options) { - production_options.merge(fallback_hosts: custom_hosts) + production_options.merge(fallback_hosts: custom_hosts, log_level: :error) } - it 'attempts the fallback hosts as this is an authentication failure' do + it 'attempts the fallback hosts as this is not an authentication failure' do expect { publish_block.call }.to raise_error(Ably::Exceptions::ServerError) expect(default_host_request_stub).to have_been_requested expect(first_fallback_request_stub).to have_been_requested expect(second_fallback_request_stub).to have_been_requested end @@ -659,24 +723,20 @@ before do stub_const 'Ably::FALLBACK_HOSTS', custom_hosts end - let(:client_options) { - production_options.merge(fallback_hosts_use_default: true) - } - let!(:first_fallback_request_stub) do stub_request(:post, "https://#{Ably::FALLBACK_HOSTS[0]}#{path}").to_return(&fallback_block) end let!(:second_fallback_request_stub) do stub_request(:post, "https://#{Ably::FALLBACK_HOSTS[1]}#{path}").to_return(&fallback_block) end let(:client_options) { - production_options.merge(fallback_hosts: custom_hosts) + production_options.merge(fallback_hosts: custom_hosts, log_level: :error) } it 'attempts the default fallback hosts as this is an authentication failure' do expect { publish_block.call }.to raise_error(Ably::Exceptions::ServerError) expect(default_host_request_stub).to have_been_requested @@ -756,20 +816,20 @@ context 'defaults' do specify '#http_open_timeout is 4s' do expect(client.http_defaults[:open_timeout]).to eql(4) end - specify '#http_request_timeout is 15s' do - expect(client.http_defaults[:request_timeout]).to eql(15) + specify '#http_request_timeout is 10s' do + expect(client.http_defaults[:request_timeout]).to eql(10) end specify '#http_max_retry_count is 3' do expect(client.http_defaults[:max_retry_count]).to eql(3) end - specify '#http_max_retry_duration is 10s' do - expect(client.http_defaults[:max_retry_duration]).to eql(10) + specify '#http_max_retry_duration is 15s' do + expect(client.http_defaults[:max_retry_duration]).to eql(15) end end context 'configured' do let(:client_options) do @@ -846,12 +906,13 @@ 'X-Ably-Lib' => lib.join('-') }). to_return(status: 201, body: '{}', headers: { 'Content-Type' => 'application/json' }) end - it 'sends a protocol version and lib version header' do + it 'sends a protocol version and lib version header (#G4, #RSC7a, #RSC7b)' do client.channels.get('foo').publish("event") expect(publish_message_stub).to have_been_requested + expect(Ably::PROTOCOL_VERSION).to eql('1.0') end end end end