Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle string response code #254

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/dogapi/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
20 changes: 10 additions & 10 deletions spec/unit/common_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,34 +182,34 @@ 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, '<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, ''
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
Expand Down