|
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 |
@@ -359,6 +360,44 @@ def test_head_via_send_error(self): |
359 | 360 | self.assertEqual(b'', data) |
360 | 361 |
|
361 | 362 |
|
| 363 | +class HTTP09ServerTestCase(BaseTestCase): |
| 364 | + |
| 365 | + class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler): |
| 366 | + """Request handler for HTTP/0.9 server.""" |
| 367 | + |
| 368 | + def do_GET(self): |
| 369 | + self.wfile.write(f'OK: here is {self.path}\r\n'.encode()) |
| 370 | + |
| 371 | + def setUp(self): |
| 372 | + super().setUp() |
| 373 | + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 374 | + self.sock = self.enterContext(self.sock) |
| 375 | + self.sock.connect((self.HOST, self.PORT)) |
| 376 | + |
| 377 | + def test_simple_get(self): |
| 378 | + self.sock.send(b'GET /index.html\r\n') |
| 379 | + res = self.sock.recv(1024) |
| 380 | + self.assertEqual(res, b"OK: here is /index.html\r\n") |
| 381 | + |
| 382 | + def test_invalid_request(self): |
| 383 | + self.sock.send(b'POST /index.html\r\n') |
| 384 | + res = self.sock.recv(1024) |
| 385 | + self.assertIn(b"Bad HTTP/0.9 request type ('POST')", res) |
| 386 | + |
| 387 | + def test_single_request(self): |
| 388 | + self.sock.send(b'GET /foo.html\r\n') |
| 389 | + res = self.sock.recv(1024) |
| 390 | + self.assertEqual(res, b"OK: here is /foo.html\r\n") |
| 391 | + |
| 392 | + # Ignore errors if the connection is already closed, |
| 393 | + # as this is the expected behavior of HTTP/0.9. |
| 394 | + with contextlib.suppress(OSError): |
| 395 | + self.sock.send(b'GET /bar.html\r\n') |
| 396 | + res = self.sock.recv(1024) |
| 397 | + # The server should not process our request. |
| 398 | + self.assertEqual(res, b'') |
| 399 | + |
| 400 | + |
362 | 401 | def certdata_file(*path): |
363 | 402 | return os.path.join(os.path.dirname(__file__), "certdata", *path) |
364 | 403 |
|
|
0 commit comments