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 2 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
23 changes: 17 additions & 6 deletions starlette/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,31 @@ def guess_type(
class Response:
media_type = None
charset = "utf-8"
default_status_code = 200
default_response = b""
aminalaee marked this conversation as resolved.
Show resolved Hide resolved

def __init__(
self,
content: typing.Any = None,
status_code: int = 200,
status_code: int = None,
headers: dict = None,
media_type: str = None,
background: BackgroundTask = None,
) -> None:
self.status_code = status_code
if media_type is not None:
self.media_type = media_type
self.background = background
self.body = self.render(content)

if content is None:
self.body = self.default_response
self.status_code = status_code
aminalaee marked this conversation as resolved.
Show resolved Hide resolved
else:
self.body = self.render(content)
self.status_code = status_code or self.default_status_code
aminalaee marked this conversation as resolved.
Show resolved Hide resolved

self.init_headers(headers)

def render(self, content: typing.Any) -> bytes:
if content is None:
return b""
if isinstance(content, bytes):
return content
return content.encode(self.charset)
Expand All @@ -74,7 +80,11 @@ def init_headers(self, headers: typing.Mapping[str, str] = None) -> None:
if (
body is not None
and populate_content_length
and not (self.status_code < 200 or self.status_code in (204, 304))
and not (
self.status_code
and self.status_code < 200
or self.status_code in (204, 304)
)
aminalaee marked this conversation as resolved.
Show resolved Hide resolved
):
content_length = str(len(body))
raw_headers.append((b"content-length", content_length.encode("latin-1")))
Expand Down Expand Up @@ -173,6 +183,7 @@ class PlainTextResponse(Response):

class JSONResponse(Response):
media_type = "application/json"
default_response = b"{}"
aminalaee marked this conversation as resolved.
Show resolved Hide resolved

def render(self, content: typing.Any) -> bytes:
return json.dumps(
Expand Down
6 changes: 3 additions & 3 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ async def app(scope, receive, send):

def test_json_none_response(test_client_factory):
async def app(scope, receive, send):
response = JSONResponse(None)
response = JSONResponse(status_code=200)
aminalaee marked this conversation as resolved.
Show resolved Hide resolved
await response(scope, receive, send)

client = test_client_factory(app)
response = client.get("/")
assert response.json() is None
assert response.json() == {}
aminalaee marked this conversation as resolved.
Show resolved Hide resolved


def test_redirect_response(test_client_factory):
Expand Down Expand Up @@ -327,7 +327,7 @@ def test_head_method(test_client_factory):


def test_empty_response(test_client_factory):
app = Response()
app = Response(status_code=200)
client: TestClient = test_client_factory(app)
response = client.get("/")
assert response.headers["content-length"] == "0"
aminalaee marked this conversation as resolved.
Show resolved Hide resolved
Expand Down