From: Amin Alaee Date: Wed, 26 Jan 2022 16:11:33 +0000 (+0100) Subject: Make `content` argument required to `JSONResponse` (#1431) X-Git-Tag: 0.19.0~57 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5a5a5c367f2bac0cb526d417adb59cd2174c3329;p=thirdparty%2Fstarlette.git Make `content` argument required to `JSONResponse` (#1431) Co-authored-by: Tom Christie --- diff --git a/starlette/responses.py b/starlette/responses.py index 26d73054..577d1bb0 100644 --- a/starlette/responses.py +++ b/starlette/responses.py @@ -174,6 +174,16 @@ class PlainTextResponse(Response): class JSONResponse(Response): media_type = "application/json" + def __init__( + self, + content: typing.Any, + status_code: int = 200, + headers: dict = None, + media_type: str = None, + background: BackgroundTask = None, + ) -> None: + super().__init__(content, status_code, headers, media_type, background) + def render(self, content: typing.Any) -> bytes: return json.dumps( content, diff --git a/tests/test_responses.py b/tests/test_responses.py index e2337bdc..38aea576 100644 --- a/tests/test_responses.py +++ b/tests/test_responses.py @@ -44,6 +44,7 @@ def test_json_none_response(test_client_factory): client = test_client_factory(app) response = client.get("/") assert response.json() is None + assert response.content == b"null" def test_redirect_response(test_client_factory): @@ -330,7 +331,9 @@ def test_empty_response(test_client_factory): app = Response() client: TestClient = test_client_factory(app) response = client.get("/") + assert response.content == b"" assert response.headers["content-length"] == "0" + assert "content-type" not in response.headers def test_empty_204_response(test_client_factory):