Skip to content

Commit

Permalink
Fix JSONDecodeError when processing empty server responses (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
allisson committed Apr 24, 2018
1 parent 2b7a0b8 commit ffb53d0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ htmlcov/
.tox/
.coverage
.coverage.*
.cache
.pytest_cache
nosetests.xml
coverage.xml
*,cover
Expand Down
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
---------

0.5.2
~~~~~

* Fix JSONDecodeError when processing empty server responses (thanks @zmbbb).

0.5.1
~~~~~

Expand Down
10 changes: 7 additions & 3 deletions simple_rest_client/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import async_timeout
from json_encoder import json

from .decorators import handle_request_error, handle_async_request_error
from .decorators import handle_async_request_error, handle_request_error
from .models import Response

logger = logging.getLogger(__name__)
Expand All @@ -26,7 +26,9 @@ def make_request(session, request):
if 'text' in content_type:
body = client_response.text
elif 'json' in content_type:
body = json.loads(client_response.text)
body = client_response.text
if body:
body = json.loads(body)
else:
body = client_response.content

Expand Down Expand Up @@ -58,7 +60,9 @@ async def make_async_request(session, request):
if 'text' in content_type:
body = await client_response.text()
elif 'json' in content_type:
body = json.loads(await client_response.text())
body = await client_response.text()
if body:
body = json.loads(body)
else:
body = await client_response.read()

Expand Down
30 changes: 16 additions & 14 deletions tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,25 @@ def test_resource_actions(url, method, status, action, args, kwargs, reqres_reso
assert response.body == {'success': True}


@pytest.mark.parametrize('content_type,response_body', [
('application/json', {'success': True}),
('text/plain', '{"success": true}'),
('application/octet-stream', b'{"success": true}'),
@pytest.mark.parametrize('content_type,response_body,expected_response_body', [
('application/json', '{"success": true}', {'success': True}),
('application/json', '', ''),
('text/plain', '{"success": true}', '{"success": true}'),
('application/octet-stream', '{"success": true}', b'{"success": true}'),
])
@responses.activate
def test_resource_response_body(content_type, response_body, reqres_resource):
def test_resource_response_body(content_type, response_body, expected_response_body, reqres_resource):
url = 'https://reqres.in/api/users'
responses.add(
responses.GET,
url,
body=b'{"success": true}',
body=response_body,
status=200,
content_type=content_type
)

response = reqres_resource.list()
assert response.body == response_body
assert response.body == expected_response_body


@pytest.mark.asyncio
Expand All @@ -124,14 +125,15 @@ async def test_async_resource_actions(url, method, status, action, args, kwargs,


@pytest.mark.asyncio
@pytest.mark.parametrize('content_type,response_body', [
('application/json', {'success': True}),
('text/plain', '{"success": true}'),
('application/octet-stream', b'{"success": true}'),
@pytest.mark.parametrize('content_type,response_body,expected_response_body', [
('application/json', '{"success": true}', {'success': True}),
('application/json', '', ''),
('text/plain', '{"success": true}', '{"success": true}'),
('application/octet-stream', '{"success": true}', b'{"success": true}'),
])
async def test_asyncresource_response_body(content_type, response_body, reqres_async_resource):
async def test_asyncresource_response_body(content_type, response_body, expected_response_body, reqres_async_resource):
url = 'https://reqres.in/api/users'
with aioresponses() as mock_response:
mock_response.get(url, status=200, body=b'{"success": true}', headers={'Content-Type': content_type})
mock_response.get(url, status=200, body=response_body, headers={'Content-Type': content_type})
response = await reqres_async_resource.list()
assert response.body == response_body
assert response.body == expected_response_body

0 comments on commit ffb53d0

Please sign in to comment.