changed None to return empty string instead of null string

This commit is contained in:
Arnulfo Solis 2018-02-01 11:52:55 +01:00
parent 8b23b5d389
commit 2135294e2e
3 changed files with 17 additions and 3 deletions

View File

@ -239,7 +239,8 @@ def json(body, status=200, headers=None,
:param headers: Custom Headers.
:param kwargs: Remaining arguments that are passed to the json encoder.
"""
return HTTPResponse(dumps(body, **kwargs), headers=headers,
_body = dumps(body, **kwargs) if body else None
return HTTPResponse(_body, headers=headers,
status=status, content_type=content_type)

View File

@ -109,12 +109,13 @@ def test_empty_json():
@app.route('/')
async def handler(request):
assert request.json == None
assert request.json is None
return json(request.json)
request, response = app.test_client.get('/')
assert response.status == 200
assert response.text == 'null'
assert response.text == ''
def test_invalid_json():
app = Sanic('test_json')

View File

@ -63,6 +63,10 @@ def json_app():
async def test(request):
return json(JSON_DATA)
@app.delete("/")
async def test_delete(request):
return json(None, status=204)
return app
@ -73,6 +77,14 @@ def test_json_response(json_app):
assert response.text == json_dumps(JSON_DATA)
assert response.json == JSON_DATA
def test_no_content(json_app):
request, response = json_app.test_client.delete('/')
assert response.status == 204
assert response.text == ''
assert response.headers['Content-Length'] == '0'
@pytest.fixture
def streaming_app():
app = Sanic('streaming')