Issue #23440: Improve http.server.SimpleHTTPRequestHandler tests
* Tests that index.html is served, rather than an automatic directory listing * Tests that there is no extra data sent after the response Patch by Martin Panter.
This commit is contained in:
commit
f25f788c6c
|
@ -298,12 +298,28 @@ class SimpleHTTPServerTestCase(BaseTestCase):
|
||||||
BaseTestCase.tearDown(self)
|
BaseTestCase.tearDown(self)
|
||||||
|
|
||||||
def check_status_and_reason(self, response, status, data=None):
|
def check_status_and_reason(self, response, status, data=None):
|
||||||
|
def close_conn():
|
||||||
|
"""Don't close reader yet so we can check if there was leftover
|
||||||
|
buffered input"""
|
||||||
|
nonlocal reader
|
||||||
|
reader = response.fp
|
||||||
|
response.fp = None
|
||||||
|
reader = None
|
||||||
|
response._close_conn = close_conn
|
||||||
|
|
||||||
body = response.read()
|
body = response.read()
|
||||||
self.assertTrue(response)
|
self.assertTrue(response)
|
||||||
self.assertEqual(response.status, status)
|
self.assertEqual(response.status, status)
|
||||||
self.assertIsNotNone(response.reason)
|
self.assertIsNotNone(response.reason)
|
||||||
if data:
|
if data:
|
||||||
self.assertEqual(data, body)
|
self.assertEqual(data, body)
|
||||||
|
# Ensure the server has not set up a persistent connection, and has
|
||||||
|
# not sent any extra data
|
||||||
|
self.assertEqual(response.version, 10)
|
||||||
|
self.assertEqual(response.msg.get("Connection", "close"), "close")
|
||||||
|
self.assertEqual(reader.read(30), b'', 'Connection should be closed')
|
||||||
|
|
||||||
|
reader.close()
|
||||||
return body
|
return body
|
||||||
|
|
||||||
@support.requires_mac_ver(10, 5)
|
@support.requires_mac_ver(10, 5)
|
||||||
|
@ -353,15 +369,21 @@ class SimpleHTTPServerTestCase(BaseTestCase):
|
||||||
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
|
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
|
||||||
response = self.request('/' + 'ThisDoesNotExist' + '/')
|
response = self.request('/' + 'ThisDoesNotExist' + '/')
|
||||||
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
|
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
|
||||||
with open(os.path.join(self.tempdir_name, 'index.html'), 'w') as f:
|
|
||||||
response = self.request('/' + self.tempdir_name + '/')
|
data = b"Dummy index file\r\n"
|
||||||
self.check_status_and_reason(response, HTTPStatus.OK)
|
with open(os.path.join(self.tempdir_name, 'index.html'), 'wb') as f:
|
||||||
# chmod() doesn't work as expected on Windows, and filesystem
|
f.write(data)
|
||||||
# permissions are ignored by root on Unix.
|
response = self.request('/' + self.tempdir_name + '/')
|
||||||
if os.name == 'posix' and os.geteuid() != 0:
|
self.check_status_and_reason(response, HTTPStatus.OK, data)
|
||||||
os.chmod(self.tempdir, 0)
|
|
||||||
|
# chmod() doesn't work as expected on Windows, and filesystem
|
||||||
|
# permissions are ignored by root on Unix.
|
||||||
|
if os.name == 'posix' and os.geteuid() != 0:
|
||||||
|
os.chmod(self.tempdir, 0)
|
||||||
|
try:
|
||||||
response = self.request(self.tempdir_name + '/')
|
response = self.request(self.tempdir_name + '/')
|
||||||
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
|
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
|
||||||
|
finally:
|
||||||
os.chmod(self.tempdir, 0o755)
|
os.chmod(self.tempdir, 0o755)
|
||||||
|
|
||||||
def test_head(self):
|
def test_head(self):
|
||||||
|
|
Loading…
Reference in New Issue