Merge pull request #773 from mbatchkarov/await-json-too
Testing: store JSON response in local request
This commit is contained in:
commit
4fdf340d04
|
@ -1,4 +1,5 @@
|
||||||
import traceback
|
import traceback
|
||||||
|
from json import JSONDecodeError
|
||||||
|
|
||||||
from sanic.log import log
|
from sanic.log import log
|
||||||
|
|
||||||
|
@ -28,6 +29,12 @@ class SanicTestClient:
|
||||||
response.text = await response.text()
|
response.text = await response.text()
|
||||||
except UnicodeDecodeError as e:
|
except UnicodeDecodeError as e:
|
||||||
response.text = None
|
response.text = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
response.json = await response.json()
|
||||||
|
except (JSONDecodeError, UnicodeDecodeError):
|
||||||
|
response.json = None
|
||||||
|
|
||||||
response.body = await response.read()
|
response.body = await response.read()
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,7 @@ def test_bp_strict_slash():
|
||||||
|
|
||||||
request, response = app.test_client.get('/get')
|
request, response = app.test_client.get('/get')
|
||||||
assert response.text == 'OK'
|
assert response.text == 'OK'
|
||||||
|
assert response.json == None
|
||||||
|
|
||||||
request, response = app.test_client.get('/get/')
|
request, response = app.test_client.get('/get/')
|
||||||
assert response.status == 404
|
assert response.status == 404
|
||||||
|
|
|
@ -9,10 +9,12 @@ import pytest
|
||||||
from random import choice
|
from random import choice
|
||||||
|
|
||||||
from sanic import Sanic
|
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 sanic.testing import HOST, PORT
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
JSON_DATA = {'ok': True}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_response_body_not_a_string():
|
def test_response_body_not_a_string():
|
||||||
|
@ -34,6 +36,24 @@ async def sample_streaming_fn(response):
|
||||||
response.write('bar')
|
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
|
@pytest.fixture
|
||||||
def streaming_app():
|
def streaming_app():
|
||||||
app = Sanic('streaming')
|
app = Sanic('streaming')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user