media_type: str = None,
background: BackgroundTask = None,
) -> None:
- if content is None:
- self.body = b""
- else:
- self.body = self.render(content)
+ self.body = self.render(content)
self.status_code = status_code
if media_type is not None:
self.media_type = media_type
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)
Response,
StreamingResponse,
UJSONResponse,
+ JSONResponse,
)
from starlette.testclient import TestClient
assert response.json() == {"hello": "world"}
+def test_json_none_response():
+ async def app(scope, receive, send):
+ response = JSONResponse(None)
+ await response(scope, receive, send)
+
+ client = TestClient(app)
+ response = client.get("/")
+ assert response.json() is None
+
+
def test_redirect_response():
async def app(scope, receive, send):
if scope["path"] == "/":