From af678010628cd76a57e7a53e114f25d5c00e931a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9stor=20P=C3=A9rez?= Date: Sun, 9 Apr 2023 21:23:21 +0200 Subject: [PATCH] Fix JSONResponse default content type (#2737) --- sanic/response/types.py | 2 +- tests/test_response_json.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/sanic/response/types.py b/sanic/response/types.py index 3f93855d..d3c8cdb6 100644 --- a/sanic/response/types.py +++ b/sanic/response/types.py @@ -345,7 +345,7 @@ class JSONResponse(HTTPResponse): body: Optional[Any] = None, status: int = 200, headers: Optional[Union[Header, Dict[str, str]]] = None, - content_type: Optional[str] = None, + content_type: str = "application/json", dumps: Optional[Callable[..., str]] = None, **kwargs: Any, ): diff --git a/tests/test_response_json.py b/tests/test_response_json.py index c89dba42..aa1c61dd 100644 --- a/tests/test_response_json.py +++ b/tests/test_response_json.py @@ -213,3 +213,12 @@ def test_pop_list(json_app: Sanic): _, resp = json_app.test_client.get("/json-pop") assert resp.body == json_dumps(["b"]).encode() + + +def test_json_response_class_sets_proper_content_type(json_app: Sanic): + @json_app.get("/json-class") + async def handler(request: Request): + return JSONResponse(JSON_BODY) + + _, resp = json_app.test_client.get("/json-class") + assert resp.headers["content-type"] == "application/json"