]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Make `content` argument required to `JSONResponse` (#1431)
authorAmin Alaee <mohammadamin.alaee@gmail.com>
Wed, 26 Jan 2022 16:11:33 +0000 (17:11 +0100)
committerGitHub <noreply@github.com>
Wed, 26 Jan 2022 16:11:33 +0000 (17:11 +0100)
Co-authored-by: Tom Christie <tom@tomchristie.com>
starlette/responses.py
tests/test_responses.py

index 26d730540cd90f4ba959d7ea19df5796ee92fe18..577d1bb0496e56907e3f70e29aa3876251f87d64 100644 (file)
@@ -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,
index e2337bdca1175814a97ef4c0d6a5b70b2f940d0c..38aea57614ad2a14fec4bd4e22904cadec6842d1 100644 (file)
@@ -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):