From e6cbba08c3abdb5ac0eaf3632efa5af0be175109 Mon Sep 17 00:00:00 2001 From: Hippolyte HENRY Date: Mon, 25 Jan 2021 15:12:59 +0100 Subject: [PATCH] Properly handle string response code --- lib/dogapi/common.rb | 4 ++-- spec/unit/common_spec.rb | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/dogapi/common.rb b/lib/dogapi/common.rb index 55203959..f9c9629f 100644 --- a/lib/dogapi/common.rb +++ b/lib/dogapi/common.rb @@ -183,7 +183,7 @@ def should_set_api_and_app_keys_in_params?(url) end def handle_response(resp) - if resp.code == 204 || resp.body == '' || resp.body == 'null' || resp.body.nil? + if resp.code.to_i == 204 || resp.body == '' || resp.body == 'null' || resp.body.nil? return resp.code, {} end begin @@ -198,7 +198,7 @@ def handle_response(resp) def handle_redirect(conn, req, resp, retries=10) req.uri = URI.parse(resp.header['location']) new_response = conn.request(req) - if retries > 1 && new_response.code / 100 == 3 + if retries > 1 && new_response.code.to_i / 100 == 3 new_response = handle_redirect(conn, req, new_response, retries - 1) end new_response diff --git a/spec/unit/common_spec.rb b/spec/unit/common_spec.rb index 89cd94d4..2afc8027 100644 --- a/spec/unit/common_spec.rb +++ b/spec/unit/common_spec.rb @@ -182,21 +182,21 @@ def initialize(code, body, content_type = 'application/json') context 'when receiving a correct reponse with valid json' do it 'parses it and return code and parsed body' do dog = dogapi_service - resp = FakeResponse.new 202, '{"test2": "test3"}' - expect(dog.handle_response(resp)).to eq([202, { 'test2' => 'test3' }]) + resp = FakeResponse.new '202', '{"test2": "test3"}' + expect(dog.handle_response(resp)).to eq(['202', { 'test2' => 'test3' }]) end end context 'when receiving a response with invalid json' do it 'raises an error' do dog = dogapi_service - resp = FakeResponse.new 202, "{'test2': }" + resp = FakeResponse.new '202', "{'test2': }" expect { dog.handle_response(resp) }.to raise_error(RuntimeError, "Invalid JSON Response: {'test2': }") end end context 'when receiving a non json response' do it 'raises an error indicating the response Content-Type' do dog = dogapi_service - resp = FakeResponse.new 202, '

403 Forbidden

', 'text/html' + resp = FakeResponse.new '202', '

403 Forbidden

', 'text/html' msg = 'Response Content-Type is not application/json but is text/html:

403 Forbidden

' expect { dog.handle_response(resp) }.to raise_error(RuntimeError, msg) end @@ -204,12 +204,12 @@ def initialize(code, body, content_type = 'application/json') context 'when receiving a bad response' do it 'returns the error code and an empty body' do dog = dogapi_service - resp = FakeResponse.new 204, '' - expect(dog.handle_response(resp)).to eq([204, {}]) - resp = FakeResponse.new 202, nil - expect(dog.handle_response(resp)).to eq([202, {}]) - resp = FakeResponse.new 202, 'null' - expect(dog.handle_response(resp)).to eq([202, {}]) + resp = FakeResponse.new '204', '' + expect(dog.handle_response(resp)).to eq(['204', {}]) + resp = FakeResponse.new '202', nil + expect(dog.handle_response(resp)).to eq(['202', {}]) + resp = FakeResponse.new '202', 'null' + expect(dog.handle_response(resp)).to eq(['202', {}]) end end end