|
8 | 8 | SimpleHTTPRequestHandler, CGIHTTPRequestHandler |
9 | 9 | from http import server, HTTPStatus |
10 | 10 |
|
| 11 | +import contextlib |
11 | 12 | import os |
12 | 13 | import socket |
13 | 14 | import sys |
@@ -316,6 +317,44 @@ def test_head_via_send_error(self): |
316 | 317 | self.assertEqual(b'', data) |
317 | 318 |
|
318 | 319 |
|
| 320 | +class HTTP09ServerTestCase(BaseTestCase): |
| 321 | + |
| 322 | + class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler): |
| 323 | + """Request handler for HTTP/0.9 server.""" |
| 324 | + |
| 325 | + def do_GET(self): |
| 326 | + self.wfile.write(f'OK: here is {self.path}\r\n'.encode()) |
| 327 | + |
| 328 | + def setUp(self): |
| 329 | + super().setUp() |
| 330 | + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 331 | + self.sock = self.enterContext(self.sock) |
| 332 | + self.sock.connect((self.HOST, self.PORT)) |
| 333 | + |
| 334 | + def test_simple_get(self): |
| 335 | + self.sock.send(b'GET /index.html\r\n') |
| 336 | + res = self.sock.recv(1024) |
| 337 | + self.assertEqual(res, b"OK: here is /index.html\r\n") |
| 338 | + |
| 339 | + def test_invalid_request(self): |
| 340 | + self.sock.send(b'POST /index.html\r\n') |
| 341 | + res = self.sock.recv(1024) |
| 342 | + self.assertIn(b"Bad HTTP/0.9 request type ('POST')", res) |
| 343 | + |
| 344 | + def test_single_request(self): |
| 345 | + self.sock.send(b'GET /foo.html\r\n') |
| 346 | + res = self.sock.recv(1024) |
| 347 | + self.assertEqual(res, b"OK: here is /foo.html\r\n") |
| 348 | + |
| 349 | + # Ignore errors if the connection is already closed, |
| 350 | + # as this is the expected behavior of HTTP/0.9. |
| 351 | + with contextlib.suppress(OSError): |
| 352 | + self.sock.send(b'GET /bar.html\r\n') |
| 353 | + res = self.sock.recv(1024) |
| 354 | + # The server should not process our request. |
| 355 | + self.assertEqual(res, b'') |
| 356 | + |
| 357 | + |
319 | 358 | class RequestHandlerLoggingTestCase(BaseTestCase): |
320 | 359 | class request_handler(BaseHTTPRequestHandler): |
321 | 360 | protocol_version = 'HTTP/1.1' |
|
0 commit comments