lib/submodules/ably-ruby/spec/acceptance/rest/client_spec.rb in ably-rest-0.8.14 vs lib/submodules/ably-ruby/spec/acceptance/rest/client_spec.rb in ably-rest-0.8.15

- old
+ new

@@ -109,11 +109,12 @@ context 'with basic auth', webmock: true do let(:client_options) { default_options.merge(key: api_key) } let!(:get_message_history_stub) do - stub_request(:get, "https://#{api_key}@#{environment}-#{Ably::Rest::Client::DOMAIN}/channels/#{channel_name}/messages?#{history_querystring}"). + stub_request(:get, "https://#{environment}-#{Ably::Rest::Client::DOMAIN}/channels/#{channel_name}/messages?#{history_querystring}"). + with(basic_auth: [key_name, key_secret]). to_return(body: [], headers: { 'Content-Type' => 'application/json' }) end it 'sends the API key in authentication part of the secure URL (the Authorization: Basic header is not used with the Faraday HTTP library by default)' do client.channel(channel_name).history history_params @@ -282,11 +283,13 @@ end context 'when environment is NOT production' do let(:client_options) { default_options.merge(environment: 'sandbox', key: api_key) } let!(:default_host_request_stub) do - stub_request(:post, "https://#{api_key}@#{environment}-#{Ably::Rest::Client::DOMAIN}#{path}").to_return do + stub_request(:post, "https://#{environment}-#{Ably::Rest::Client::DOMAIN}#{path}"). + with(basic_auth: [key_name, key_secret]). + to_return do raise Faraday::TimeoutError.new('timeout error message') end end it 'does not retry failed requests with fallback hosts when there is a connection error' do @@ -311,20 +314,20 @@ before do stub_const 'Ably::FALLBACK_HOSTS', custom_hosts end let!(:first_fallback_request_stub) do - stub_request(:post, "https://#{api_key}@#{custom_hosts[0]}#{path}").to_return(&fallback_block) + stub_request(:post, "https://#{custom_hosts[0]}#{path}").with(basic_auth: [key_name, key_secret]).to_return(&fallback_block) end let!(:second_fallback_request_stub) do - stub_request(:post, "https://#{api_key}@#{custom_hosts[1]}#{path}").to_return(&fallback_block) + stub_request(:post, "https://#{custom_hosts[1]}#{path}").with(basic_auth: [key_name, key_secret]).to_return(&fallback_block) end context 'and connection times out' do let!(:default_host_request_stub) do - stub_request(:post, "https://#{api_key}@#{Ably::Rest::Client::DOMAIN}#{path}").to_return do + stub_request(:post, "https://#{Ably::Rest::Client::DOMAIN}#{path}").with(basic_auth: [key_name, key_secret]).to_return do raise Faraday::TimeoutError.new('timeout error message') end end it "tries fallback hosts #{http_defaults.fetch(:max_retry_count)} times" do @@ -334,11 +337,11 @@ expect(second_fallback_request_stub).to have_been_requested end context "and the total request time exeeds #{http_defaults.fetch(:max_retry_duration)} seconds" do let!(:default_host_request_stub) do - stub_request(:post, "https://#{api_key}@#{Ably::Rest::Client::DOMAIN}#{path}").to_return do + stub_request(:post, "https://#{Ably::Rest::Client::DOMAIN}#{path}").with(basic_auth: [key_name, key_secret]).to_return do sleep max_retry_duration * 1.5 raise Faraday::TimeoutError.new('timeout error message') end end @@ -351,11 +354,11 @@ end end context 'and connection fails' do let!(:default_host_request_stub) do - stub_request(:post, "https://#{api_key}@#{Ably::Rest::Client::DOMAIN}#{path}").to_return do + stub_request(:post, "https://#{Ably::Rest::Client::DOMAIN}#{path}").with(basic_auth: [key_name, key_secret]).to_return do raise Faraday::ConnectionFailed.new('connection failure error message') end end it "tries fallback hosts #{http_defaults.fetch(:max_retry_count)} times" do @@ -367,11 +370,11 @@ end context 'and basic authentication fails' do let(:status) { 401 } let!(:default_host_request_stub) do - stub_request(:post, "https://#{api_key}@#{Ably::Rest::Client::DOMAIN}#{path}").to_return( + stub_request(:post, "https://#{Ably::Rest::Client::DOMAIN}#{path}").with(basic_auth: [key_name, key_secret]).to_return( headers: { 'Content-Type' => 'application/json' }, status: status, body: { "error" => { "statusCode" => 401, @@ -399,11 +402,11 @@ status: status } end end let!(:default_host_request_stub) do - stub_request(:post, "https://#{api_key}@#{Ably::Rest::Client::DOMAIN}#{path}").to_return(&fallback_block) + stub_request(:post, "https://#{Ably::Rest::Client::DOMAIN}#{path}").with(basic_auth: [key_name, key_secret]).to_return(&fallback_block) end it 'attempts the 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 @@ -435,24 +438,24 @@ status: status } end end let!(:default_host_request_stub) do - stub_request(:post, "https://#{api_key}@#{Ably::Rest::Client::DOMAIN}#{path}").to_return(&fallback_block) + stub_request(:post, "https://#{Ably::Rest::Client::DOMAIN}#{path}").with(basic_auth: [key_name, key_secret]).to_return(&fallback_block) end context 'with custom fallback hosts provided' do let!(:first_fallback_request_stub) do - stub_request(:post, "https://#{api_key}@#{custom_hosts[0]}#{path}").to_return(&fallback_block) + stub_request(:post, "https://#{custom_hosts[0]}#{path}").with(basic_auth: [key_name, key_secret]).to_return(&fallback_block) end let!(:second_fallback_request_stub) do - stub_request(:post, "https://#{api_key}@#{custom_hosts[1]}#{path}").to_return(&fallback_block) + stub_request(:post, "https://#{custom_hosts[1]}#{path}").with(basic_auth: [key_name, key_secret]).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 expect { publish_block.call }.to raise_error(Ably::Exceptions::ServerError) expect(default_host_request_stub).to have_been_requested @@ -514,11 +517,12 @@ 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 @@ -617,20 +621,20 @@ status: status } end end let!(:default_host_request_stub) do - stub_request(:post, "https://#{api_key}@#{env}-#{Ably::Rest::Client::DOMAIN}#{path}").to_return(&fallback_block) + stub_request(:post, "https://#{env}-#{Ably::Rest::Client::DOMAIN}#{path}").with(basic_auth: [key_name, key_secret]).to_return(&fallback_block) end context 'with custom fallback hosts provided (#RSC15b, #TO3k6)' do let!(:first_fallback_request_stub) do - stub_request(:post, "https://#{api_key}@#{custom_hosts[0]}#{path}").to_return(&fallback_block) + stub_request(:post, "https://#{custom_hosts[0]}#{path}").with(basic_auth: [key_name, key_secret]).to_return(&fallback_block) end let!(:second_fallback_request_stub) do - stub_request(:post, "https://#{api_key}@#{custom_hosts[1]}#{path}").to_return(&fallback_block) + stub_request(:post, "https://#{custom_hosts[1]}#{path}").with(basic_auth: [key_name, key_secret]).to_return(&fallback_block) end let(:client_options) { production_options.merge(fallback_hosts: custom_hosts) } @@ -664,15 +668,15 @@ let(:client_options) { production_options.merge(fallback_hosts_use_default: true) } let!(:first_fallback_request_stub) do - stub_request(:post, "https://#{api_key}@#{Ably::FALLBACK_HOSTS[0]}#{path}").to_return(&fallback_block) + stub_request(:post, "https://#{Ably::FALLBACK_HOSTS[0]}#{path}").with(basic_auth: [key_name, key_secret]).to_return(&fallback_block) end let!(:second_fallback_request_stub) do - stub_request(:post, "https://#{api_key}@#{Ably::FALLBACK_HOSTS[1]}#{path}").to_return(&fallback_block) + stub_request(:post, "https://#{Ably::FALLBACK_HOSTS[1]}#{path}").with(basic_auth: [key_name, key_secret]).to_return(&fallback_block) end let(:client_options) { production_options.merge(fallback_hosts: custom_hosts) } @@ -699,11 +703,11 @@ context 'fallback hosts', :webmock do let(:path) { '/channels/test/publish' } let!(:custom_host_request_stub) do - stub_request(:post, "https://#{api_key}@#{custom_host}#{path}").to_return do + stub_request(:post, "https://#{custom_host}#{path}").with(basic_auth: [key_name, key_secret]).to_return do raise Faraday::ConnectionFailed.new('connection failure error message') end end before do @@ -838,20 +842,64 @@ lib = ['ruby'] lib << variant if variant lib << Ably::VERSION - stub_request(:post, "#{client.endpoint.to_s.gsub('://', "://#{api_key}@")}/channels/foo/publish"). + stub_request(:post, "#{client.endpoint.to_s.gsub('://', "://")}/channels/foo/publish"). with(headers: { 'X-Ably-Version' => Ably::PROTOCOL_VERSION, 'X-Ably-Lib' => lib.join('-') }). + with(basic_auth: [key_name, key_secret]). to_return(status: 201, body: '{}', headers: { 'Content-Type' => 'application/json' }) end it 'sends a protocol version and lib version header' do client.channels.get('foo').publish("event") expect(publish_message_stub).to have_been_requested + end + end + end + end + + context '#request (#RSC19*)' do + let(:client_options) { default_options.merge(key: api_key) } + + context 'get' do + it 'returns an HttpPaginatedResponse object' do + response = client.request(:get, 'time') + expect(response).to be_a(Ably::Models::HttpPaginatedResponse) + expect(response.status_code).to eql(200) + end + + context '404 request to invalid URL' do + it 'returns an object with 404 status code and error message' do + response = client.request(:get, 'does-not-exist') + expect(response).to be_a(Ably::Models::HttpPaginatedResponse) + expect(response.error_message).to match(/Could not find/) + expect(response.error_code).to eql(40400) + expect(response.status_code).to eql(404) + end + end + + context 'paged results' do + let(:channel_name) { random_str } + + it 'provides paging' do + 10.times do + client.request(:post, "/channels/#{channel_name}/publish", {}, { 'name': 'test' }) + end + response = client.request(:get, "/channels/#{channel_name}/messages", { limit: 2 }) + expect(response.items.length).to eql(2) + expect(response).to be_has_next + next_page = response.next + expect(next_page.items.length).to eql(2) + expect(next_page).to be_has_next + first_page_ids = response.items.map { |message| message['id'] }.uniq.sort + next_page_ids = next_page.items.map { |message| message['id'] }.uniq.sort + expect(first_page_ids).to_not eql(next_page_ids) + next_page = next_page.next + expect(next_page.items.length).to eql(2) end end end end end