From 0a3ebace45abf0a1bf58efdbd2eebb0131b6f3aa Mon Sep 17 00:00:00 2001 From: bc-zachary Date: Wed, 9 Feb 2022 13:06:23 -0800 Subject: [PATCH] APPEX-243: adds case-insensitive handling for HTTP header titles --- .rubocop.yml | 2 +- CHANGELOG.md | 1 + lib/bigcommerce/exception.rb | 17 +++++++---------- spec/bigcommerce/unit/exception_spec.rb | 16 +++++++++++++++- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 0f3a188..b6204bf 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -6,7 +6,7 @@ AllCops: - vendor/**/* - examples/**/* -Metrics/LineLength: +Layout/LineLength: Max: 120 Metrics/AbcSize: diff --git a/CHANGELOG.md b/CHANGELOG.md index 1da5962..16e03d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/bigcommerce/exception.rb b/lib/bigcommerce/exception.rb index 2757ceb..98056e4 100644 --- a/lib/bigcommerce/exception.rb +++ b/lib/bigcommerce/exception.rb @@ -1,6 +1,7 @@ module Bigcommerce class HttpError < StandardError attr_accessor :response_headers + def initialize(headers) @response_headers = headers end @@ -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 diff --git a/spec/bigcommerce/unit/exception_spec.rb b/spec/bigcommerce/unit/exception_spec.rb index c428555..88c1087 100644 --- a/spec/bigcommerce/unit/exception_spec.rb +++ b/spec/bigcommerce/unit/exception_spec.rb @@ -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 @@ -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 }