Skip to content

Commit

Permalink
fix: fix the headers issue (#671)
Browse files Browse the repository at this point in the history
* fix: fix the headers issue

* chore: fix linting

* chore: fix lint
  • Loading branch information
kridai authored Aug 8, 2023
1 parent ad3dc29 commit 9bd7e72
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.7.2
7 changes: 2 additions & 5 deletions lib/twilio-ruby/base/client_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ def initialize(username = nil, password = nil, account_sid = nil, region = nil,
##
# Makes a request to the Twilio API using the configured http client
# Authentication information is automatically added if none is provided
# rubocop:disable Lint/ShadowedArgument
def request(host, port, method, uri, params = {}, data = {}, headers = {}, auth = nil, timeout = nil) # rubocop:disable Metrics/MethodLength
auth ||= @auth
headers = generate_headers(method)
headers = generate_headers(method, headers)
uri = build_uri(uri)

if @logger
Expand Down Expand Up @@ -68,7 +67,6 @@ def request(host, port, method, uri, params = {}, data = {}, headers = {}, auth

response
end
# rubocop:enable Lint/ShadowedArgument

##
# Build the final request uri
Expand Down Expand Up @@ -105,9 +103,8 @@ def validate_ssl_certificate
raise RestError.new 'Unexpected response from certificate endpoint', response
end

def generate_headers(method)
def generate_headers(method, headers)
ruby_config = RbConfig::CONFIG
headers = {}
headers['User-Agent'] =
"twilio-ruby/#{Twilio::VERSION} (#{ruby_config['host_os']} #{ruby_config['host_cpu']}) Ruby/#{RUBY_VERSION}"
headers['Accept-Charset'] = 'utf-8'
Expand Down
8 changes: 7 additions & 1 deletion spec/rest/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def initialize(client)

it 'use default user agent format' do
@client.request('host', 'port', 'GET', 'https://api.twilio.com')
expect(@client.http_client.last_request.headers['User-Agent']).to match %r{^twilio-ruby/[0-9.]+(-rc\.[0-9]+)?\s\([\w-]+\s\w+\)\sRuby/[^\s]+$}
expect(@client.http_client.last_request.headers['User-Agent']).to match %r{^twilio-ruby/[0-9.]+(-rc\.[0-9]+)?\s\([\w.-]+\s\w+\)\sRuby/[^\s]+$}
end

it 'add user agent extensions' do
Expand All @@ -231,5 +231,11 @@ def initialize(client)
actual_extensions = @client.http_client.last_request.headers['User-Agent'].split(/ /).last(extensions.size)
expect(actual_extensions).to match_array(extensions)
end

it 'preserve headers passed' do
headers = Twilio::Values.of({ 'X-Twilio-Webhook-Enabled' => 'true' })
@client.request('host', 'port', 'GET', 'https://api.twilio.com', nil, nil, headers)
expect(@client.http_client.last_request.headers['X-Twilio-Webhook-Enabled']).to eq('true')
end
end
end

0 comments on commit 9bd7e72

Please sign in to comment.