Skip to content

Commit

Permalink
APPEX-243: adds case-insensitive handling for HTTP header titles
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-zachary committed Feb 9, 2022
1 parent d59d0da commit 333ae22
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
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:
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.

* [#000](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
14 changes: 3 additions & 11 deletions lib/bigcommerce/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,9 @@ 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
end
response_headers = env.response_headers
x_retry_after = response_headers['x-retry-after'] || response_headers['X-Retry-After']
x_retry_after && response_headers[:retry_after] = x_retry_after.to_i
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

0 comments on commit 333ae22

Please sign in to comment.