Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1 | """Unittests for the various HTTPServer modules. |
| 2 | |
| 3 | Written by Cody A.W. Somerville <cody-somerville@ubuntu.com>, |
| 4 | Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest. |
| 5 | """ |
Senthil Kumaran | da3d2ab | 2020-12-05 05:26:24 -0800 | [diff] [blame] | 6 | from collections import OrderedDict |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 7 | from http.server import BaseHTTPRequestHandler, HTTPServer, \ |
| 8 | SimpleHTTPRequestHandler, CGIHTTPRequestHandler |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 9 | from http import server, HTTPStatus |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 10 | |
| 11 | import os |
Lisa Roach | 433433f | 2018-11-26 10:43:38 -0800 | [diff] [blame] | 12 | import socket |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 13 | import sys |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 14 | import re |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 15 | import base64 |
Martin Panter | d274b3f | 2016-04-18 03:45:18 +0000 | [diff] [blame] | 16 | import ntpath |
Géry Ogam | 781266e | 2019-09-11 15:03:46 +0200 | [diff] [blame] | 17 | import pathlib |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 18 | import shutil |
Pierre Quentel | 351adda | 2017-04-02 12:26:12 +0200 | [diff] [blame] | 19 | import email.message |
| 20 | import email.utils |
Serhiy Storchaka | cb5bc40 | 2014-08-17 08:22:11 +0300 | [diff] [blame] | 21 | import html |
Senthil Kumaran | da3d2ab | 2020-12-05 05:26:24 -0800 | [diff] [blame] | 22 | import http, http.client |
Pierre Quentel | 351adda | 2017-04-02 12:26:12 +0200 | [diff] [blame] | 23 | import urllib.parse |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 24 | import tempfile |
Berker Peksag | 04bc5b9 | 2016-03-14 06:06:03 +0200 | [diff] [blame] | 25 | import time |
Pierre Quentel | 351adda | 2017-04-02 12:26:12 +0200 | [diff] [blame] | 26 | import datetime |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 27 | import threading |
Stéphane Wirtel | a17a2f5 | 2017-05-24 09:29:06 +0200 | [diff] [blame] | 28 | from unittest import mock |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 29 | from io import BytesIO |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 30 | |
| 31 | import unittest |
| 32 | from test import support |
Hai Shi | a089d21 | 2020-07-06 17:15:08 +0800 | [diff] [blame] | 33 | from test.support import os_helper |
Hai Shi | e80697d | 2020-05-28 06:10:27 +0800 | [diff] [blame] | 34 | from test.support import threading_helper |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 35 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 36 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 37 | class NoLogRequestHandler: |
| 38 | def log_message(self, *args): |
| 39 | # don't write log messages to stderr |
| 40 | pass |
| 41 | |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 42 | def read(self, n=None): |
| 43 | return '' |
| 44 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 45 | |
| 46 | class TestServerThread(threading.Thread): |
| 47 | def __init__(self, test_object, request_handler): |
| 48 | threading.Thread.__init__(self) |
| 49 | self.request_handler = request_handler |
| 50 | self.test_object = test_object |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 51 | |
| 52 | def run(self): |
Antoine Pitrou | cb34218 | 2011-03-21 00:26:51 +0100 | [diff] [blame] | 53 | self.server = HTTPServer(('localhost', 0), self.request_handler) |
| 54 | self.test_object.HOST, self.test_object.PORT = self.server.socket.getsockname() |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 55 | self.test_object.server_started.set() |
| 56 | self.test_object = None |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 57 | try: |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 58 | self.server.serve_forever(0.05) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 59 | finally: |
| 60 | self.server.server_close() |
| 61 | |
| 62 | def stop(self): |
| 63 | self.server.shutdown() |
Victor Stinner | 830d7d2 | 2017-08-22 18:05:07 +0200 | [diff] [blame] | 64 | self.join() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 65 | |
| 66 | |
| 67 | class BaseTestCase(unittest.TestCase): |
| 68 | def setUp(self): |
Hai Shi | e80697d | 2020-05-28 06:10:27 +0800 | [diff] [blame] | 69 | self._threads = threading_helper.threading_setup() |
Hai Shi | 79bb2c9 | 2020-08-06 19:51:29 +0800 | [diff] [blame] | 70 | os.environ = os_helper.EnvironmentVarGuard() |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 71 | self.server_started = threading.Event() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 72 | self.thread = TestServerThread(self, self.request_handler) |
| 73 | self.thread.start() |
Antoine Pitrou | 08911bd | 2010-04-25 22:19:43 +0000 | [diff] [blame] | 74 | self.server_started.wait() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 75 | |
| 76 | def tearDown(self): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 77 | self.thread.stop() |
Antoine Pitrou | f727082 | 2012-09-30 01:05:30 +0200 | [diff] [blame] | 78 | self.thread = None |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 79 | os.environ.__exit__() |
Hai Shi | e80697d | 2020-05-28 06:10:27 +0800 | [diff] [blame] | 80 | threading_helper.threading_cleanup(*self._threads) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 81 | |
| 82 | def request(self, uri, method='GET', body=None, headers={}): |
Antoine Pitrou | cb34218 | 2011-03-21 00:26:51 +0100 | [diff] [blame] | 83 | self.connection = http.client.HTTPConnection(self.HOST, self.PORT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 84 | self.connection.request(method, uri, body, headers) |
| 85 | return self.connection.getresponse() |
| 86 | |
| 87 | |
| 88 | class BaseHTTPServerTestCase(BaseTestCase): |
| 89 | class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler): |
| 90 | protocol_version = 'HTTP/1.1' |
| 91 | default_request_version = 'HTTP/1.1' |
| 92 | |
| 93 | def do_TEST(self): |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 94 | self.send_response(HTTPStatus.NO_CONTENT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 95 | self.send_header('Content-Type', 'text/html') |
| 96 | self.send_header('Connection', 'close') |
| 97 | self.end_headers() |
| 98 | |
| 99 | def do_KEEP(self): |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 100 | self.send_response(HTTPStatus.NO_CONTENT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 101 | self.send_header('Content-Type', 'text/html') |
| 102 | self.send_header('Connection', 'keep-alive') |
| 103 | self.end_headers() |
| 104 | |
| 105 | def do_KEYERROR(self): |
| 106 | self.send_error(999) |
| 107 | |
Senthil Kumaran | 52d2720 | 2012-10-10 23:16:21 -0700 | [diff] [blame] | 108 | def do_NOTFOUND(self): |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 109 | self.send_error(HTTPStatus.NOT_FOUND) |
Senthil Kumaran | 52d2720 | 2012-10-10 23:16:21 -0700 | [diff] [blame] | 110 | |
Senthil Kumaran | 2688644 | 2013-03-15 07:53:21 -0700 | [diff] [blame] | 111 | def do_EXPLAINERROR(self): |
| 112 | self.send_error(999, "Short Message", |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 113 | "This is a long \n explanation") |
Senthil Kumaran | 2688644 | 2013-03-15 07:53:21 -0700 | [diff] [blame] | 114 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 115 | def do_CUSTOM(self): |
| 116 | self.send_response(999) |
| 117 | self.send_header('Content-Type', 'text/html') |
| 118 | self.send_header('Connection', 'close') |
| 119 | self.end_headers() |
| 120 | |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 121 | def do_LATINONEHEADER(self): |
| 122 | self.send_response(999) |
| 123 | self.send_header('X-Special', 'Dängerous Mind') |
Armin Ronacher | 5953128 | 2011-01-22 13:44:22 +0000 | [diff] [blame] | 124 | self.send_header('Connection', 'close') |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 125 | self.end_headers() |
Armin Ronacher | 5953128 | 2011-01-22 13:44:22 +0000 | [diff] [blame] | 126 | body = self.headers['x-special-incoming'].encode('utf-8') |
| 127 | self.wfile.write(body) |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 128 | |
Martin Panter | e42e129 | 2016-06-08 08:29:13 +0000 | [diff] [blame] | 129 | def do_SEND_ERROR(self): |
| 130 | self.send_error(int(self.path[1:])) |
| 131 | |
| 132 | def do_HEAD(self): |
| 133 | self.send_error(int(self.path[1:])) |
| 134 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 135 | def setUp(self): |
| 136 | BaseTestCase.setUp(self) |
Antoine Pitrou | cb34218 | 2011-03-21 00:26:51 +0100 | [diff] [blame] | 137 | self.con = http.client.HTTPConnection(self.HOST, self.PORT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 138 | self.con.connect() |
| 139 | |
| 140 | def test_command(self): |
| 141 | self.con.request('GET', '/') |
| 142 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 143 | self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 144 | |
| 145 | def test_request_line_trimming(self): |
| 146 | self.con._http_vsn_str = 'HTTP/1.1\n' |
R David Murray | 14199f9 | 2014-06-24 16:39:49 -0400 | [diff] [blame] | 147 | self.con.putrequest('XYZBOGUS', '/') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 148 | self.con.endheaders() |
| 149 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 150 | self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 151 | |
| 152 | def test_version_bogus(self): |
| 153 | self.con._http_vsn_str = 'FUBAR' |
| 154 | self.con.putrequest('GET', '/') |
| 155 | self.con.endheaders() |
| 156 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 157 | self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 158 | |
| 159 | def test_version_digits(self): |
| 160 | self.con._http_vsn_str = 'HTTP/9.9.9' |
| 161 | self.con.putrequest('GET', '/') |
| 162 | self.con.endheaders() |
| 163 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 164 | self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 165 | |
| 166 | def test_version_none_get(self): |
| 167 | self.con._http_vsn_str = '' |
| 168 | self.con.putrequest('GET', '/') |
| 169 | self.con.endheaders() |
| 170 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 171 | self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 172 | |
| 173 | def test_version_none(self): |
R David Murray | 14199f9 | 2014-06-24 16:39:49 -0400 | [diff] [blame] | 174 | # Test that a valid method is rejected when not HTTP/1.x |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 175 | self.con._http_vsn_str = '' |
R David Murray | 14199f9 | 2014-06-24 16:39:49 -0400 | [diff] [blame] | 176 | self.con.putrequest('CUSTOM', '/') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 177 | self.con.endheaders() |
| 178 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 179 | self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 180 | |
| 181 | def test_version_invalid(self): |
| 182 | self.con._http_vsn = 99 |
| 183 | self.con._http_vsn_str = 'HTTP/9.9' |
| 184 | self.con.putrequest('GET', '/') |
| 185 | self.con.endheaders() |
| 186 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 187 | self.assertEqual(res.status, HTTPStatus.HTTP_VERSION_NOT_SUPPORTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 188 | |
| 189 | def test_send_blank(self): |
| 190 | self.con._http_vsn_str = '' |
| 191 | self.con.putrequest('', '') |
| 192 | self.con.endheaders() |
| 193 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 194 | self.assertEqual(res.status, HTTPStatus.BAD_REQUEST) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 195 | |
| 196 | def test_header_close(self): |
| 197 | self.con.putrequest('GET', '/') |
| 198 | self.con.putheader('Connection', 'close') |
| 199 | self.con.endheaders() |
| 200 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 201 | self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 202 | |
Berker Peksag | 2085361 | 2016-08-25 01:13:34 +0300 | [diff] [blame] | 203 | def test_header_keep_alive(self): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 204 | self.con._http_vsn_str = 'HTTP/1.1' |
| 205 | self.con.putrequest('GET', '/') |
| 206 | self.con.putheader('Connection', 'keep-alive') |
| 207 | self.con.endheaders() |
| 208 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 209 | self.assertEqual(res.status, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 210 | |
| 211 | def test_handler(self): |
| 212 | self.con.request('TEST', '/') |
| 213 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 214 | self.assertEqual(res.status, HTTPStatus.NO_CONTENT) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 215 | |
| 216 | def test_return_header_keep_alive(self): |
| 217 | self.con.request('KEEP', '/') |
| 218 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 219 | self.assertEqual(res.getheader('Connection'), 'keep-alive') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 220 | self.con.request('TEST', '/') |
Brian Curtin | 61d0d60 | 2010-10-31 00:34:23 +0000 | [diff] [blame] | 221 | self.addCleanup(self.con.close) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 222 | |
| 223 | def test_internal_key_error(self): |
| 224 | self.con.request('KEYERROR', '/') |
| 225 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 226 | self.assertEqual(res.status, 999) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 227 | |
| 228 | def test_return_custom_status(self): |
| 229 | self.con.request('CUSTOM', '/') |
| 230 | res = self.con.getresponse() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 231 | self.assertEqual(res.status, 999) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 232 | |
Senthil Kumaran | 2688644 | 2013-03-15 07:53:21 -0700 | [diff] [blame] | 233 | def test_return_explain_error(self): |
| 234 | self.con.request('EXPLAINERROR', '/') |
| 235 | res = self.con.getresponse() |
| 236 | self.assertEqual(res.status, 999) |
| 237 | self.assertTrue(int(res.getheader('Content-Length'))) |
| 238 | |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 239 | def test_latin1_header(self): |
Armin Ronacher | 5953128 | 2011-01-22 13:44:22 +0000 | [diff] [blame] | 240 | self.con.request('LATINONEHEADER', '/', headers={ |
| 241 | 'X-Special-Incoming': 'Ärger mit Unicode' |
| 242 | }) |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 243 | res = self.con.getresponse() |
| 244 | self.assertEqual(res.getheader('X-Special'), 'Dängerous Mind') |
Armin Ronacher | 5953128 | 2011-01-22 13:44:22 +0000 | [diff] [blame] | 245 | self.assertEqual(res.read(), 'Ärger mit Unicode'.encode('utf-8')) |
Armin Ronacher | 8d96d77 | 2011-01-22 13:13:05 +0000 | [diff] [blame] | 246 | |
Senthil Kumaran | 52d2720 | 2012-10-10 23:16:21 -0700 | [diff] [blame] | 247 | def test_error_content_length(self): |
| 248 | # Issue #16088: standard error responses should have a content-length |
| 249 | self.con.request('NOTFOUND', '/') |
| 250 | res = self.con.getresponse() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 251 | self.assertEqual(res.status, HTTPStatus.NOT_FOUND) |
| 252 | |
Senthil Kumaran | 52d2720 | 2012-10-10 23:16:21 -0700 | [diff] [blame] | 253 | data = res.read() |
Senthil Kumaran | 52d2720 | 2012-10-10 23:16:21 -0700 | [diff] [blame] | 254 | self.assertEqual(int(res.getheader('Content-Length')), len(data)) |
| 255 | |
Martin Panter | e42e129 | 2016-06-08 08:29:13 +0000 | [diff] [blame] | 256 | def test_send_error(self): |
| 257 | allow_transfer_encoding_codes = (HTTPStatus.NOT_MODIFIED, |
| 258 | HTTPStatus.RESET_CONTENT) |
| 259 | for code in (HTTPStatus.NO_CONTENT, HTTPStatus.NOT_MODIFIED, |
| 260 | HTTPStatus.PROCESSING, HTTPStatus.RESET_CONTENT, |
| 261 | HTTPStatus.SWITCHING_PROTOCOLS): |
| 262 | self.con.request('SEND_ERROR', '/{}'.format(code)) |
| 263 | res = self.con.getresponse() |
| 264 | self.assertEqual(code, res.status) |
| 265 | self.assertEqual(None, res.getheader('Content-Length')) |
| 266 | self.assertEqual(None, res.getheader('Content-Type')) |
| 267 | if code not in allow_transfer_encoding_codes: |
| 268 | self.assertEqual(None, res.getheader('Transfer-Encoding')) |
| 269 | |
| 270 | data = res.read() |
| 271 | self.assertEqual(b'', data) |
| 272 | |
| 273 | def test_head_via_send_error(self): |
| 274 | allow_transfer_encoding_codes = (HTTPStatus.NOT_MODIFIED, |
| 275 | HTTPStatus.RESET_CONTENT) |
| 276 | for code in (HTTPStatus.OK, HTTPStatus.NO_CONTENT, |
| 277 | HTTPStatus.NOT_MODIFIED, HTTPStatus.RESET_CONTENT, |
| 278 | HTTPStatus.SWITCHING_PROTOCOLS): |
| 279 | self.con.request('HEAD', '/{}'.format(code)) |
| 280 | res = self.con.getresponse() |
| 281 | self.assertEqual(code, res.status) |
| 282 | if code == HTTPStatus.OK: |
| 283 | self.assertTrue(int(res.getheader('Content-Length')) > 0) |
| 284 | self.assertIn('text/html', res.getheader('Content-Type')) |
| 285 | else: |
| 286 | self.assertEqual(None, res.getheader('Content-Length')) |
| 287 | self.assertEqual(None, res.getheader('Content-Type')) |
| 288 | if code not in allow_transfer_encoding_codes: |
| 289 | self.assertEqual(None, res.getheader('Transfer-Encoding')) |
| 290 | |
| 291 | data = res.read() |
| 292 | self.assertEqual(b'', data) |
| 293 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 294 | |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 295 | class RequestHandlerLoggingTestCase(BaseTestCase): |
| 296 | class request_handler(BaseHTTPRequestHandler): |
| 297 | protocol_version = 'HTTP/1.1' |
| 298 | default_request_version = 'HTTP/1.1' |
| 299 | |
| 300 | def do_GET(self): |
| 301 | self.send_response(HTTPStatus.OK) |
| 302 | self.end_headers() |
| 303 | |
| 304 | def do_ERROR(self): |
| 305 | self.send_error(HTTPStatus.NOT_FOUND, 'File not found') |
| 306 | |
| 307 | def test_get(self): |
| 308 | self.con = http.client.HTTPConnection(self.HOST, self.PORT) |
| 309 | self.con.connect() |
| 310 | |
| 311 | with support.captured_stderr() as err: |
| 312 | self.con.request('GET', '/') |
| 313 | self.con.getresponse() |
| 314 | |
| 315 | self.assertTrue( |
| 316 | err.getvalue().endswith('"GET / HTTP/1.1" 200 -\n')) |
| 317 | |
| 318 | def test_err(self): |
| 319 | self.con = http.client.HTTPConnection(self.HOST, self.PORT) |
| 320 | self.con.connect() |
| 321 | |
| 322 | with support.captured_stderr() as err: |
| 323 | self.con.request('ERROR', '/') |
| 324 | self.con.getresponse() |
| 325 | |
| 326 | lines = err.getvalue().split('\n') |
| 327 | self.assertTrue(lines[0].endswith('code 404, message File not found')) |
| 328 | self.assertTrue(lines[1].endswith('"ERROR / HTTP/1.1" 404 -')) |
| 329 | |
| 330 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 331 | class SimpleHTTPServerTestCase(BaseTestCase): |
| 332 | class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler): |
| 333 | pass |
| 334 | |
| 335 | def setUp(self): |
| 336 | BaseTestCase.setUp(self) |
| 337 | self.cwd = os.getcwd() |
| 338 | basetempdir = tempfile.gettempdir() |
| 339 | os.chdir(basetempdir) |
| 340 | self.data = b'We are the knights who say Ni!' |
| 341 | self.tempdir = tempfile.mkdtemp(dir=basetempdir) |
| 342 | self.tempdir_name = os.path.basename(self.tempdir) |
Martin Panter | fc475a9 | 2016-04-09 04:56:10 +0000 | [diff] [blame] | 343 | self.base_url = '/' + self.tempdir_name |
Victor Stinner | 28ce07a | 2017-07-28 18:15:02 +0200 | [diff] [blame] | 344 | tempname = os.path.join(self.tempdir, 'test') |
| 345 | with open(tempname, 'wb') as temp: |
Brett Cannon | 105df5d | 2010-10-29 23:43:42 +0000 | [diff] [blame] | 346 | temp.write(self.data) |
Victor Stinner | 28ce07a | 2017-07-28 18:15:02 +0200 | [diff] [blame] | 347 | temp.flush() |
| 348 | mtime = os.stat(tempname).st_mtime |
Pierre Quentel | 351adda | 2017-04-02 12:26:12 +0200 | [diff] [blame] | 349 | # compute last modification datetime for browser cache tests |
| 350 | last_modif = datetime.datetime.fromtimestamp(mtime, |
| 351 | datetime.timezone.utc) |
| 352 | self.last_modif_datetime = last_modif.replace(microsecond=0) |
| 353 | self.last_modif_header = email.utils.formatdate( |
| 354 | last_modif.timestamp(), usegmt=True) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 355 | |
| 356 | def tearDown(self): |
| 357 | try: |
| 358 | os.chdir(self.cwd) |
| 359 | try: |
| 360 | shutil.rmtree(self.tempdir) |
| 361 | except: |
| 362 | pass |
| 363 | finally: |
| 364 | BaseTestCase.tearDown(self) |
| 365 | |
| 366 | def check_status_and_reason(self, response, status, data=None): |
Berker Peksag | b575432 | 2015-07-22 19:25:37 +0300 | [diff] [blame] | 367 | def close_conn(): |
| 368 | """Don't close reader yet so we can check if there was leftover |
| 369 | buffered input""" |
| 370 | nonlocal reader |
| 371 | reader = response.fp |
| 372 | response.fp = None |
| 373 | reader = None |
| 374 | response._close_conn = close_conn |
| 375 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 376 | body = response.read() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 377 | self.assertTrue(response) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 378 | self.assertEqual(response.status, status) |
| 379 | self.assertIsNotNone(response.reason) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 380 | if data: |
| 381 | self.assertEqual(data, body) |
Berker Peksag | b575432 | 2015-07-22 19:25:37 +0300 | [diff] [blame] | 382 | # Ensure the server has not set up a persistent connection, and has |
| 383 | # not sent any extra data |
| 384 | self.assertEqual(response.version, 10) |
| 385 | self.assertEqual(response.msg.get("Connection", "close"), "close") |
| 386 | self.assertEqual(reader.read(30), b'', 'Connection should be closed') |
| 387 | |
| 388 | reader.close() |
Serhiy Storchaka | cb5bc40 | 2014-08-17 08:22:11 +0300 | [diff] [blame] | 389 | return body |
| 390 | |
Ned Deily | b3edde8 | 2017-12-04 23:42:02 -0500 | [diff] [blame] | 391 | @unittest.skipIf(sys.platform == 'darwin', |
| 392 | 'undecodable name cannot always be decoded on macOS') |
Steve Dower | e58571b | 2016-09-08 11:11:13 -0700 | [diff] [blame] | 393 | @unittest.skipIf(sys.platform == 'win32', |
| 394 | 'undecodable name cannot be decoded on win32') |
Hai Shi | a089d21 | 2020-07-06 17:15:08 +0800 | [diff] [blame] | 395 | @unittest.skipUnless(os_helper.TESTFN_UNDECODABLE, |
| 396 | 'need os_helper.TESTFN_UNDECODABLE') |
Serhiy Storchaka | cb5bc40 | 2014-08-17 08:22:11 +0300 | [diff] [blame] | 397 | def test_undecodable_filename(self): |
Serhiy Storchaka | a64ce5d | 2014-08-17 12:20:02 +0300 | [diff] [blame] | 398 | enc = sys.getfilesystemencoding() |
Hai Shi | a089d21 | 2020-07-06 17:15:08 +0800 | [diff] [blame] | 399 | filename = os.fsdecode(os_helper.TESTFN_UNDECODABLE) + '.txt' |
Serhiy Storchaka | cb5bc40 | 2014-08-17 08:22:11 +0300 | [diff] [blame] | 400 | with open(os.path.join(self.tempdir, filename), 'wb') as f: |
Hai Shi | a089d21 | 2020-07-06 17:15:08 +0800 | [diff] [blame] | 401 | f.write(os_helper.TESTFN_UNDECODABLE) |
Martin Panter | fc475a9 | 2016-04-09 04:56:10 +0000 | [diff] [blame] | 402 | response = self.request(self.base_url + '/') |
Serhiy Storchaka | d9e9528 | 2014-08-17 16:57:39 +0300 | [diff] [blame] | 403 | if sys.platform == 'darwin': |
| 404 | # On Mac OS the HFS+ filesystem replaces bytes that aren't valid |
| 405 | # UTF-8 into a percent-encoded value. |
| 406 | for name in os.listdir(self.tempdir): |
| 407 | if name != 'test': # Ignore a filename created in setUp(). |
| 408 | filename = name |
| 409 | break |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 410 | body = self.check_status_and_reason(response, HTTPStatus.OK) |
Serhiy Storchaka | cb5bc40 | 2014-08-17 08:22:11 +0300 | [diff] [blame] | 411 | quotedname = urllib.parse.quote(filename, errors='surrogatepass') |
| 412 | self.assertIn(('href="%s"' % quotedname) |
Serhiy Storchaka | a64ce5d | 2014-08-17 12:20:02 +0300 | [diff] [blame] | 413 | .encode(enc, 'surrogateescape'), body) |
Martin Panter | da3bb38 | 2016-04-11 00:40:08 +0000 | [diff] [blame] | 414 | self.assertIn(('>%s<' % html.escape(filename, quote=False)) |
Serhiy Storchaka | a64ce5d | 2014-08-17 12:20:02 +0300 | [diff] [blame] | 415 | .encode(enc, 'surrogateescape'), body) |
Martin Panter | fc475a9 | 2016-04-09 04:56:10 +0000 | [diff] [blame] | 416 | response = self.request(self.base_url + '/' + quotedname) |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 417 | self.check_status_and_reason(response, HTTPStatus.OK, |
Hai Shi | a089d21 | 2020-07-06 17:15:08 +0800 | [diff] [blame] | 418 | data=os_helper.TESTFN_UNDECODABLE) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 419 | |
| 420 | def test_get(self): |
| 421 | #constructs the path relative to the root directory of the HTTPServer |
Martin Panter | fc475a9 | 2016-04-09 04:56:10 +0000 | [diff] [blame] | 422 | response = self.request(self.base_url + '/test') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 423 | self.check_status_and_reason(response, HTTPStatus.OK, data=self.data) |
Senthil Kumaran | 72c238e | 2013-09-13 00:21:18 -0700 | [diff] [blame] | 424 | # check for trailing "/" which should return 404. See Issue17324 |
Martin Panter | fc475a9 | 2016-04-09 04:56:10 +0000 | [diff] [blame] | 425 | response = self.request(self.base_url + '/test/') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 426 | self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
Martin Panter | fc475a9 | 2016-04-09 04:56:10 +0000 | [diff] [blame] | 427 | response = self.request(self.base_url + '/') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 428 | self.check_status_and_reason(response, HTTPStatus.OK) |
Martin Panter | fc475a9 | 2016-04-09 04:56:10 +0000 | [diff] [blame] | 429 | response = self.request(self.base_url) |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 430 | self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY) |
Martin Panter | fc475a9 | 2016-04-09 04:56:10 +0000 | [diff] [blame] | 431 | response = self.request(self.base_url + '/?hi=2') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 432 | self.check_status_and_reason(response, HTTPStatus.OK) |
Martin Panter | fc475a9 | 2016-04-09 04:56:10 +0000 | [diff] [blame] | 433 | response = self.request(self.base_url + '?hi=1') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 434 | self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY) |
Benjamin Peterson | 94cb7a2 | 2014-12-26 10:53:43 -0600 | [diff] [blame] | 435 | self.assertEqual(response.getheader("Location"), |
Martin Panter | fc475a9 | 2016-04-09 04:56:10 +0000 | [diff] [blame] | 436 | self.base_url + "/?hi=1") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 437 | response = self.request('/ThisDoesNotExist') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 438 | self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 439 | response = self.request('/' + 'ThisDoesNotExist' + '/') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 440 | self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
Berker Peksag | b575432 | 2015-07-22 19:25:37 +0300 | [diff] [blame] | 441 | |
| 442 | data = b"Dummy index file\r\n" |
| 443 | with open(os.path.join(self.tempdir_name, 'index.html'), 'wb') as f: |
| 444 | f.write(data) |
Martin Panter | fc475a9 | 2016-04-09 04:56:10 +0000 | [diff] [blame] | 445 | response = self.request(self.base_url + '/') |
Berker Peksag | b575432 | 2015-07-22 19:25:37 +0300 | [diff] [blame] | 446 | self.check_status_and_reason(response, HTTPStatus.OK, data) |
| 447 | |
| 448 | # chmod() doesn't work as expected on Windows, and filesystem |
| 449 | # permissions are ignored by root on Unix. |
| 450 | if os.name == 'posix' and os.geteuid() != 0: |
| 451 | os.chmod(self.tempdir, 0) |
| 452 | try: |
Martin Panter | fc475a9 | 2016-04-09 04:56:10 +0000 | [diff] [blame] | 453 | response = self.request(self.base_url + '/') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 454 | self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
Berker Peksag | b575432 | 2015-07-22 19:25:37 +0300 | [diff] [blame] | 455 | finally: |
Brett Cannon | 105df5d | 2010-10-29 23:43:42 +0000 | [diff] [blame] | 456 | os.chmod(self.tempdir, 0o755) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 457 | |
| 458 | def test_head(self): |
| 459 | response = self.request( |
Martin Panter | fc475a9 | 2016-04-09 04:56:10 +0000 | [diff] [blame] | 460 | self.base_url + '/test', method='HEAD') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 461 | self.check_status_and_reason(response, HTTPStatus.OK) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 462 | self.assertEqual(response.getheader('content-length'), |
| 463 | str(len(self.data))) |
| 464 | self.assertEqual(response.getheader('content-type'), |
| 465 | 'application/octet-stream') |
| 466 | |
Pierre Quentel | 351adda | 2017-04-02 12:26:12 +0200 | [diff] [blame] | 467 | def test_browser_cache(self): |
| 468 | """Check that when a request to /test is sent with the request header |
| 469 | If-Modified-Since set to date of last modification, the server returns |
| 470 | status code 304, not 200 |
| 471 | """ |
| 472 | headers = email.message.Message() |
| 473 | headers['If-Modified-Since'] = self.last_modif_header |
| 474 | response = self.request(self.base_url + '/test', headers=headers) |
| 475 | self.check_status_and_reason(response, HTTPStatus.NOT_MODIFIED) |
| 476 | |
| 477 | # one hour after last modification : must return 304 |
| 478 | new_dt = self.last_modif_datetime + datetime.timedelta(hours=1) |
| 479 | headers = email.message.Message() |
| 480 | headers['If-Modified-Since'] = email.utils.format_datetime(new_dt, |
| 481 | usegmt=True) |
| 482 | response = self.request(self.base_url + '/test', headers=headers) |
Victor Stinner | 28ce07a | 2017-07-28 18:15:02 +0200 | [diff] [blame] | 483 | self.check_status_and_reason(response, HTTPStatus.NOT_MODIFIED) |
Pierre Quentel | 351adda | 2017-04-02 12:26:12 +0200 | [diff] [blame] | 484 | |
| 485 | def test_browser_cache_file_changed(self): |
| 486 | # with If-Modified-Since earlier than Last-Modified, must return 200 |
| 487 | dt = self.last_modif_datetime |
| 488 | # build datetime object : 365 days before last modification |
| 489 | old_dt = dt - datetime.timedelta(days=365) |
| 490 | headers = email.message.Message() |
| 491 | headers['If-Modified-Since'] = email.utils.format_datetime(old_dt, |
| 492 | usegmt=True) |
| 493 | response = self.request(self.base_url + '/test', headers=headers) |
| 494 | self.check_status_and_reason(response, HTTPStatus.OK) |
| 495 | |
| 496 | def test_browser_cache_with_If_None_Match_header(self): |
| 497 | # if If-None-Match header is present, ignore If-Modified-Since |
| 498 | |
| 499 | headers = email.message.Message() |
| 500 | headers['If-Modified-Since'] = self.last_modif_header |
| 501 | headers['If-None-Match'] = "*" |
| 502 | response = self.request(self.base_url + '/test', headers=headers) |
Victor Stinner | 28ce07a | 2017-07-28 18:15:02 +0200 | [diff] [blame] | 503 | self.check_status_and_reason(response, HTTPStatus.OK) |
Pierre Quentel | 351adda | 2017-04-02 12:26:12 +0200 | [diff] [blame] | 504 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 505 | def test_invalid_requests(self): |
| 506 | response = self.request('/', method='FOO') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 507 | self.check_status_and_reason(response, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 508 | # requests must be case sensitive,so this should fail too |
Terry Jan Reedy | dd09efd | 2014-10-18 17:10:09 -0400 | [diff] [blame] | 509 | response = self.request('/', method='custom') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 510 | self.check_status_and_reason(response, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 511 | response = self.request('/', method='GETs') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 512 | self.check_status_and_reason(response, HTTPStatus.NOT_IMPLEMENTED) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 513 | |
Pierre Quentel | 351adda | 2017-04-02 12:26:12 +0200 | [diff] [blame] | 514 | def test_last_modified(self): |
| 515 | """Checks that the datetime returned in Last-Modified response header |
| 516 | is the actual datetime of last modification, rounded to the second |
| 517 | """ |
| 518 | response = self.request(self.base_url + '/test') |
| 519 | self.check_status_and_reason(response, HTTPStatus.OK, data=self.data) |
| 520 | last_modif_header = response.headers['Last-modified'] |
| 521 | self.assertEqual(last_modif_header, self.last_modif_header) |
| 522 | |
Martin Panter | fc475a9 | 2016-04-09 04:56:10 +0000 | [diff] [blame] | 523 | def test_path_without_leading_slash(self): |
| 524 | response = self.request(self.tempdir_name + '/test') |
| 525 | self.check_status_and_reason(response, HTTPStatus.OK, data=self.data) |
| 526 | response = self.request(self.tempdir_name + '/test/') |
| 527 | self.check_status_and_reason(response, HTTPStatus.NOT_FOUND) |
| 528 | response = self.request(self.tempdir_name + '/') |
| 529 | self.check_status_and_reason(response, HTTPStatus.OK) |
| 530 | response = self.request(self.tempdir_name) |
| 531 | self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY) |
| 532 | response = self.request(self.tempdir_name + '/?hi=2') |
| 533 | self.check_status_and_reason(response, HTTPStatus.OK) |
| 534 | response = self.request(self.tempdir_name + '?hi=1') |
| 535 | self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY) |
| 536 | self.assertEqual(response.getheader("Location"), |
| 537 | self.tempdir_name + "/?hi=1") |
| 538 | |
Martin Panter | da3bb38 | 2016-04-11 00:40:08 +0000 | [diff] [blame] | 539 | def test_html_escape_filename(self): |
| 540 | filename = '<test&>.txt' |
| 541 | fullpath = os.path.join(self.tempdir, filename) |
| 542 | |
| 543 | try: |
| 544 | open(fullpath, 'w').close() |
| 545 | except OSError: |
| 546 | raise unittest.SkipTest('Can not create file %s on current file ' |
| 547 | 'system' % filename) |
| 548 | |
| 549 | try: |
| 550 | response = self.request(self.base_url + '/') |
| 551 | body = self.check_status_and_reason(response, HTTPStatus.OK) |
| 552 | enc = response.headers.get_content_charset() |
| 553 | finally: |
| 554 | os.unlink(fullpath) # avoid affecting test_undecodable_filename |
| 555 | |
| 556 | self.assertIsNotNone(enc) |
| 557 | html_text = '>%s<' % html.escape(filename, quote=False) |
| 558 | self.assertIn(html_text.encode(enc), body) |
| 559 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 560 | |
| 561 | cgi_file1 = """\ |
| 562 | #!%s |
| 563 | |
| 564 | print("Content-type: text/html") |
| 565 | print() |
| 566 | print("Hello World") |
| 567 | """ |
| 568 | |
| 569 | cgi_file2 = """\ |
| 570 | #!%s |
| 571 | import cgi |
| 572 | |
| 573 | print("Content-type: text/html") |
| 574 | print() |
| 575 | |
| 576 | form = cgi.FieldStorage() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 577 | print("%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"), |
| 578 | form.getfirst("bacon"))) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 579 | """ |
| 580 | |
Martin Panter | a02e18a | 2015-10-03 05:38:07 +0000 | [diff] [blame] | 581 | cgi_file4 = """\ |
| 582 | #!%s |
| 583 | import os |
| 584 | |
| 585 | print("Content-type: text/html") |
| 586 | print() |
| 587 | |
| 588 | print(os.environ["%s"]) |
| 589 | """ |
| 590 | |
Senthil Kumaran | da3d2ab | 2020-12-05 05:26:24 -0800 | [diff] [blame] | 591 | cgi_file6 = """\ |
| 592 | #!%s |
| 593 | import os |
| 594 | |
| 595 | print("Content-type: text/plain") |
| 596 | print() |
| 597 | print(repr(os.environ)) |
| 598 | """ |
| 599 | |
Charles-François Natali | f7ed9fc | 2011-11-02 19:35:14 +0100 | [diff] [blame] | 600 | |
| 601 | @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0, |
| 602 | "This test can't be run reliably as root (issue #13308).") |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 603 | class CGIHTTPServerTestCase(BaseTestCase): |
| 604 | class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler): |
| 605 | pass |
| 606 | |
Antoine Pitrou | e768c39 | 2012-08-05 14:52:45 +0200 | [diff] [blame] | 607 | linesep = os.linesep.encode('ascii') |
| 608 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 609 | def setUp(self): |
| 610 | BaseTestCase.setUp(self) |
Victor Stinner | 0b0ca0c | 2010-10-17 19:46:36 +0000 | [diff] [blame] | 611 | self.cwd = os.getcwd() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 612 | self.parent_dir = tempfile.mkdtemp() |
| 613 | self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin') |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 614 | self.cgi_child_dir = os.path.join(self.cgi_dir, 'child-dir') |
Siwon Kang | 91daa9d | 2019-11-22 18:13:05 +0900 | [diff] [blame] | 615 | self.sub_dir_1 = os.path.join(self.parent_dir, 'sub') |
| 616 | self.sub_dir_2 = os.path.join(self.sub_dir_1, 'dir') |
| 617 | self.cgi_dir_in_sub_dir = os.path.join(self.sub_dir_2, 'cgi-bin') |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 618 | os.mkdir(self.cgi_dir) |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 619 | os.mkdir(self.cgi_child_dir) |
Siwon Kang | 91daa9d | 2019-11-22 18:13:05 +0900 | [diff] [blame] | 620 | os.mkdir(self.sub_dir_1) |
| 621 | os.mkdir(self.sub_dir_2) |
| 622 | os.mkdir(self.cgi_dir_in_sub_dir) |
Benjamin Peterson | 35aca89 | 2013-10-30 12:48:59 -0400 | [diff] [blame] | 623 | self.nocgi_path = None |
Victor Stinner | 0b0ca0c | 2010-10-17 19:46:36 +0000 | [diff] [blame] | 624 | self.file1_path = None |
| 625 | self.file2_path = None |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 626 | self.file3_path = None |
Martin Panter | a02e18a | 2015-10-03 05:38:07 +0000 | [diff] [blame] | 627 | self.file4_path = None |
Siwon Kang | 91daa9d | 2019-11-22 18:13:05 +0900 | [diff] [blame] | 628 | self.file5_path = None |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 629 | |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 630 | # The shebang line should be pure ASCII: use symlink if possible. |
| 631 | # See issue #7668. |
Steve Dower | 9048c49 | 2019-06-29 10:34:11 -0700 | [diff] [blame] | 632 | self._pythonexe_symlink = None |
Hai Shi | 79bb2c9 | 2020-08-06 19:51:29 +0800 | [diff] [blame] | 633 | if os_helper.can_symlink(): |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 634 | self.pythonexe = os.path.join(self.parent_dir, 'python') |
Steve Dower | 9048c49 | 2019-06-29 10:34:11 -0700 | [diff] [blame] | 635 | self._pythonexe_symlink = support.PythonSymlink(self.pythonexe).__enter__() |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 636 | else: |
| 637 | self.pythonexe = sys.executable |
| 638 | |
Victor Stinner | 3218c31 | 2010-10-17 20:13:36 +0000 | [diff] [blame] | 639 | try: |
| 640 | # The python executable path is written as the first line of the |
| 641 | # CGI Python script. The encoding cookie cannot be used, and so the |
| 642 | # path should be encodable to the default script encoding (utf-8) |
| 643 | self.pythonexe.encode('utf-8') |
| 644 | except UnicodeEncodeError: |
| 645 | self.tearDown() |
Serhiy Storchaka | 0b4591e | 2013-02-04 15:45:00 +0200 | [diff] [blame] | 646 | self.skipTest("Python executable path is not encodable to utf-8") |
Victor Stinner | 3218c31 | 2010-10-17 20:13:36 +0000 | [diff] [blame] | 647 | |
Benjamin Peterson | 04e9de4 | 2013-10-30 12:43:09 -0400 | [diff] [blame] | 648 | self.nocgi_path = os.path.join(self.parent_dir, 'nocgi.py') |
| 649 | with open(self.nocgi_path, 'w') as fp: |
| 650 | fp.write(cgi_file1 % self.pythonexe) |
| 651 | os.chmod(self.nocgi_path, 0o777) |
| 652 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 653 | self.file1_path = os.path.join(self.cgi_dir, 'file1.py') |
Victor Stinner | 6fb4575 | 2010-10-17 20:17:41 +0000 | [diff] [blame] | 654 | with open(self.file1_path, 'w', encoding='utf-8') as file1: |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 655 | file1.write(cgi_file1 % self.pythonexe) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 656 | os.chmod(self.file1_path, 0o777) |
| 657 | |
| 658 | self.file2_path = os.path.join(self.cgi_dir, 'file2.py') |
Victor Stinner | 6fb4575 | 2010-10-17 20:17:41 +0000 | [diff] [blame] | 659 | with open(self.file2_path, 'w', encoding='utf-8') as file2: |
Florent Xicluna | fd1b093 | 2010-03-28 00:25:02 +0000 | [diff] [blame] | 660 | file2.write(cgi_file2 % self.pythonexe) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 661 | os.chmod(self.file2_path, 0o777) |
| 662 | |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 663 | self.file3_path = os.path.join(self.cgi_child_dir, 'file3.py') |
| 664 | with open(self.file3_path, 'w', encoding='utf-8') as file3: |
| 665 | file3.write(cgi_file1 % self.pythonexe) |
| 666 | os.chmod(self.file3_path, 0o777) |
| 667 | |
Martin Panter | a02e18a | 2015-10-03 05:38:07 +0000 | [diff] [blame] | 668 | self.file4_path = os.path.join(self.cgi_dir, 'file4.py') |
| 669 | with open(self.file4_path, 'w', encoding='utf-8') as file4: |
| 670 | file4.write(cgi_file4 % (self.pythonexe, 'QUERY_STRING')) |
| 671 | os.chmod(self.file4_path, 0o777) |
| 672 | |
Siwon Kang | 91daa9d | 2019-11-22 18:13:05 +0900 | [diff] [blame] | 673 | self.file5_path = os.path.join(self.cgi_dir_in_sub_dir, 'file5.py') |
| 674 | with open(self.file5_path, 'w', encoding='utf-8') as file5: |
| 675 | file5.write(cgi_file1 % self.pythonexe) |
| 676 | os.chmod(self.file5_path, 0o777) |
| 677 | |
Senthil Kumaran | da3d2ab | 2020-12-05 05:26:24 -0800 | [diff] [blame] | 678 | self.file6_path = os.path.join(self.cgi_dir, 'file6.py') |
| 679 | with open(self.file6_path, 'w', encoding='utf-8') as file6: |
| 680 | file6.write(cgi_file6 % self.pythonexe) |
| 681 | os.chmod(self.file6_path, 0o777) |
| 682 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 683 | os.chdir(self.parent_dir) |
| 684 | |
| 685 | def tearDown(self): |
| 686 | try: |
| 687 | os.chdir(self.cwd) |
Steve Dower | 9048c49 | 2019-06-29 10:34:11 -0700 | [diff] [blame] | 688 | if self._pythonexe_symlink: |
| 689 | self._pythonexe_symlink.__exit__(None, None, None) |
Benjamin Peterson | 35aca89 | 2013-10-30 12:48:59 -0400 | [diff] [blame] | 690 | if self.nocgi_path: |
| 691 | os.remove(self.nocgi_path) |
Victor Stinner | 0b0ca0c | 2010-10-17 19:46:36 +0000 | [diff] [blame] | 692 | if self.file1_path: |
| 693 | os.remove(self.file1_path) |
| 694 | if self.file2_path: |
| 695 | os.remove(self.file2_path) |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 696 | if self.file3_path: |
| 697 | os.remove(self.file3_path) |
Martin Panter | a02e18a | 2015-10-03 05:38:07 +0000 | [diff] [blame] | 698 | if self.file4_path: |
| 699 | os.remove(self.file4_path) |
Siwon Kang | 91daa9d | 2019-11-22 18:13:05 +0900 | [diff] [blame] | 700 | if self.file5_path: |
| 701 | os.remove(self.file5_path) |
Senthil Kumaran | da3d2ab | 2020-12-05 05:26:24 -0800 | [diff] [blame] | 702 | if self.file6_path: |
| 703 | os.remove(self.file6_path) |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 704 | os.rmdir(self.cgi_child_dir) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 705 | os.rmdir(self.cgi_dir) |
Siwon Kang | 91daa9d | 2019-11-22 18:13:05 +0900 | [diff] [blame] | 706 | os.rmdir(self.cgi_dir_in_sub_dir) |
| 707 | os.rmdir(self.sub_dir_2) |
| 708 | os.rmdir(self.sub_dir_1) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 709 | os.rmdir(self.parent_dir) |
| 710 | finally: |
| 711 | BaseTestCase.tearDown(self) |
| 712 | |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 713 | def test_url_collapse_path(self): |
| 714 | # verify tail is the last portion and head is the rest on proper urls |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 715 | test_vectors = { |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 716 | '': '//', |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 717 | '..': IndexError, |
| 718 | '/.//..': IndexError, |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 719 | '/': '//', |
| 720 | '//': '//', |
| 721 | '/\\': '//\\', |
| 722 | '/.//': '//', |
| 723 | 'cgi-bin/file1.py': '/cgi-bin/file1.py', |
| 724 | '/cgi-bin/file1.py': '/cgi-bin/file1.py', |
| 725 | 'a': '//a', |
| 726 | '/a': '//a', |
| 727 | '//a': '//a', |
| 728 | './a': '//a', |
| 729 | './C:/': '/C:/', |
| 730 | '/a/b': '/a/b', |
| 731 | '/a/b/': '/a/b/', |
| 732 | '/a/b/.': '/a/b/', |
| 733 | '/a/b/c/..': '/a/b/', |
| 734 | '/a/b/c/../d': '/a/b/d', |
| 735 | '/a/b/c/../d/e/../f': '/a/b/d/f', |
| 736 | '/a/b/c/../d/e/../../f': '/a/b/f', |
| 737 | '/a/b/c/../d/e/.././././..//f': '/a/b/f', |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 738 | '../a/b/c/../d/e/.././././..//f': IndexError, |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 739 | '/a/b/c/../d/e/../../../f': '/a/f', |
| 740 | '/a/b/c/../d/e/../../../../f': '//f', |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 741 | '/a/b/c/../d/e/../../../../../f': IndexError, |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 742 | '/a/b/c/../d/e/../../../../f/..': '//', |
| 743 | '/a/b/c/../d/e/../../../../f/../.': '//', |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 744 | } |
| 745 | for path, expected in test_vectors.items(): |
| 746 | if isinstance(expected, type) and issubclass(expected, Exception): |
| 747 | self.assertRaises(expected, |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 748 | server._url_collapse_path, path) |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 749 | else: |
Senthil Kumaran | d70846b | 2012-04-12 02:34:32 +0800 | [diff] [blame] | 750 | actual = server._url_collapse_path(path) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 751 | self.assertEqual(expected, actual, |
| 752 | msg='path = %r\nGot: %r\nWanted: %r' % |
| 753 | (path, actual, expected)) |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 754 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 755 | def test_headers_and_content(self): |
| 756 | res = self.request('/cgi-bin/file1.py') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 757 | self.assertEqual( |
| 758 | (res.read(), res.getheader('Content-type'), res.status), |
| 759 | (b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 760 | |
Benjamin Peterson | 04e9de4 | 2013-10-30 12:43:09 -0400 | [diff] [blame] | 761 | def test_issue19435(self): |
| 762 | res = self.request('///////////nocgi.py/../cgi-bin/nothere.sh') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 763 | self.assertEqual(res.status, HTTPStatus.NOT_FOUND) |
Benjamin Peterson | 04e9de4 | 2013-10-30 12:43:09 -0400 | [diff] [blame] | 764 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 765 | def test_post(self): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 766 | params = urllib.parse.urlencode( |
| 767 | {'spam' : 1, 'eggs' : 'python', 'bacon' : 123456}) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 768 | headers = {'Content-type' : 'application/x-www-form-urlencoded'} |
| 769 | res = self.request('/cgi-bin/file2.py', 'POST', params, headers) |
| 770 | |
Antoine Pitrou | e768c39 | 2012-08-05 14:52:45 +0200 | [diff] [blame] | 771 | self.assertEqual(res.read(), b'1, python, 123456' + self.linesep) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 772 | |
| 773 | def test_invaliduri(self): |
| 774 | res = self.request('/cgi-bin/invalid') |
| 775 | res.read() |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 776 | self.assertEqual(res.status, HTTPStatus.NOT_FOUND) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 777 | |
| 778 | def test_authorization(self): |
| 779 | headers = {b'Authorization' : b'Basic ' + |
| 780 | base64.b64encode(b'username:pass')} |
| 781 | res = self.request('/cgi-bin/file1.py', 'GET', headers=headers) |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 782 | self.assertEqual( |
| 783 | (b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 784 | (res.read(), res.getheader('Content-type'), res.status)) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 785 | |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 786 | def test_no_leading_slash(self): |
| 787 | # http://bugs.python.org/issue2254 |
| 788 | res = self.request('cgi-bin/file1.py') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 789 | self.assertEqual( |
| 790 | (b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 791 | (res.read(), res.getheader('Content-type'), res.status)) |
Benjamin Peterson | ad71f0f | 2009-04-11 20:12:10 +0000 | [diff] [blame] | 792 | |
Senthil Kumaran | 4271372 | 2010-10-03 17:55:45 +0000 | [diff] [blame] | 793 | def test_os_environ_is_not_altered(self): |
| 794 | signature = "Test CGI Server" |
| 795 | os.environ['SERVER_SOFTWARE'] = signature |
| 796 | res = self.request('/cgi-bin/file1.py') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 797 | self.assertEqual( |
| 798 | (b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 799 | (res.read(), res.getheader('Content-type'), res.status)) |
Senthil Kumaran | 4271372 | 2010-10-03 17:55:45 +0000 | [diff] [blame] | 800 | self.assertEqual(os.environ['SERVER_SOFTWARE'], signature) |
| 801 | |
Benjamin Peterson | 73b8b1c | 2014-06-14 18:36:29 -0700 | [diff] [blame] | 802 | def test_urlquote_decoding_in_cgi_check(self): |
| 803 | res = self.request('/cgi-bin%2ffile1.py') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 804 | self.assertEqual( |
| 805 | (b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 806 | (res.read(), res.getheader('Content-type'), res.status)) |
Benjamin Peterson | 73b8b1c | 2014-06-14 18:36:29 -0700 | [diff] [blame] | 807 | |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 808 | def test_nested_cgi_path_issue21323(self): |
| 809 | res = self.request('/cgi-bin/child-dir/file3.py') |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 810 | self.assertEqual( |
| 811 | (b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 812 | (res.read(), res.getheader('Content-type'), res.status)) |
Ned Deily | 915a30f | 2014-07-12 22:06:26 -0700 | [diff] [blame] | 813 | |
Martin Panter | a02e18a | 2015-10-03 05:38:07 +0000 | [diff] [blame] | 814 | def test_query_with_multiple_question_mark(self): |
| 815 | res = self.request('/cgi-bin/file4.py?a=b?c=d') |
| 816 | self.assertEqual( |
Martin Panter | eb1fee9 | 2015-10-03 06:07:22 +0000 | [diff] [blame] | 817 | (b'a=b?c=d' + self.linesep, 'text/html', HTTPStatus.OK), |
Martin Panter | a02e18a | 2015-10-03 05:38:07 +0000 | [diff] [blame] | 818 | (res.read(), res.getheader('Content-type'), res.status)) |
| 819 | |
Martin Panter | cb29e8c | 2015-10-03 05:55:46 +0000 | [diff] [blame] | 820 | def test_query_with_continuous_slashes(self): |
| 821 | res = self.request('/cgi-bin/file4.py?k=aa%2F%2Fbb&//q//p//=//a//b//') |
| 822 | self.assertEqual( |
| 823 | (b'k=aa%2F%2Fbb&//q//p//=//a//b//' + self.linesep, |
Martin Panter | eb1fee9 | 2015-10-03 06:07:22 +0000 | [diff] [blame] | 824 | 'text/html', HTTPStatus.OK), |
Martin Panter | cb29e8c | 2015-10-03 05:55:46 +0000 | [diff] [blame] | 825 | (res.read(), res.getheader('Content-type'), res.status)) |
| 826 | |
Siwon Kang | 91daa9d | 2019-11-22 18:13:05 +0900 | [diff] [blame] | 827 | def test_cgi_path_in_sub_directories(self): |
Pablo Galindo | 24f5cac | 2019-12-04 09:29:10 +0000 | [diff] [blame] | 828 | try: |
| 829 | CGIHTTPRequestHandler.cgi_directories.append('/sub/dir/cgi-bin') |
| 830 | res = self.request('/sub/dir/cgi-bin/file5.py') |
| 831 | self.assertEqual( |
| 832 | (b'Hello World' + self.linesep, 'text/html', HTTPStatus.OK), |
| 833 | (res.read(), res.getheader('Content-type'), res.status)) |
| 834 | finally: |
| 835 | CGIHTTPRequestHandler.cgi_directories.remove('/sub/dir/cgi-bin') |
| 836 | |
Senthil Kumaran | da3d2ab | 2020-12-05 05:26:24 -0800 | [diff] [blame] | 837 | def test_accept(self): |
| 838 | browser_accept = \ |
| 839 | 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' |
| 840 | tests = ( |
| 841 | ((('Accept', browser_accept),), browser_accept), |
| 842 | ((), ''), |
| 843 | # Hack case to get two values for the one header |
| 844 | ((('Accept', 'text/html'), ('ACCEPT', 'text/plain')), |
| 845 | 'text/html,text/plain'), |
| 846 | ) |
| 847 | for headers, expected in tests: |
| 848 | headers = OrderedDict(headers) |
| 849 | with self.subTest(headers): |
| 850 | res = self.request('/cgi-bin/file6.py', 'GET', headers=headers) |
| 851 | self.assertEqual(http.HTTPStatus.OK, res.status) |
| 852 | expected = f"'HTTP_ACCEPT': {expected!r}" |
| 853 | self.assertIn(expected.encode('ascii'), res.read()) |
Siwon Kang | 91daa9d | 2019-11-22 18:13:05 +0900 | [diff] [blame] | 854 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 855 | |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 856 | class SocketlessRequestHandler(SimpleHTTPRequestHandler): |
Géry Ogam | 781266e | 2019-09-11 15:03:46 +0200 | [diff] [blame] | 857 | def __init__(self, directory=None): |
Stéphane Wirtel | a17a2f5 | 2017-05-24 09:29:06 +0200 | [diff] [blame] | 858 | request = mock.Mock() |
| 859 | request.makefile.return_value = BytesIO() |
Géry Ogam | 781266e | 2019-09-11 15:03:46 +0200 | [diff] [blame] | 860 | super().__init__(request, None, None, directory=directory) |
Stéphane Wirtel | a17a2f5 | 2017-05-24 09:29:06 +0200 | [diff] [blame] | 861 | |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 862 | self.get_called = False |
| 863 | self.protocol_version = "HTTP/1.1" |
| 864 | |
| 865 | def do_GET(self): |
| 866 | self.get_called = True |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 867 | self.send_response(HTTPStatus.OK) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 868 | self.send_header('Content-Type', 'text/html') |
| 869 | self.end_headers() |
| 870 | self.wfile.write(b'<html><body>Data</body></html>\r\n') |
| 871 | |
| 872 | def log_message(self, format, *args): |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 873 | pass |
| 874 | |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 875 | class RejectingSocketlessRequestHandler(SocketlessRequestHandler): |
| 876 | def handle_expect_100(self): |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 877 | self.send_error(HTTPStatus.EXPECTATION_FAILED) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 878 | return False |
| 879 | |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 880 | |
| 881 | class AuditableBytesIO: |
| 882 | |
| 883 | def __init__(self): |
| 884 | self.datas = [] |
| 885 | |
| 886 | def write(self, data): |
| 887 | self.datas.append(data) |
| 888 | |
| 889 | def getData(self): |
| 890 | return b''.join(self.datas) |
| 891 | |
| 892 | @property |
| 893 | def numWrites(self): |
| 894 | return len(self.datas) |
| 895 | |
| 896 | |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 897 | class BaseHTTPRequestHandlerTestCase(unittest.TestCase): |
Ezio Melotti | 3b3499b | 2011-03-16 11:35:38 +0200 | [diff] [blame] | 898 | """Test the functionality of the BaseHTTPServer. |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 899 | |
| 900 | Test the support for the Expect 100-continue header. |
| 901 | """ |
| 902 | |
| 903 | HTTPResponseMatch = re.compile(b'HTTP/1.[0-9]+ 200 OK') |
| 904 | |
| 905 | def setUp (self): |
| 906 | self.handler = SocketlessRequestHandler() |
| 907 | |
| 908 | def send_typical_request(self, message): |
| 909 | input = BytesIO(message) |
| 910 | output = BytesIO() |
| 911 | self.handler.rfile = input |
| 912 | self.handler.wfile = output |
| 913 | self.handler.handle_one_request() |
| 914 | output.seek(0) |
| 915 | return output.readlines() |
| 916 | |
| 917 | def verify_get_called(self): |
| 918 | self.assertTrue(self.handler.get_called) |
| 919 | |
| 920 | def verify_expected_headers(self, headers): |
| 921 | for fieldName in b'Server: ', b'Date: ', b'Content-Type: ': |
| 922 | self.assertEqual(sum(h.startswith(fieldName) for h in headers), 1) |
| 923 | |
| 924 | def verify_http_server_response(self, response): |
| 925 | match = self.HTTPResponseMatch.search(response) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 926 | self.assertIsNotNone(match) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 927 | |
| 928 | def test_http_1_1(self): |
| 929 | result = self.send_typical_request(b'GET / HTTP/1.1\r\n\r\n') |
| 930 | self.verify_http_server_response(result[0]) |
| 931 | self.verify_expected_headers(result[1:-1]) |
| 932 | self.verify_get_called() |
| 933 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
Benjamin Peterson | 70e2847 | 2015-02-17 21:11:10 -0500 | [diff] [blame] | 934 | self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1') |
| 935 | self.assertEqual(self.handler.command, 'GET') |
| 936 | self.assertEqual(self.handler.path, '/') |
| 937 | self.assertEqual(self.handler.request_version, 'HTTP/1.1') |
| 938 | self.assertSequenceEqual(self.handler.headers.items(), ()) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 939 | |
| 940 | def test_http_1_0(self): |
| 941 | result = self.send_typical_request(b'GET / HTTP/1.0\r\n\r\n') |
| 942 | self.verify_http_server_response(result[0]) |
| 943 | self.verify_expected_headers(result[1:-1]) |
| 944 | self.verify_get_called() |
| 945 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
Benjamin Peterson | 70e2847 | 2015-02-17 21:11:10 -0500 | [diff] [blame] | 946 | self.assertEqual(self.handler.requestline, 'GET / HTTP/1.0') |
| 947 | self.assertEqual(self.handler.command, 'GET') |
| 948 | self.assertEqual(self.handler.path, '/') |
| 949 | self.assertEqual(self.handler.request_version, 'HTTP/1.0') |
| 950 | self.assertSequenceEqual(self.handler.headers.items(), ()) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 951 | |
| 952 | def test_http_0_9(self): |
| 953 | result = self.send_typical_request(b'GET / HTTP/0.9\r\n\r\n') |
| 954 | self.assertEqual(len(result), 1) |
| 955 | self.assertEqual(result[0], b'<html><body>Data</body></html>\r\n') |
| 956 | self.verify_get_called() |
| 957 | |
Martin Panter | e82338d | 2016-11-19 01:06:37 +0000 | [diff] [blame] | 958 | def test_extra_space(self): |
| 959 | result = self.send_typical_request( |
| 960 | b'GET /spaced out HTTP/1.1\r\n' |
| 961 | b'Host: dummy\r\n' |
| 962 | b'\r\n' |
| 963 | ) |
| 964 | self.assertTrue(result[0].startswith(b'HTTP/1.1 400 ')) |
| 965 | self.verify_expected_headers(result[1:result.index(b'\r\n')]) |
| 966 | self.assertFalse(self.handler.get_called) |
| 967 | |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 968 | def test_with_continue_1_0(self): |
| 969 | result = self.send_typical_request(b'GET / HTTP/1.0\r\nExpect: 100-continue\r\n\r\n') |
| 970 | self.verify_http_server_response(result[0]) |
| 971 | self.verify_expected_headers(result[1:-1]) |
| 972 | self.verify_get_called() |
| 973 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
Benjamin Peterson | 70e2847 | 2015-02-17 21:11:10 -0500 | [diff] [blame] | 974 | self.assertEqual(self.handler.requestline, 'GET / HTTP/1.0') |
| 975 | self.assertEqual(self.handler.command, 'GET') |
| 976 | self.assertEqual(self.handler.path, '/') |
| 977 | self.assertEqual(self.handler.request_version, 'HTTP/1.0') |
| 978 | headers = (("Expect", "100-continue"),) |
| 979 | self.assertSequenceEqual(self.handler.headers.items(), headers) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 980 | |
| 981 | def test_with_continue_1_1(self): |
| 982 | result = self.send_typical_request(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n') |
| 983 | self.assertEqual(result[0], b'HTTP/1.1 100 Continue\r\n') |
Benjamin Peterson | 0442423 | 2014-01-18 21:50:18 -0500 | [diff] [blame] | 984 | self.assertEqual(result[1], b'\r\n') |
| 985 | self.assertEqual(result[2], b'HTTP/1.1 200 OK\r\n') |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 986 | self.verify_expected_headers(result[2:-1]) |
| 987 | self.verify_get_called() |
| 988 | self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
Benjamin Peterson | 70e2847 | 2015-02-17 21:11:10 -0500 | [diff] [blame] | 989 | self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1') |
| 990 | self.assertEqual(self.handler.command, 'GET') |
| 991 | self.assertEqual(self.handler.path, '/') |
| 992 | self.assertEqual(self.handler.request_version, 'HTTP/1.1') |
| 993 | headers = (("Expect", "100-continue"),) |
| 994 | self.assertSequenceEqual(self.handler.headers.items(), headers) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 995 | |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 996 | def test_header_buffering_of_send_error(self): |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 997 | |
| 998 | input = BytesIO(b'GET / HTTP/1.1\r\n\r\n') |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 999 | output = AuditableBytesIO() |
| 1000 | handler = SocketlessRequestHandler() |
| 1001 | handler.rfile = input |
| 1002 | handler.wfile = output |
| 1003 | handler.request_version = 'HTTP/1.1' |
| 1004 | handler.requestline = '' |
| 1005 | handler.command = None |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 1006 | |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 1007 | handler.send_error(418) |
| 1008 | self.assertEqual(output.numWrites, 2) |
| 1009 | |
| 1010 | def test_header_buffering_of_send_response_only(self): |
| 1011 | |
| 1012 | input = BytesIO(b'GET / HTTP/1.1\r\n\r\n') |
| 1013 | output = AuditableBytesIO() |
| 1014 | handler = SocketlessRequestHandler() |
| 1015 | handler.rfile = input |
| 1016 | handler.wfile = output |
| 1017 | handler.request_version = 'HTTP/1.1' |
| 1018 | |
| 1019 | handler.send_response_only(418) |
| 1020 | self.assertEqual(output.numWrites, 0) |
| 1021 | handler.end_headers() |
| 1022 | self.assertEqual(output.numWrites, 1) |
| 1023 | |
| 1024 | def test_header_buffering_of_send_header(self): |
| 1025 | |
| 1026 | input = BytesIO(b'GET / HTTP/1.1\r\n\r\n') |
| 1027 | output = AuditableBytesIO() |
| 1028 | handler = SocketlessRequestHandler() |
| 1029 | handler.rfile = input |
| 1030 | handler.wfile = output |
| 1031 | handler.request_version = 'HTTP/1.1' |
| 1032 | |
| 1033 | handler.send_header('Foo', 'foo') |
| 1034 | handler.send_header('bar', 'bar') |
| 1035 | self.assertEqual(output.numWrites, 0) |
| 1036 | handler.end_headers() |
| 1037 | self.assertEqual(output.getData(), b'Foo: foo\r\nbar: bar\r\n\r\n') |
| 1038 | self.assertEqual(output.numWrites, 1) |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 1039 | |
| 1040 | def test_header_unbuffered_when_continue(self): |
| 1041 | |
| 1042 | def _readAndReseek(f): |
| 1043 | pos = f.tell() |
| 1044 | f.seek(0) |
| 1045 | data = f.read() |
| 1046 | f.seek(pos) |
| 1047 | return data |
| 1048 | |
| 1049 | input = BytesIO(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n') |
| 1050 | output = BytesIO() |
| 1051 | self.handler.rfile = input |
| 1052 | self.handler.wfile = output |
| 1053 | self.handler.request_version = 'HTTP/1.1' |
| 1054 | |
| 1055 | self.handler.handle_one_request() |
| 1056 | self.assertNotEqual(_readAndReseek(output), b'') |
| 1057 | result = _readAndReseek(output).split(b'\r\n') |
| 1058 | self.assertEqual(result[0], b'HTTP/1.1 100 Continue') |
Benjamin Peterson | 0442423 | 2014-01-18 21:50:18 -0500 | [diff] [blame] | 1059 | self.assertEqual(result[1], b'') |
| 1060 | self.assertEqual(result[2], b'HTTP/1.1 200 OK') |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 1061 | |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 1062 | def test_with_continue_rejected(self): |
| 1063 | usual_handler = self.handler # Save to avoid breaking any subsequent tests. |
| 1064 | self.handler = RejectingSocketlessRequestHandler() |
| 1065 | result = self.send_typical_request(b'GET / HTTP/1.1\r\nExpect: 100-continue\r\n\r\n') |
| 1066 | self.assertEqual(result[0], b'HTTP/1.1 417 Expectation Failed\r\n') |
| 1067 | self.verify_expected_headers(result[1:-1]) |
| 1068 | # The expect handler should short circuit the usual get method by |
| 1069 | # returning false here, so get_called should be false |
| 1070 | self.assertFalse(self.handler.get_called) |
| 1071 | self.assertEqual(sum(r == b'Connection: close\r\n' for r in result[1:-1]), 1) |
| 1072 | self.handler = usual_handler # Restore to avoid breaking any subsequent tests. |
| 1073 | |
Antoine Pitrou | c492437 | 2010-12-16 16:48:36 +0000 | [diff] [blame] | 1074 | def test_request_length(self): |
| 1075 | # Issue #10714: huge request lines are discarded, to avoid Denial |
| 1076 | # of Service attacks. |
| 1077 | result = self.send_typical_request(b'GET ' + b'x' * 65537) |
| 1078 | self.assertEqual(result[0], b'HTTP/1.1 414 Request-URI Too Long\r\n') |
| 1079 | self.assertFalse(self.handler.get_called) |
Benjamin Peterson | 70e2847 | 2015-02-17 21:11:10 -0500 | [diff] [blame] | 1080 | self.assertIsInstance(self.handler.requestline, str) |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 1081 | |
Senthil Kumaran | 5466bf1 | 2010-12-18 16:55:23 +0000 | [diff] [blame] | 1082 | def test_header_length(self): |
| 1083 | # Issue #6791: same for headers |
| 1084 | result = self.send_typical_request( |
| 1085 | b'GET / HTTP/1.1\r\nX-Foo: bar' + b'r' * 65537 + b'\r\n\r\n') |
Martin Panter | 50badad | 2016-04-03 01:28:53 +0000 | [diff] [blame] | 1086 | self.assertEqual(result[0], b'HTTP/1.1 431 Line too long\r\n') |
Senthil Kumaran | 5466bf1 | 2010-12-18 16:55:23 +0000 | [diff] [blame] | 1087 | self.assertFalse(self.handler.get_called) |
Benjamin Peterson | 70e2847 | 2015-02-17 21:11:10 -0500 | [diff] [blame] | 1088 | self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1') |
| 1089 | |
Martin Panter | acc0319 | 2016-04-03 00:45:46 +0000 | [diff] [blame] | 1090 | def test_too_many_headers(self): |
| 1091 | result = self.send_typical_request( |
| 1092 | b'GET / HTTP/1.1\r\n' + b'X-Foo: bar\r\n' * 101 + b'\r\n') |
| 1093 | self.assertEqual(result[0], b'HTTP/1.1 431 Too many headers\r\n') |
| 1094 | self.assertFalse(self.handler.get_called) |
| 1095 | self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1') |
| 1096 | |
Martin Panter | da3bb38 | 2016-04-11 00:40:08 +0000 | [diff] [blame] | 1097 | def test_html_escape_on_error(self): |
| 1098 | result = self.send_typical_request( |
| 1099 | b'<script>alert("hello")</script> / HTTP/1.1') |
| 1100 | result = b''.join(result) |
| 1101 | text = '<script>alert("hello")</script>' |
| 1102 | self.assertIn(html.escape(text, quote=False).encode('ascii'), result) |
| 1103 | |
Benjamin Peterson | 70e2847 | 2015-02-17 21:11:10 -0500 | [diff] [blame] | 1104 | def test_close_connection(self): |
| 1105 | # handle_one_request() should be repeatedly called until |
| 1106 | # it sets close_connection |
| 1107 | def handle_one_request(): |
| 1108 | self.handler.close_connection = next(close_values) |
| 1109 | self.handler.handle_one_request = handle_one_request |
| 1110 | |
| 1111 | close_values = iter((True,)) |
| 1112 | self.handler.handle() |
| 1113 | self.assertRaises(StopIteration, next, close_values) |
| 1114 | |
| 1115 | close_values = iter((False, False, True)) |
| 1116 | self.handler.handle() |
| 1117 | self.assertRaises(StopIteration, next, close_values) |
Senthil Kumaran | 5466bf1 | 2010-12-18 16:55:23 +0000 | [diff] [blame] | 1118 | |
Berker Peksag | 04bc5b9 | 2016-03-14 06:06:03 +0200 | [diff] [blame] | 1119 | def test_date_time_string(self): |
| 1120 | now = time.time() |
| 1121 | # this is the old code that formats the timestamp |
| 1122 | year, month, day, hh, mm, ss, wd, y, z = time.gmtime(now) |
| 1123 | expected = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % ( |
| 1124 | self.handler.weekdayname[wd], |
| 1125 | day, |
| 1126 | self.handler.monthname[month], |
| 1127 | year, hh, mm, ss |
| 1128 | ) |
| 1129 | self.assertEqual(self.handler.date_time_string(timestamp=now), expected) |
| 1130 | |
| 1131 | |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 1132 | class SimpleHTTPRequestHandlerTestCase(unittest.TestCase): |
| 1133 | """ Test url parsing """ |
| 1134 | def setUp(self): |
Géry Ogam | 781266e | 2019-09-11 15:03:46 +0200 | [diff] [blame] | 1135 | self.translated_1 = os.path.join(os.getcwd(), 'filename') |
| 1136 | self.translated_2 = os.path.join('foo', 'filename') |
| 1137 | self.translated_3 = os.path.join('bar', 'filename') |
| 1138 | self.handler_1 = SocketlessRequestHandler() |
| 1139 | self.handler_2 = SocketlessRequestHandler(directory='foo') |
| 1140 | self.handler_3 = SocketlessRequestHandler(directory=pathlib.PurePath('bar')) |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 1141 | |
| 1142 | def test_query_arguments(self): |
Géry Ogam | 781266e | 2019-09-11 15:03:46 +0200 | [diff] [blame] | 1143 | path = self.handler_1.translate_path('/filename') |
| 1144 | self.assertEqual(path, self.translated_1) |
| 1145 | path = self.handler_2.translate_path('/filename') |
| 1146 | self.assertEqual(path, self.translated_2) |
| 1147 | path = self.handler_3.translate_path('/filename') |
| 1148 | self.assertEqual(path, self.translated_3) |
| 1149 | |
| 1150 | path = self.handler_1.translate_path('/filename?foo=bar') |
| 1151 | self.assertEqual(path, self.translated_1) |
| 1152 | path = self.handler_2.translate_path('/filename?foo=bar') |
| 1153 | self.assertEqual(path, self.translated_2) |
| 1154 | path = self.handler_3.translate_path('/filename?foo=bar') |
| 1155 | self.assertEqual(path, self.translated_3) |
| 1156 | |
| 1157 | path = self.handler_1.translate_path('/filename?a=b&spam=eggs#zot') |
| 1158 | self.assertEqual(path, self.translated_1) |
| 1159 | path = self.handler_2.translate_path('/filename?a=b&spam=eggs#zot') |
| 1160 | self.assertEqual(path, self.translated_2) |
| 1161 | path = self.handler_3.translate_path('/filename?a=b&spam=eggs#zot') |
| 1162 | self.assertEqual(path, self.translated_3) |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 1163 | |
| 1164 | def test_start_with_double_slash(self): |
Géry Ogam | 781266e | 2019-09-11 15:03:46 +0200 | [diff] [blame] | 1165 | path = self.handler_1.translate_path('//filename') |
| 1166 | self.assertEqual(path, self.translated_1) |
| 1167 | path = self.handler_2.translate_path('//filename') |
| 1168 | self.assertEqual(path, self.translated_2) |
| 1169 | path = self.handler_3.translate_path('//filename') |
| 1170 | self.assertEqual(path, self.translated_3) |
| 1171 | |
| 1172 | path = self.handler_1.translate_path('//filename?foo=bar') |
| 1173 | self.assertEqual(path, self.translated_1) |
| 1174 | path = self.handler_2.translate_path('//filename?foo=bar') |
| 1175 | self.assertEqual(path, self.translated_2) |
| 1176 | path = self.handler_3.translate_path('//filename?foo=bar') |
| 1177 | self.assertEqual(path, self.translated_3) |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 1178 | |
Martin Panter | d274b3f | 2016-04-18 03:45:18 +0000 | [diff] [blame] | 1179 | def test_windows_colon(self): |
| 1180 | with support.swap_attr(server.os, 'path', ntpath): |
Géry Ogam | 781266e | 2019-09-11 15:03:46 +0200 | [diff] [blame] | 1181 | path = self.handler_1.translate_path('c:c:c:foo/filename') |
Martin Panter | d274b3f | 2016-04-18 03:45:18 +0000 | [diff] [blame] | 1182 | path = path.replace(ntpath.sep, os.sep) |
Géry Ogam | 781266e | 2019-09-11 15:03:46 +0200 | [diff] [blame] | 1183 | self.assertEqual(path, self.translated_1) |
| 1184 | path = self.handler_2.translate_path('c:c:c:foo/filename') |
| 1185 | path = path.replace(ntpath.sep, os.sep) |
| 1186 | self.assertEqual(path, self.translated_2) |
| 1187 | path = self.handler_3.translate_path('c:c:c:foo/filename') |
| 1188 | path = path.replace(ntpath.sep, os.sep) |
| 1189 | self.assertEqual(path, self.translated_3) |
Martin Panter | d274b3f | 2016-04-18 03:45:18 +0000 | [diff] [blame] | 1190 | |
Géry Ogam | 781266e | 2019-09-11 15:03:46 +0200 | [diff] [blame] | 1191 | path = self.handler_1.translate_path('\\c:../filename') |
Martin Panter | d274b3f | 2016-04-18 03:45:18 +0000 | [diff] [blame] | 1192 | path = path.replace(ntpath.sep, os.sep) |
Géry Ogam | 781266e | 2019-09-11 15:03:46 +0200 | [diff] [blame] | 1193 | self.assertEqual(path, self.translated_1) |
| 1194 | path = self.handler_2.translate_path('\\c:../filename') |
| 1195 | path = path.replace(ntpath.sep, os.sep) |
| 1196 | self.assertEqual(path, self.translated_2) |
| 1197 | path = self.handler_3.translate_path('\\c:../filename') |
| 1198 | path = path.replace(ntpath.sep, os.sep) |
| 1199 | self.assertEqual(path, self.translated_3) |
Martin Panter | d274b3f | 2016-04-18 03:45:18 +0000 | [diff] [blame] | 1200 | |
Géry Ogam | 781266e | 2019-09-11 15:03:46 +0200 | [diff] [blame] | 1201 | path = self.handler_1.translate_path('c:\\c:..\\foo/filename') |
Martin Panter | d274b3f | 2016-04-18 03:45:18 +0000 | [diff] [blame] | 1202 | path = path.replace(ntpath.sep, os.sep) |
Géry Ogam | 781266e | 2019-09-11 15:03:46 +0200 | [diff] [blame] | 1203 | self.assertEqual(path, self.translated_1) |
| 1204 | path = self.handler_2.translate_path('c:\\c:..\\foo/filename') |
| 1205 | path = path.replace(ntpath.sep, os.sep) |
| 1206 | self.assertEqual(path, self.translated_2) |
| 1207 | path = self.handler_3.translate_path('c:\\c:..\\foo/filename') |
| 1208 | path = path.replace(ntpath.sep, os.sep) |
| 1209 | self.assertEqual(path, self.translated_3) |
Martin Panter | d274b3f | 2016-04-18 03:45:18 +0000 | [diff] [blame] | 1210 | |
Géry Ogam | 781266e | 2019-09-11 15:03:46 +0200 | [diff] [blame] | 1211 | path = self.handler_1.translate_path('c:c:foo\\c:c:bar/filename') |
Martin Panter | d274b3f | 2016-04-18 03:45:18 +0000 | [diff] [blame] | 1212 | path = path.replace(ntpath.sep, os.sep) |
Géry Ogam | 781266e | 2019-09-11 15:03:46 +0200 | [diff] [blame] | 1213 | self.assertEqual(path, self.translated_1) |
| 1214 | path = self.handler_2.translate_path('c:c:foo\\c:c:bar/filename') |
| 1215 | path = path.replace(ntpath.sep, os.sep) |
| 1216 | self.assertEqual(path, self.translated_2) |
| 1217 | path = self.handler_3.translate_path('c:c:foo\\c:c:bar/filename') |
| 1218 | path = path.replace(ntpath.sep, os.sep) |
| 1219 | self.assertEqual(path, self.translated_3) |
Martin Panter | d274b3f | 2016-04-18 03:45:18 +0000 | [diff] [blame] | 1220 | |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 1221 | |
Berker Peksag | 366c570 | 2015-02-13 20:48:15 +0200 | [diff] [blame] | 1222 | class MiscTestCase(unittest.TestCase): |
| 1223 | def test_all(self): |
| 1224 | expected = [] |
Victor Stinner | fabd7bb | 2020-08-11 15:26:59 +0200 | [diff] [blame] | 1225 | denylist = {'executable', 'nobody_uid', 'test'} |
Berker Peksag | 366c570 | 2015-02-13 20:48:15 +0200 | [diff] [blame] | 1226 | for name in dir(server): |
Victor Stinner | fabd7bb | 2020-08-11 15:26:59 +0200 | [diff] [blame] | 1227 | if name.startswith('_') or name in denylist: |
Berker Peksag | 366c570 | 2015-02-13 20:48:15 +0200 | [diff] [blame] | 1228 | continue |
| 1229 | module_object = getattr(server, name) |
| 1230 | if getattr(module_object, '__module__', None) == 'http.server': |
| 1231 | expected.append(name) |
| 1232 | self.assertCountEqual(server.__all__, expected) |
| 1233 | |
| 1234 | |
Lisa Roach | 433433f | 2018-11-26 10:43:38 -0800 | [diff] [blame] | 1235 | class ScriptTestCase(unittest.TestCase): |
Jason R. Coombs | f289084 | 2019-02-07 08:22:45 -0500 | [diff] [blame] | 1236 | |
| 1237 | def mock_server_class(self): |
| 1238 | return mock.MagicMock( |
| 1239 | return_value=mock.MagicMock( |
| 1240 | __enter__=mock.MagicMock( |
| 1241 | return_value=mock.MagicMock( |
| 1242 | socket=mock.MagicMock( |
| 1243 | getsockname=lambda: ('', 0), |
| 1244 | ), |
| 1245 | ), |
| 1246 | ), |
| 1247 | ), |
| 1248 | ) |
| 1249 | |
| 1250 | @mock.patch('builtins.print') |
| 1251 | def test_server_test_unspec(self, _): |
| 1252 | mock_server = self.mock_server_class() |
| 1253 | server.test(ServerClass=mock_server, bind=None) |
| 1254 | self.assertIn( |
| 1255 | mock_server.address_family, |
| 1256 | (socket.AF_INET6, socket.AF_INET), |
| 1257 | ) |
| 1258 | |
| 1259 | @mock.patch('builtins.print') |
| 1260 | def test_server_test_localhost(self, _): |
| 1261 | mock_server = self.mock_server_class() |
| 1262 | server.test(ServerClass=mock_server, bind="localhost") |
| 1263 | self.assertIn( |
| 1264 | mock_server.address_family, |
| 1265 | (socket.AF_INET6, socket.AF_INET), |
| 1266 | ) |
| 1267 | |
| 1268 | ipv6_addrs = ( |
| 1269 | "::", |
| 1270 | "2001:0db8:85a3:0000:0000:8a2e:0370:7334", |
| 1271 | "::1", |
| 1272 | ) |
| 1273 | |
| 1274 | ipv4_addrs = ( |
| 1275 | "0.0.0.0", |
| 1276 | "8.8.8.8", |
| 1277 | "127.0.0.1", |
| 1278 | ) |
| 1279 | |
Lisa Roach | 433433f | 2018-11-26 10:43:38 -0800 | [diff] [blame] | 1280 | @mock.patch('builtins.print') |
| 1281 | def test_server_test_ipv6(self, _): |
Jason R. Coombs | f289084 | 2019-02-07 08:22:45 -0500 | [diff] [blame] | 1282 | for bind in self.ipv6_addrs: |
| 1283 | mock_server = self.mock_server_class() |
| 1284 | server.test(ServerClass=mock_server, bind=bind) |
| 1285 | self.assertEqual(mock_server.address_family, socket.AF_INET6) |
Lisa Roach | 433433f | 2018-11-26 10:43:38 -0800 | [diff] [blame] | 1286 | |
Jason R. Coombs | f289084 | 2019-02-07 08:22:45 -0500 | [diff] [blame] | 1287 | @mock.patch('builtins.print') |
| 1288 | def test_server_test_ipv4(self, _): |
| 1289 | for bind in self.ipv4_addrs: |
| 1290 | mock_server = self.mock_server_class() |
| 1291 | server.test(ServerClass=mock_server, bind=bind) |
| 1292 | self.assertEqual(mock_server.address_family, socket.AF_INET) |
Lisa Roach | 433433f | 2018-11-26 10:43:38 -0800 | [diff] [blame] | 1293 | |
| 1294 | |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1295 | def test_main(verbose=None): |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 1296 | cwd = os.getcwd() |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1297 | try: |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 1298 | support.run_unittest( |
Serhiy Storchaka | c0a23e6 | 2015-03-07 11:51:37 +0200 | [diff] [blame] | 1299 | RequestHandlerLoggingTestCase, |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 1300 | BaseHTTPRequestHandlerTestCase, |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 1301 | BaseHTTPServerTestCase, |
| 1302 | SimpleHTTPServerTestCase, |
| 1303 | CGIHTTPServerTestCase, |
| 1304 | SimpleHTTPRequestHandlerTestCase, |
Berker Peksag | 366c570 | 2015-02-13 20:48:15 +0200 | [diff] [blame] | 1305 | MiscTestCase, |
Lisa Roach | 433433f | 2018-11-26 10:43:38 -0800 | [diff] [blame] | 1306 | ScriptTestCase |
Georg Brandl | 6fcac0d | 2010-08-02 18:56:54 +0000 | [diff] [blame] | 1307 | ) |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 1308 | finally: |
| 1309 | os.chdir(cwd) |
| 1310 | |
| 1311 | if __name__ == '__main__': |
| 1312 | test_main() |