From 199fa50a9d1840e5b0d612972383b43c1616f941 Mon Sep 17 00:00:00 2001 From: Miroslav Batchkarov Date: Mon, 5 Jun 2017 16:24:23 +0100 Subject: [PATCH 1/3] also store json result in local request --- sanic/testing.py | 1 + tests/test_response.py | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/sanic/testing.py b/sanic/testing.py index 787106a6..6be79cd0 100644 --- a/sanic/testing.py +++ b/sanic/testing.py @@ -26,6 +26,7 @@ class SanicTestClient: session, method.lower())(url, *args, **kwargs) as response: try: response.text = await response.text() + response.json = await response.json() except UnicodeDecodeError as e: response.text = None response.body = await response.read() diff --git a/tests/test_response.py b/tests/test_response.py index ba87f68d..fb213b56 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -9,10 +9,12 @@ import pytest from random import choice from sanic import Sanic -from sanic.response import HTTPResponse, stream, StreamingHTTPResponse, file, file_stream +from sanic.response import HTTPResponse, stream, StreamingHTTPResponse, file, file_stream, json from sanic.testing import HOST, PORT from unittest.mock import MagicMock +JSON_DATA = {'ok': True} + def test_response_body_not_a_string(): @@ -34,6 +36,24 @@ async def sample_streaming_fn(response): response.write('bar') +@pytest.fixture +def json_app(): + app = Sanic('json') + + @app.route("/") + async def test(request): + return json(JSON_DATA) + + return app + + +def test_json_response(json_app): + from sanic.response import json_dumps + request, response = json_app.test_client.get('/') + assert response.status == 200 + assert response.text == json_dumps(JSON_DATA) + assert response.json == JSON_DATA + @pytest.fixture def streaming_app(): app = Sanic('streaming') From 3f22b644b6d4bfef961065854f0c419481277eba Mon Sep 17 00:00:00 2001 From: Miroslav Batchkarov Date: Wed, 7 Jun 2017 09:57:07 +0100 Subject: [PATCH 2/3] wrap call to json in try-except to make tests pass --- sanic/testing.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sanic/testing.py b/sanic/testing.py index 6be79cd0..09554e21 100644 --- a/sanic/testing.py +++ b/sanic/testing.py @@ -1,4 +1,5 @@ import traceback +from json import JSONDecodeError from sanic.log import log @@ -26,9 +27,14 @@ class SanicTestClient: session, method.lower())(url, *args, **kwargs) as response: try: response.text = await response.text() - response.json = await response.json() except UnicodeDecodeError as e: response.text = None + + try: + response.json = await response.json() + except (JSONDecodeError, UnicodeDecodeError): + response.json = None + response.body = await response.read() return response From ddd7145153d5a52e74f27a4d7f0750f72d84a17d Mon Sep 17 00:00:00 2001 From: Miroslav Batchkarov Date: Wed, 7 Jun 2017 10:03:27 +0100 Subject: [PATCH 3/3] check json is None if body is not JSON --- tests/test_blueprints.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index d9f5cd61..9ab387be 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -41,6 +41,7 @@ def test_bp_strict_slash(): request, response = app.test_client.get('/get') assert response.text == 'OK' + assert response.json == None request, response = app.test_client.get('/get/') assert response.status == 404