Skip to content

Commit

Permalink
Set User-Agent header to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaswilliamgomes committed Aug 9, 2023
1 parent 59a04ce commit f5bb9c4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/incognia_api/client.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "time"
require "pry"

module Incognia
class Client
Expand Down Expand Up @@ -26,6 +27,12 @@ def initialize(client_id:, client_secret:, host:)
def request(method, endpoint = nil, data = nil, headers = {})
json_data = JSON.generate(data) if data

user_agent_header = "incognia-ruby/#{Incognia::VERSION} " +
"({#{Util::OS_HOST}}) {#{Util::OS_ARCH}} " +
"Ruby/#{Util::LANGUAGE_VERSION}"

headers.merge!('User-Agent' => user_agent_header)

connection.send(method, endpoint, json_data, headers) do |r|
r.headers[Faraday::Request::Authorization::KEY] ||= Faraday::Request
.lookup_middleware(:authorization)
Expand Down
4 changes: 4 additions & 0 deletions lib/incognia_api/util.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module Incognia
module Util
OS_HOST = RbConfig::CONFIG['host']
OS_ARCH = RbConfig::CONFIG['arch']
LANGUAGE_VERSION = RbConfig::CONFIG['ruby_version']

def self.symbolize_names(object)
case object
when Hash
Expand Down
26 changes: 26 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ module Incognia
expect(stub).to have_been_made.once
end

it "injects User-Agent header" do
user_agent_header = { 'User-Agent' => "incognia-ruby/#{Incognia::VERSION} " +
"({#{Util::OS_HOST}}) " +
"{#{Util::OS_ARCH}} " +
"Ruby/#{Util::LANGUAGE_VERSION}" }

stub_token_request
stub = stub_request(:post, test_endpoint)
.with(
body: sample_json,
headers: user_agent_header
).to_return(
status: 200,
body: sample_json,
headers: { 'Content-Type' => 'application/json' }
)

subject.request(
:post,
"v2/endpoint",
{ foo: :bar }
)

expect(stub).to have_been_made.once
end

context "when passing an Authorization header" do
it "overrides default header" do
stub = stub_request(:post, test_endpoint).
Expand Down

0 comments on commit f5bb9c4

Please sign in to comment.