Add ability to return Falsey but not-None from handlers (#2236)
This commit is contained in:
parent
ef4f058a6c
commit
a937e08ef0
|
@ -839,7 +839,7 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
|
|||
if isawaitable(response):
|
||||
response = await response
|
||||
|
||||
if response:
|
||||
if response is not None:
|
||||
response = await request.respond(response)
|
||||
elif not hasattr(handler, "is_websocket"):
|
||||
response = request.stream.response # type: ignore
|
||||
|
|
|
@ -5,7 +5,7 @@ from itertools import count
|
|||
|
||||
from sanic.exceptions import NotFound
|
||||
from sanic.request import Request
|
||||
from sanic.response import HTTPResponse, text
|
||||
from sanic.response import HTTPResponse, json, text
|
||||
|
||||
|
||||
# ------------------------------------------------------------ #
|
||||
|
@ -283,3 +283,17 @@ def test_request_middleware_executes_once(app):
|
|||
|
||||
request, response = app.test_client.get("/")
|
||||
assert next(i) == 3
|
||||
|
||||
|
||||
def test_middleware_added_response(app):
|
||||
@app.on_response
|
||||
def display(_, response):
|
||||
response["foo"] = "bar"
|
||||
return json(response)
|
||||
|
||||
@app.get("/")
|
||||
async def handler(request):
|
||||
return {}
|
||||
|
||||
_, response = app.test_client.get("/")
|
||||
assert response.json["foo"] == "bar"
|
||||
|
|
Loading…
Reference in New Issue
Block a user