changed None to return empty string instead of null string
This commit is contained in:
parent
8b23b5d389
commit
2135294e2e
|
@ -239,7 +239,8 @@ def json(body, status=200, headers=None,
|
||||||
:param headers: Custom Headers.
|
:param headers: Custom Headers.
|
||||||
:param kwargs: Remaining arguments that are passed to the json encoder.
|
: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)
|
status=status, content_type=content_type)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -109,12 +109,13 @@ def test_empty_json():
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
async def handler(request):
|
async def handler(request):
|
||||||
assert request.json == None
|
assert request.json is None
|
||||||
return json(request.json)
|
return json(request.json)
|
||||||
|
|
||||||
request, response = app.test_client.get('/')
|
request, response = app.test_client.get('/')
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
assert response.text == 'null'
|
assert response.text == ''
|
||||||
|
|
||||||
|
|
||||||
def test_invalid_json():
|
def test_invalid_json():
|
||||||
app = Sanic('test_json')
|
app = Sanic('test_json')
|
||||||
|
|
|
@ -63,6 +63,10 @@ def json_app():
|
||||||
async def test(request):
|
async def test(request):
|
||||||
return json(JSON_DATA)
|
return json(JSON_DATA)
|
||||||
|
|
||||||
|
@app.delete("/")
|
||||||
|
async def test_delete(request):
|
||||||
|
return json(None, status=204)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
@ -73,6 +77,14 @@ def test_json_response(json_app):
|
||||||
assert response.text == json_dumps(JSON_DATA)
|
assert response.text == json_dumps(JSON_DATA)
|
||||||
assert response.json == 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
|
@pytest.fixture
|
||||||
def streaming_app():
|
def streaming_app():
|
||||||
app = Sanic('streaming')
|
app = Sanic('streaming')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user