Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make content argument required to JSONResponse #1431

Merged
merged 17 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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