Skip to content

Commit

Permalink
Merge pull request #26 from vbyno/pass_user_agent_header
Browse files Browse the repository at this point in the history
Add ability to pass 'User-Agent' header
  • Loading branch information
main24 authored Jun 3, 2019
2 parents 370a758 + 277f397 commit 4fbc863
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 39 deletions.
37 changes: 12 additions & 25 deletions lib/mws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,17 @@
require_relative 'mws/api/merchant_fulfillment'

module MWS
@@aws_access_key_id = nil
@@aws_secret_access_key = nil

def self.aws_access_key_id=(key_id)
@@aws_access_key_id = key_id
end

def self.aws_access_key_id
@@aws_access_key_id
end

def self.aws_secret_access_key=(secret_key)
@@aws_secret_access_key = secret_key
end

def self.aws_secret_access_key
@@aws_secret_access_key
end

def self.new(options = {})
@connection = MWS::Connection.new(options)
end

def self.config
yield self
class << self
attr_accessor :aws_access_key_id,
:aws_secret_access_key,
:user_agent

def config
yield self
end

def new(options = {})
MWS::Connection.new(options)
end
end
end
14 changes: 10 additions & 4 deletions lib/mws/api/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Base

# TODO: Temporary solution, move to configuration
DEFAULT_TIMEOUT = 2000
USER_AGENT = 'User-Agent'.freeze

def initialize(connection)
@verb ||= :get
Expand All @@ -20,7 +21,10 @@ def call(action, params = {})
when 'GET'
HTTParty.get(query.request_uri, http_request_options)
when 'POST'
HTTParty.post(query.request_uri, (params[:request_params] || {}).merge(http_request_options))
HTTParty.post(
query.request_uri,
params.fetch(:request_params, {}).deep_merge(http_request_options)
)
end
end

Expand All @@ -46,9 +50,11 @@ def method_missing(name, *args)
end

def http_request_options
{
timeout: DEFAULT_TIMEOUT
}
@http_request_options ||= begin
options = { timeout: DEFAULT_TIMEOUT }
options.merge!(headers: { USER_AGENT => MWS.user_agent }) if MWS.user_agent.present?
options
end
end
end
end
Expand Down
72 changes: 62 additions & 10 deletions spec/mws-rb/api/base_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
require 'spec_helper'

describe MWS::API::Base do
class TestApi < MWS::API::Base
ACTIONS = [:test_action].freeze

def initialize(connection)
@uri = '/Products/2011-10-01'
@version = '2011-10-01'
super(connection)
end
end

let(:connection) do
MWS::Connection.new(aws_access_key_id: 'access key',
aws_secret_access_key: 'secret key',
seller_id: 'seller id',
mws_auth_token: 'auth token')
end

let(:test_api) { TestApi.new(connection) }
let(:base) { MWS::API::Base.new(connection) }

it 'should receive a connection object' do
Expand All @@ -29,20 +40,61 @@
end

describe 'method_missing to call actions' do
class TestApi < MWS::API::Base
ACTIONS = [:test_action].freeze
def initialize(connection)
@uri = '/Products/2011-10-01'
@version = '2011-10-01'
super(connection)
end
end

let(:test_api) { TestApi.new(connection) }
before(:each) { allow(HTTParty).to receive(:get).and_return({}) }

it 'should raise exception if Actions do not contain the action name' do
expect { test_api.action_not_found }.to raise_error(NoMethodError)
end
end

context 'user agent' do
subject { test_api.test_action(params) }

before do |example|
@original_user_agent = MWS.user_agent
MWS.config { |config| config.user_agent = user_agent }
end

after do
subject
MWS.config { |config| config.user_agent = @original_user_agent }
end

let(:params) { {} }

context 'presents in configuration' do
let(:user_agent) { 'My Seller Tool/2.0 (Language=Java/1.6.0.11; Platform=Windows/XP' }

it 'User-Agent is passed' do
expect(HTTParty).to receive(:get) do |_uri, params|
expect(params[:headers]['User-Agent']).to eq user_agent
end
end

context 'other header params presents as well' do
let(:params) { { request_params: { headers: { 'Content-Type' => 'text/xml' } } } }

before { test_api.instance_variable_set(:@verb, :post) }

it 'merges all header params' do
expect(HTTParty).to receive(:post) do |_uri, params|
params.fetch(:headers).tap do |headers|
expect(headers.fetch('User-Agent')).to eq user_agent
expect(headers.fetch('Content-Type')).to eq 'text/xml'
end
end
end
end
end

context 'is not present in configuration' do
let(:user_agent) { nil }

it 'headers are not passed' do
expect(HTTParty).to receive(:get) do |_uri, params|
expect(params.key?(:headers)).to be false
end
end
end
end
end

0 comments on commit 4fbc863

Please sign in to comment.