Skip to content
This repository has been archived by the owner on Apr 4, 2018. It is now read-only.

Commit

Permalink
Don't try and parse JSON when the response is empty
Browse files Browse the repository at this point in the history
When the response from a request is empty (eg. when requests are stubbed 
out in a test), attempting to parse the response as JSON raises an 
exception.

This changes the log_response method to return an 'UNKNOWN' status if 
the response is empty, consistent with behaviour when the response is 
JSON but doesn't contain a result key.
  • Loading branch information
JordanHatch committed Mar 21, 2014
1 parent be35fc5 commit 31f0f0d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rummageable.rb
Expand Up @@ -74,7 +74,7 @@ def log_request(method, url, payload = nil)

def log_response(method, url, call_time, response)
time = sprintf('%.03f', call_time)
result = JSON.parse(response).fetch('result', 'UNKNOWN')
result = response.length > 0 ? JSON.parse(response).fetch('result', 'UNKNOWN') : "UNKNOWN"
@logger.info("Rummageable response: #{method.upcase} #{url} - time: #{time}s, result: #{result}")
end

Expand Down
11 changes: 11 additions & 0 deletions test/add_test.rb
Expand Up @@ -104,4 +104,15 @@ def test_add_should_log_failures
index = Rummageable::Index.new(rummager_url, index_name, logger: logger)
index.add(one_document)
end

def test_should_return_unknown_status_for_blank_response
RestClient.expects(:send).returns("")
Rummageable::Index.any_instance.stubs(:sleep)

logger = stub('logger', debug: true, info: true)
logger.expects(:info).once.with(regexp_matches(/result: UNKNOWN/))

index = Rummageable::Index.new(rummager_url, index_name, logger: logger)
index.add(one_document)
end
end

0 comments on commit 31f0f0d

Please sign in to comment.