Skip to content

Commit

Permalink
Add test for empty response with 204 status
Browse files Browse the repository at this point in the history
  • Loading branch information
dolamroth committed Sep 17, 2023
1 parent 17e11c8 commit 2ad9e3e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion starlette_web/common/http/base_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async def dispatch(self) -> None:
if settings.REMOVE_BODY_FROM_RESPONSE_WITH_NO_BODY:
response.body = b""
elif response.body:
response.headers.append("content-length", str(len(response.body)))
response.headers.update({"content-length": str(len(response.body))})

await response(self.scope, self.receive, self.send)

Expand Down
5 changes: 5 additions & 0 deletions starlette_web/tests/core/test_responses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def test_empty_response(client):
# This test covers ability of starlette-web to circumvent design of uvicorn library,
# which forbids sending any request body for 204, 304 responses
response = client.get("/empty/")
assert response.content == b"null"
2 changes: 2 additions & 0 deletions starlette_web/tests/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
FinitePeriodicTaskWebsocketTestEndpoint,
InfinitePeriodicTaskWebsocketTestEndpoint,
ChatWebsocketTestEndpoint,
EmptyResponseAPIView,
)


Expand All @@ -24,6 +25,7 @@
Mount("/static", app=StaticFiles(directory=settings.STATIC["ROOT_DIR"]), name="static"),
Mount("/media", app=StaticFiles(directory=settings.MEDIA["ROOT_DIR"]), name="media"),
Route("/health_check/", HealthCheckAPIView),
Route("/empty/", EmptyResponseAPIView, include_in_schema=False),
WebSocketRoute("/ws/test_websocket_base", BaseWebsocketTestEndpoint),
WebSocketRoute("/ws/test_websocket_cancel", CancellationWebsocketTestEndpoint),
WebSocketRoute("/ws/test_websocket_auth", AuthenticationWebsocketTestEndpoint),
Expand Down
2 changes: 1 addition & 1 deletion starlette_web/tests/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# flake8: noqa

from starlette_web.tests.views.http import HealthCheckAPIView
from starlette_web.tests.views.http import HealthCheckAPIView, EmptyResponseAPIView
from starlette_web.tests.views.websocket import (
BaseWebsocketTestEndpoint,
CancellationWebsocketTestEndpoint,
Expand Down
7 changes: 7 additions & 0 deletions starlette_web/tests/views/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,10 @@ async def get(self, *_):
data=response_data,
status_code=result_status,
)


class EmptyResponseAPIView(BaseHTTPEndpoint):
auth_backend = None

async def get(self, *_):
return self._response(status_code=204)

0 comments on commit 2ad9e3e

Please sign in to comment.