diff --git a/sanic/response.py b/sanic/response.py index 01d0843d6f..26da64ae48 100644 --- a/sanic/response.py +++ b/sanic/response.py @@ -198,7 +198,7 @@ def output( body = b'' content_length = 0 - if self.status is not 204: + if self.status is not 204 and self.status != 304: body = self.body content_length = self.headers.get('Content-Length', len(self.body)) diff --git a/tests/test_response.py b/tests/test_response.py index e7690c1180..79f2b74f2b 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -67,6 +67,14 @@ async def test(request): async def no_content_handler(request): return json(JSON_DATA, status=204) + @app.get("/no-content/unmodified") + async def no_content_unmodified_handler(request): + return json(None, status=304) + + @app.get("/unmodified") + async def unmodified_handler(request): + return json(JSON_DATA, status=304) + @app.delete("/") async def delete_handler(request): return json(None, status=204) @@ -88,6 +96,16 @@ def test_no_content(json_app): assert response.text == '' assert response.headers['Content-Length'] == '0' + request, response = json_app.test_client.get('/no-content/unmodified') + assert response.status == 304 + assert response.text == '' + assert response.headers['Content-Length'] == '0' + + request, response = json_app.test_client.get('/unmodified') + assert response.status == 304 + assert response.text == '' + assert response.headers['Content-Length'] == '0' + request, response = json_app.test_client.delete('/') assert response.status == 204 assert response.text == ''