Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APPEX-243: adds case-insensitive handling for HTTP header titles #167

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ AllCops:
- vendor/**/*
- examples/**/*

Metrics/LineLength:
Layout/LineLength:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix warning: .rubocop.yml: Metrics/LineLength has the wrong namespace - should be Layout when running rubocop.

Max: 120

Metrics/AbcSize:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Next Release
Your contribution here.

* [#167](https://github.com/bigcommerce/bigcommerce-api-ruby/pull/000): Changes handling of HTTP response headers to handle lowercased titles. - [@bc-zachary](https://github.com/bc-zachary).
* [#000](https://github.com/bigcommerce/bigcommerce-api-ruby/pull/000): Brief description here. - [@username](https://github.com/username).

## 1.0.1
Expand Down
17 changes: 7 additions & 10 deletions lib/bigcommerce/exception.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Bigcommerce
class HttpError < StandardError
attr_accessor :response_headers

def initialize(headers)
@response_headers = headers
end
Expand Down Expand Up @@ -41,17 +42,13 @@ module HttpErrors

def throw_http_exception!(code, env)
return unless ERRORS.keys.include? code
response_headers = {}
unless env.body.empty?
response_headers = begin
JSON.parse(env.body, symbolize_names: true)
rescue StandardError
{}
end
end
unless env[:response_headers] && env[:response_headers]['X-Retry-After'].nil?
response_headers[:retry_after] = env[:response_headers]['X-Retry-After'].to_i

response_headers = Faraday::Utils::Headers.new(env.response_headers)

unless response_headers['x-retry-after'].nil?
response_headers[:retry_after] = response_headers['x-retry-after'].to_i
end

raise ERRORS[code].new(response_headers), env.body
end
end
Expand Down
16 changes: 15 additions & 1 deletion spec/bigcommerce/unit/exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

before do
allow(env).to receive(:body) { body }
allow(env).to receive(:[]) { headers }
allow(env).to receive(:response_headers) { headers }
end

it '::ERRORS is not nil' do
Expand Down Expand Up @@ -43,6 +43,20 @@
end
end

context 'when have a body and lowercase response headers' do
let(:body) { JSON.generate({ time: '1426184190' }) }
let(:headers) { { 'x-retry-after' => 1 } }
let(:code) { 429 }

it 'should parse out a retry-after header if present' do
begin
dummy_class.throw_http_exception!(code, env)
rescue Bigcommerce::TooManyRequests => e
expect(e.response_headers[:retry_after]).to eq 1
end
end
end

context 'when we get a string body' do
let(:body) { 'Unauthorized' }
let(:code) { 401 }
Expand Down