Skip to content

Commit

Permalink
Properly handle string response code
Browse files Browse the repository at this point in the history
  • Loading branch information
zippolyte committed Jan 25, 2021
1 parent a91f105 commit ffd85a1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/dogapi/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ 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?
return resp.code, {}
if resp.code.to_i == 204 || resp.body == '' || resp.body == 'null' || resp.body.nil?
return resp.code.to_i, {}
end
begin
return resp.code, MultiJson.load(resp.body)
return resp.code.to_i, MultiJson.load(resp.body)
rescue
is_json = resp.content_type == 'application/json'
raise "Response Content-Type is not application/json but is #{resp.content_type}: " + resp.body unless is_json
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions spec/unit/common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,33 +182,33 @@ 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"}'
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, '<html><body><h1>403 Forbidden</h1>', 'text/html'
resp = FakeResponse.new '202', '<html><body><h1>403 Forbidden</h1>', 'text/html'
msg = 'Response Content-Type is not application/json but is text/html: <html><body><h1>403 Forbidden</h1>'
expect { dog.handle_response(resp) }.to raise_error(RuntimeError, msg)
end
end
context 'when receiving a bad response' do
it 'returns the error code and an empty body' do
dog = dogapi_service
resp = FakeResponse.new 204, ''
resp = FakeResponse.new '204', ''
expect(dog.handle_response(resp)).to eq([204, {}])
resp = FakeResponse.new 202, nil
resp = FakeResponse.new '202', nil
expect(dog.handle_response(resp)).to eq([202, {}])
resp = FakeResponse.new 202, 'null'
resp = FakeResponse.new '202', 'null'
expect(dog.handle_response(resp)).to eq([202, {}])
end
end
Expand Down

0 comments on commit ffd85a1

Please sign in to comment.