Skip to content

Commit

Permalink
Merge pull request #600 from watson-developer-cloud/fix-no-text
Browse files Browse the repository at this point in the history
handle deserialization of empty responses
  • Loading branch information
ehdsouza authored Nov 20, 2018
2 parents d2946d1 + cf17bf3 commit 6e6d4aa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
13 changes: 13 additions & 0 deletions test/unit/test_watson_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,16 @@ def test_http_head():
assert len(responses.calls) == 1
assert response.headers is not None
assert response.headers == expectedHeaders

@responses.activate
def test_response_with_no_body():
service = AnyServiceV1('2018-11-20', username='username', password='password')
responses.add(responses.GET,
service.default_url,
status=200,
body=None)

response = service.any_service_call()
assert response is not None
assert len(responses.calls) == 1
assert response.get_result() is None
6 changes: 5 additions & 1 deletion watson_developer_cloud/watson_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,11 @@ def request(self, method, url, accept_json=False, headers=None,
# There is no body content for a HEAD request or a 204 response
return DetailedResponse(None, response.headers, response.status_code) if self.detailed_response else None
if accept_json:
response_json = response.json()
try:
response_json = response.json()
except:
# deserialization fails because there is no text
return DetailedResponse(None, response.headers, response.status_code) if self.detailed_response else None
if 'status' in response_json and response_json['status'] \
== 'ERROR':
status_code = 400
Expand Down

0 comments on commit 6e6d4aa

Please sign in to comment.