summaryrefslogtreecommitdiff
path: root/tests/servers/test_basehttp.py
diff options
context:
space:
mode:
authorPaul Bailey <paul@neutron.studio>2023-12-31 01:32:37 -0600
committerGitHub <noreply@github.com>2023-12-31 08:32:37 +0100
commit9d52e0720f79ac3cff3d9888a97ac227884a621e (patch)
tree56b1e0ad3a5aa95eb2b59fe6c36cb1dd4604c9ab /tests/servers/test_basehttp.py
parentdc26a3d563b1e1d98d40f5d351a6a61c34f12d98 (diff)
Fixed #35051 -- Prevented runserver from removing non-zero Content-Length for HEAD requests.
Diffstat (limited to 'tests/servers/test_basehttp.py')
-rw-r--r--tests/servers/test_basehttp.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/servers/test_basehttp.py b/tests/servers/test_basehttp.py
index 1e535e933e..cc4701114a 100644
--- a/tests/servers/test_basehttp.py
+++ b/tests/servers/test_basehttp.py
@@ -161,6 +161,45 @@ class WSGIRequestHandlerTestCase(SimpleTestCase):
)
self.assertNotIn(b"Connection: close\r\n", lines)
+ def test_non_zero_content_length_set_head_request(self):
+ hello_world_body = b"<!DOCTYPE html><html><body>Hello World</body></html>"
+ content_length = len(hello_world_body)
+
+ def test_app(environ, start_response):
+ """
+ A WSGI app that returns a hello world with non-zero Content-Length.
+ """
+ start_response("200 OK", [("Content-length", str(content_length))])
+ return [hello_world_body]
+
+ rfile = BytesIO(b"HEAD / HTTP/1.0\r\n")
+ rfile.seek(0)
+
+ wfile = UnclosableBytesIO()
+
+ def makefile(mode, *a, **kw):
+ if mode == "rb":
+ return rfile
+ elif mode == "wb":
+ return wfile
+
+ request = Stub(makefile=makefile)
+ server = Stub(base_environ={}, get_app=lambda: test_app)
+
+ # Prevent logging from appearing in test output.
+ with self.assertLogs("django.server", "INFO"):
+ # Instantiating a handler runs the request as side effect.
+ WSGIRequestHandler(request, "192.168.0.2", server)
+
+ wfile.seek(0)
+ lines = list(wfile.readlines())
+ body = lines[-1]
+ # The body is not returned in a HEAD response.
+ self.assertEqual(body, b"\r\n")
+ # Non-zero Content-Length is not removed.
+ self.assertEqual(lines[-2], f"Content-length: {content_length}\r\n".encode())
+ self.assertNotIn(b"Connection: close\r\n", lines)
+
class WSGIServerTestCase(SimpleTestCase):
request_factory = RequestFactory()