test/unit/peddler/test_client.rb in peddler-1.6.7 vs test/unit/peddler/test_client.rb in peddler-2.0.0

- old
+ new

@@ -3,14 +3,11 @@ require 'helper' require 'null_client' class TestPeddlerClient < MiniTest::Test def setup - @response_body = 'foo' Excon.defaults[:mock] = true - Excon.stub({}, body: @response_body, status: 200) - @klass = Class.new(Null::Client) @client = @klass.new @client.configure_with_mock_data! @client.operation('Foo') end @@ -18,240 +15,189 @@ def teardown Excon.stubs.clear Excon.defaults.delete(:mock) end - def test_configures_path - @klass.path('/Foo') - assert @client.aws_endpoint.match(%r{/Foo$}) - end + class HappyPath < TestPeddlerClient + def setup + @response_body = 'foo' + Excon.stub({}, body: @response_body, status: 200) + super + end - def test_instance_path_overrides_class_path - @klass.path('/Foo') + def test_has_user_agent + assert @client.connection.data[:headers].key?('User-Agent') + end - @client.path = '/Foo/Bar' - assert @client.aws_endpoint.match(%r{/Foo/Bar$}) - end + def test_inherits_parents_params + assert_equal Peddler::Client.params, @klass.params + end - def test_default_path - assert_equal '/', @klass.path - end + def test_params_include_seller_id + assert @klass.params.key?('SellerId') + end - def test_has_user_agent - assert @client.connection.data[:headers].key?('User-Agent') - end + def test_params_include_auth_token + @klass.params.key?('MWSAuthToken') + end - def test_inherits_parents_params - assert_equal Peddler::Client.params, @klass.params - end + def test_inherits_parents_parser + assert_equal @klass.parser, Class.new(@klass).parser + end - def test_inherits_parents_path - assert_equal @klass.path, Class.new(@klass).path - end + def test_sets_marketplace + marketplace = Peddler::Marketplace.find('US') + @client.marketplace = 'US' + assert_equal marketplace, @client.marketplace + @client.marketplace = marketplace + assert_equal marketplace, @client.marketplace + end - def test_inherits_parents_parser - assert_equal @klass.parser, Class.new(@klass).parser - end + def test_configures_when_initialising + client = @klass.new(aws_access_key_id: '123') + assert_equal '123', client.aws_access_key_id + end - def test_params_include_seller_id - assert @klass.params.key?('SellerId') - end + def test_sets_content_type_header_for_latin_flat_file + @client.body = 'foo' + content_type = @client.headers.fetch('Content-Type') - def test_params_include_auth_token - @klass.params.key?('MWSAuthToken') - end - - def test_configures - @client.configure do |config| - config.aws_access_key_id = '123' + assert_equal 'text/tab-separated-values; charset=CP1252', content_type end - assert_equal '123', @client.aws_access_key_id - end + def test_sets_content_type_header_for_chinese_flat_file + @client.marketplace = 'CN' + @client.body = 'foo' + content_type = @client.headers.fetch('Content-Type') - def test_configures_when_initialising - client = @klass.new(aws_access_key_id: '123') - assert_equal '123', client.aws_access_key_id - end + assert_equal 'text/tab-separated-values; charset=UTF-16', content_type + end - def test_sets_content_type_header_for_latin_flat_file - @client.body = 'foo' - content_type = @client.headers.fetch('Content-Type') + def test_sets_content_type_header_for_japanese_flat_file + @client.marketplace = 'JP' + @client.body = 'foo' + content_type = @client.headers.fetch('Content-Type') - assert_equal 'text/tab-separated-values; charset=CP1252', content_type - end + assert_equal 'text/tab-separated-values; charset=Windows-31J', content_type + end - def test_sets_content_type_header_for_chinese_flat_file - @client.primary_marketplace_id = 'AAHKV2X7AFYLW' - @client.body = 'foo' - content_type = @client.headers.fetch('Content-Type') + def test_sets_content_type_header_for_xml + @client.body = '<?xml version="1.0"?><Foo></Foo>' + content_type = @client.headers.fetch('Content-Type') - assert_equal 'text/tab-separated-values; charset=UTF-16', content_type - end + assert_equal 'text/xml', content_type + end - def test_sets_content_type_header_for_japanese_flat_file - @client.primary_marketplace_id = 'A1VC38T7YXB528' - @client.body = 'foo' - content_type = @client.headers.fetch('Content-Type') + def test_encodes_body_for_latin_flat_file + @client.body = 'foo' + assert_equal 'Windows-1252', @client.body.encoding.to_s + end - assert_equal 'text/tab-separated-values; charset=Windows-31J', content_type - end + def test_encodes_body_for_chinese_flat_file + @client.marketplace = 'CN' + @client.body = 'foo' + assert_equal 'UTF-16', @client.body.encoding.to_s + end - def test_sets_content_type_header_for_xml - @client.body = '<?xml version="1.0"?><Foo></Foo>' - content_type = @client.headers.fetch('Content-Type') + def test_encodes_body_for_japanese_flat_file + @client.marketplace = 'JP' + @client.body = 'foo' + assert_equal 'Windows-31J', @client.body.encoding.to_s + end - assert_equal 'text/xml', content_type - end + def test_runs_a_request + res = @client.run + assert_equal @response_body, res.body + end - def test_encodes_body_for_latin_flat_file - @client.body = 'foo' - assert_equal 'Windows-1252', @client.body.encoding.to_s - end - - def test_encodes_body_for_chinese_flat_file - @client.primary_marketplace_id = 'AAHKV2X7AFYLW' - @client.body = 'foo' - assert_equal 'UTF-16', @client.body.encoding.to_s - end - - def test_encodes_body_for_japanese_flat_file - @client.primary_marketplace_id = 'A1VC38T7YXB528' - @client.body = 'foo' - assert_equal 'Windows-31J', @client.body.encoding.to_s - end - - def test_runs_a_request - res = @client.run - assert_equal @response_body, res.body - end - - def test_clears_body_when_run_succeeds - @client.body = 'foo' - @client.run - assert_nil @client.body - end - - def test_does_not_clear_body_when_run_fails - Excon.stub({}, status: 503) - @client.body = 'foo' - assert_raises(Excon::Error::ServiceUnavailable) do + def test_clears_body_when_run_succeeds + @client.body = 'foo' @client.run + assert_nil @client.body end - refute_nil @client.body - end - def test_streams_response - chunks = '' - streamer = ->(chunk, _, _) { chunks += chunk } - @client.run(&streamer) + def test_streams_response + chunks = '' + streamer = ->(chunk, _, _) { chunks += chunk } + @client.run(&streamer) - assert_equal @response_body, chunks - end + assert_equal @response_body, chunks + end - class Instrumentor - class << self - attr_accessor :events + class Instrumentor + class << self + attr_accessor :events - def instrument(name, params = {}) - events.update(name => params) - yield if block_given? + def instrument(name, params = {}) + events.update(name => params) + yield if block_given? + end end - end - @events = {} - end - - def test_request_preserves_user_agent - @client.defaults.update(instrumentor: Instrumentor) - @client.run - headers = Instrumentor.events['excon.request'][:headers] - - assert headers.key?('User-Agent') - end - - def test_error_callback_on_class - Excon.stub({}, status: 503) - - assert_raises(Excon::Error::ServiceUnavailable) do - @client.run + @events = {} end - @klass.on_error do |e| - assert_equal 503, e.response.status - end - @client.run # no longer raises - - Excon.stubs.clear - end - - def test_error_callback_on_instance - Excon.stub({}, status: 503) - - assert_raises(Excon::Error::ServiceUnavailable) do + def test_request_preserves_user_agent + @client.defaults.update(instrumentor: Instrumentor) @client.run - end + headers = Instrumentor.events['excon.request'][:headers] - @client.on_error do |e| - assert_equal 503, e.response.status + assert headers.key?('User-Agent') end - @client.run - - Excon.stubs.clear end - def test_error_callback_on_client_ancestor - Excon.stub({}, status: 503) - - @klass.on_error do |e| - assert_equal 503, e.response.status + class MWSErrorPath < TestPeddlerClient + def setup + body = <<-XML + <ErrorResponse> + <Error> + <Code>RequestThrottled</Code> + </Error> + </ErrorResponse> + XML + Excon.stub({}, body: body, status: 503) + super end - @client.run # no longer raises - klass = Class.new(Null::Client) - other_client = klass.new - other_client.configure_with_mock_data! - other_client.operation('Foo') - assert_raises(Excon::Error::ServiceUnavailable) do - other_client.run + def test_default_error_handling + assert_raises Peddler::Errors::RequestThrottled do + @client.run + end end - Excon.stubs.clear + def test_does_not_clear_body_when_run_fails + @client.body = 'foo' + assert_raises Peddler::Errors::RequestThrottled do + @client.run + end + refute_nil @client.body + end end - def test_decorates_error_response - res = { - body: '<ErrorResponse><Error>Foo</Error></ErrorResponse>', - status: 503 - } - Excon.stub({}, res) - e = nil - - begin - @client.run - rescue StandardError => e - assert e.response.parse + class OtherHTTPStatusErrorPath < TestPeddlerClient + def setup + body = <<-XML + <ErrorResponse> + <Error> + <Code>500</Code> + </Error> + </ErrorResponse> + XML + Excon.stub({}, body: body, status: 500) + super end - assert e - end - - def test_deprecated_error_callback - Excon.stub({}, status: 503) - - @client.on_error do |_, res| - assert_equal 503, res.status + def test_error_handling + assert_raises Excon::Error::InternalServerError do + @client.run + end end - assert_output nil, /DEPRECATION/ do - @client.run - end - Excon.stubs.clear - end - - def test_deprecated_marketplace_id_accessor - refute_nil @client.marketplace_id - @client.marketplace_id = '123' - assert_equal '123', @client.marketplace_id - assert_equal @client.primary_marketplace_id, @client.marketplace_id + def test_does_not_clear_body_when_run_fails + @client.body = 'foo' + assert_raises Excon::Error::InternalServerError do + @client.run + end + refute_nil @client.body + end end end