Skip to content

Commit

Permalink
Make content argument required to JSONResponse (#1431)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom Christie <[email protected]>
  • Loading branch information
aminalaee and tomchristie authored Jan 26, 2022
1 parent 79812bb commit 5a5a5c3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions starlette/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async def app(scope, receive, send):
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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 5a5a5c3

Please sign in to comment.