Skip to content

Commit cf17bf3

Browse files
committed
fix(response): handle deserialization of empty responses
1 parent d2946d1 commit cf17bf3

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

test/unit/test_watson_service.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,16 @@ def test_http_head():
191191
assert len(responses.calls) == 1
192192
assert response.headers is not None
193193
assert response.headers == expectedHeaders
194+
195+
@responses.activate
196+
def test_response_with_no_body():
197+
service = AnyServiceV1('2018-11-20', username='username', password='password')
198+
responses.add(responses.GET,
199+
service.default_url,
200+
status=200,
201+
body=None)
202+
203+
response = service.any_service_call()
204+
assert response is not None
205+
assert len(responses.calls) == 1
206+
assert response.get_result() is None

watson_developer_cloud/watson_service.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,11 @@ def request(self, method, url, accept_json=False, headers=None,
467467
# There is no body content for a HEAD request or a 204 response
468468
return DetailedResponse(None, response.headers, response.status_code) if self.detailed_response else None
469469
if accept_json:
470-
response_json = response.json()
470+
try:
471+
response_json = response.json()
472+
except:
473+
# deserialization fails because there is no text
474+
return DetailedResponse(None, response.headers, response.status_code) if self.detailed_response else None
471475
if 'status' in response_json and response_json['status'] \
472476
== 'ERROR':
473477
status_code = 400

0 commit comments

Comments
 (0)