Skip to content

Commit

Permalink
Add twurl version to user agent header (#126)
Browse files Browse the repository at this point in the history
* Add twurl version to user agent header

* Add test
  • Loading branch information
smaeda-ks committed Dec 8, 2019
1 parent 6fb3c08 commit d037cf6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/twurl/oauth_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,15 @@ def perform_request_from_options(options, &block)
end

request.oauth!(consumer.http, consumer, access_token)
request['user-agent'] = user_agent
consumer.http.request(request, &block)
end

def user_agent
"twurl version: #{Version} " \
"platform: #{RUBY_ENGINE} #{RUBY_VERSION} (#{RUBY_PLATFORM})"
end

def exchange_credentials_for_access_token
response = begin
consumer.token_request(:post, consumer.access_token_path, nil, {}, client_auth_parameters)
Expand Down
14 changes: 14 additions & 0 deletions test/oauth_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ def test_content_type_is_set_to_form_encoded_if_not_set_and_data_in_options

client.perform_request_from_options(options)
end

def test_user_agent_request_header_is_set
client = Twurl::OAuthClient.test_exemplar
expected_ua_string = "twurl version: #{Twurl::Version} platform: #{RUBY_ENGINE} #{RUBY_VERSION} (#{RUBY_PLATFORM})"

mock(client.consumer.http).request(
satisfy { |req|
req.is_a?(Net::HTTP::Get) &&
req['user-agent'] == expected_ua_string
}
)

client.perform_request_from_options(options)
end
end

class Twurl::OAuthClient::CredentialsForAccessTokenExchangeTest < Twurl::OAuthClient::AbstractOAuthClientTest
Expand Down

0 comments on commit d037cf6

Please sign in to comment.