Skip to content

Commit

Permalink
The custom user agent is now appended to the default user agent
Browse files Browse the repository at this point in the history
If no custom user agent is provided, the default user agent is used.

    dnsimple-ruby/1.0

If a custom user agent is provided, the final user agent is the combination of the custom user agent
prepended by the default user agent.

    dnsimple-ruby/1.0 customAgentFlag
  • Loading branch information
weppos committed May 26, 2016
1 parent bb0a152 commit 50129d3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
32 changes: 26 additions & 6 deletions lib/dnsimple/client.rb
Expand Up @@ -169,18 +169,19 @@ def request(method, path, data = nil, options = {})
private

def request_options(custom_options = {})
options = base_options
add_auth_options!(options)
add_proxy_options!(options)
Extra.deep_merge!(options, custom_options)
base_options.tap do |options|
Extra.deep_merge!(options, custom_options)
Extra.deep_merge!(options, headers: { "User-Agent" => format_user_agent })
add_auth_options!(options)
add_proxy_options!(options)
end
end

def base_options
{
format: :json,
headers: {
'Accept' => 'application/json',
'User-Agent' => user_agent,
"Accept" => "application/json",
},
}
end
Expand All @@ -201,6 +202,25 @@ def add_auth_options!(options)
end
end

# Builds the final user agent to use for HTTP requests.
#
# If no custom user agent is provided, the default user agent is used.
#
# dnsimple-ruby/1.0
#
# If a custom user agent is provided, the final user agent is the combination
# of the custom user agent prepended by the default user agent.
#
# dnsimple-ruby/1.0 customAgentFlag
#
def format_user_agent
if user_agent.to_s.empty?
Dnsimple::Default::USER_AGENT
else
"#{Dnsimple::Default::USER_AGENT} #{user_agent}"
end
end

def content_type(headers)
headers["Content-Type"] || "application/json"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/dnsimple/default.rb
Expand Up @@ -61,10 +61,10 @@ def domain_api_token
ENV['DNSIMPLE_API_DOMAIN_TOKEN']
end

# Default User-Agent header string from ENV or {USER_AGENT}
# Default User-Agent header string from ENV
# @return [String]
def user_agent
ENV['DNSIMPLE_USER_AGENT'] || USER_AGENT
ENV['DNSIMPLE_USER_AGENT']
end

# Default Proxy address:port from ENV
Expand Down
18 changes: 9 additions & 9 deletions spec/dnsimple/client_spec.rb
Expand Up @@ -118,7 +118,7 @@

expect(WebMock).to have_requested(:get, "https://api.dnsimple.com/foo").
with(basic_auth: %w(user pass),
headers: { 'Accept' => 'application/json', 'User-Agent' => "dnsimple-ruby/#{Dnsimple::VERSION}" })
headers: { 'Accept' => 'application/json', 'User-Agent' => Dnsimple::Default::USER_AGENT })
end

it "delegates to HTTParty" do
Expand All @@ -129,7 +129,7 @@
"#{subject.base_url}foo",
format: :json,
basic_auth: { username: "user", password: "pass" },
headers: { 'Accept' => 'application/json', 'User-Agent' => "dnsimple-ruby/#{Dnsimple::VERSION}" }
headers: { 'Accept' => 'application/json', 'User-Agent' => Dnsimple::Default::USER_AGENT }
).
and_return(double('response', code: 200))

Expand All @@ -144,7 +144,7 @@
body: JSON.dump(something: "else"),
query: { foo: "bar" },
basic_auth: { username: "user", password: "pass" },
headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'User-Agent' => "dnsimple-ruby/#{Dnsimple::VERSION}", "Custom" => "Header" }
headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'User-Agent' => Dnsimple::Default::USER_AGENT, "Custom" => "Header" }
).
and_return(double('response', code: 200))

Expand All @@ -158,7 +158,7 @@
format: :json,
body: { something: "else" },
basic_auth: { username: "user", password: "pass" },
headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => "dnsimple-ruby/#{Dnsimple::VERSION}" }
headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => Dnsimple::Default::USER_AGENT }
).
and_return(double('response', code: 200))

Expand All @@ -172,25 +172,25 @@
format: :json,
http_proxyaddr: "example-proxy.com",
http_proxyport: "4321",
headers: { 'Accept' => 'application/json', 'User-Agent' => "dnsimple-ruby/#{Dnsimple::VERSION}" }
headers: { 'Accept' => 'application/json', 'User-Agent' => Dnsimple::Default::USER_AGENT }
).
and_return(double('response', code: 200))

subject = described_class.new(proxy: "example-proxy.com:4321")
subject.request(:get, "test", nil, {})
end

it "default options can be overriden" do
it "supports custom user agent" do
expect(HTTParty).to receive(:get).
with(
"#{subject.base_url}test",
format: :json,
headers: { "Accept" => "application/json", "User-Agent" => "dnsimple-custom-integration" }
headers: hash_including("User-Agent" => "#{Dnsimple::Default::USER_AGENT} customAgent")
).
and_return(double("response", code: 200))

subject = described_class.new
subject.request(:get, "test", nil, headers: { "User-Agent" => "dnsimple-custom-integration" })
subject = described_class.new(user_agent: "customAgent")
subject.request(:get, "test", nil)
end
end

Expand Down

0 comments on commit 50129d3

Please sign in to comment.