Skip to content

Commit 0a3ebac

Browse files
committed
APPEX-243: adds case-insensitive handling for HTTP header titles
1 parent d59d0da commit 0a3ebac

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ AllCops:
66
- vendor/**/*
77
- examples/**/*
88

9-
Metrics/LineLength:
9+
Layout/LineLength:
1010
Max: 120
1111

1212
Metrics/AbcSize:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Next Release
22
Your contribution here.
33

4+
* [#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).
45
* [#000](https://github.com/bigcommerce/bigcommerce-api-ruby/pull/000): Brief description here. - [@username](https://github.com/username).
56

67
## 1.0.1

lib/bigcommerce/exception.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Bigcommerce
22
class HttpError < StandardError
33
attr_accessor :response_headers
4+
45
def initialize(headers)
56
@response_headers = headers
67
end
@@ -41,17 +42,13 @@ module HttpErrors
4142

4243
def throw_http_exception!(code, env)
4344
return unless ERRORS.keys.include? code
44-
response_headers = {}
45-
unless env.body.empty?
46-
response_headers = begin
47-
JSON.parse(env.body, symbolize_names: true)
48-
rescue StandardError
49-
{}
50-
end
51-
end
52-
unless env[:response_headers] && env[:response_headers]['X-Retry-After'].nil?
53-
response_headers[:retry_after] = env[:response_headers]['X-Retry-After'].to_i
45+
46+
response_headers = Faraday::Utils::Headers.new(env.response_headers)
47+
48+
unless response_headers['x-retry-after'].nil?
49+
response_headers[:retry_after] = response_headers['x-retry-after'].to_i
5450
end
51+
5552
raise ERRORS[code].new(response_headers), env.body
5653
end
5754
end

spec/bigcommerce/unit/exception_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
before do
99
allow(env).to receive(:body) { body }
10-
allow(env).to receive(:[]) { headers }
10+
allow(env).to receive(:response_headers) { headers }
1111
end
1212

1313
it '::ERRORS is not nil' do
@@ -43,6 +43,20 @@
4343
end
4444
end
4545

46+
context 'when have a body and lowercase response headers' do
47+
let(:body) { JSON.generate({ time: '1426184190' }) }
48+
let(:headers) { { 'x-retry-after' => 1 } }
49+
let(:code) { 429 }
50+
51+
it 'should parse out a retry-after header if present' do
52+
begin
53+
dummy_class.throw_http_exception!(code, env)
54+
rescue Bigcommerce::TooManyRequests => e
55+
expect(e.response_headers[:retry_after]).to eq 1
56+
end
57+
end
58+
end
59+
4660
context 'when we get a string body' do
4761
let(:body) { 'Unauthorized' }
4862
let(:code) { 401 }

0 commit comments

Comments
 (0)