Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_httpservers.py @ 66973:2c429d8c5ee2 2.7
Merged revisions 87317 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r87317 | antoine.pitrou | 2010-12-16 17:48:36 +0100 (jeu., 16 déc. 2010) | 4 lines
Issue #10714: Limit length of incoming request in http.server to 65536 bytes
for security reasons. Initial patch by Ross Lagerwall.
........
(also backported some tests)
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 16 Dec 2010 17:11:34 +0000 |
| parents | 2e1ad7121e29 |
| children | 4d2b428d7882 |
| rev | line source |
|---|---|
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
1 """Unittests for the various HTTPServer modules. |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
2 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
3 Written by Cody A.W. Somerville <cody-somerville@ubuntu.com>, |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
4 Josip Dzolonga, and Michael Otteneder for the 2007/08 GHOP contest. |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
5 """ |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
6 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
7 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
8 from SimpleHTTPServer import SimpleHTTPRequestHandler |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
9 from CGIHTTPServer import CGIHTTPRequestHandler |
|
52929
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
10 import CGIHTTPServer |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
11 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
12 import os |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
13 import sys |
|
65030
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
14 import re |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
15 import base64 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
16 import shutil |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
17 import urllib |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
18 import httplib |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
19 import tempfile |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
20 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
21 import unittest |
|
65030
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
22 |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
23 from StringIO import StringIO |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
24 |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
25 from test import test_support |
|
61046
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60961
diff
changeset
|
26 threading = test_support.import_module('threading') |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
27 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
28 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
29 class NoLogRequestHandler: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
30 def log_message(self, *args): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
31 # don't write log messages to stderr |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
32 pass |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
33 |
|
65030
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
34 class SocketlessRequestHandler(BaseHTTPRequestHandler): |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
35 def __init__(self): |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
36 self.get_called = False |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
37 self.protocol_version = "HTTP/1.1" |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
38 |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
39 def do_GET(self): |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
40 self.get_called = True |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
41 self.send_response(200) |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
42 self.send_header('Content-Type', 'text/html') |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
43 self.end_headers() |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
44 self.wfile.write('<html><body>Data</body></html>\r\n') |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
45 |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
46 def log_message(self, format, *args): |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
47 pass |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
48 |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
49 |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
50 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
51 class TestServerThread(threading.Thread): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
52 def __init__(self, test_object, request_handler): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
53 threading.Thread.__init__(self) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
54 self.request_handler = request_handler |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
55 self.test_object = test_object |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
56 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
57 def run(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
58 self.server = HTTPServer(('', 0), self.request_handler) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
59 self.test_object.PORT = self.server.socket.getsockname()[1] |
|
60961
5ac0aae28f4c
Replace a Lock with a better suited Event.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59914
diff
changeset
|
60 self.test_object.server_started.set() |
|
5ac0aae28f4c
Replace a Lock with a better suited Event.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59914
diff
changeset
|
61 self.test_object = None |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
62 try: |
|
60961
5ac0aae28f4c
Replace a Lock with a better suited Event.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59914
diff
changeset
|
63 self.server.serve_forever(0.05) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
64 finally: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
65 self.server.server_close() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
66 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
67 def stop(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
68 self.server.shutdown() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
69 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
70 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
71 class BaseTestCase(unittest.TestCase): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
72 def setUp(self): |
|
56870
269f1d9c86cd
(Hopefully) suppress transient refleaks in test_httpservers.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56565
diff
changeset
|
73 self._threads = test_support.threading_setup() |
|
56564
81433a676a8b
Revert changes made to environment in test_httpservers
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
74 os.environ = test_support.EnvironmentVarGuard() |
|
60961
5ac0aae28f4c
Replace a Lock with a better suited Event.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59914
diff
changeset
|
75 self.server_started = threading.Event() |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
76 self.thread = TestServerThread(self, self.request_handler) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
77 self.thread.start() |
|
60961
5ac0aae28f4c
Replace a Lock with a better suited Event.
Antoine Pitrou <solipsis@pitrou.net>
parents:
59914
diff
changeset
|
78 self.server_started.wait() |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
79 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
80 def tearDown(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
81 self.thread.stop() |
|
56564
81433a676a8b
Revert changes made to environment in test_httpservers
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
82 os.environ.__exit__() |
|
56870
269f1d9c86cd
(Hopefully) suppress transient refleaks in test_httpservers.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56565
diff
changeset
|
83 test_support.threading_cleanup(*self._threads) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
84 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
85 def request(self, uri, method='GET', body=None, headers={}): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
86 self.connection = httplib.HTTPConnection('localhost', self.PORT) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
87 self.connection.request(method, uri, body, headers) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
88 return self.connection.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
89 |
|
65030
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
90 class BaseHTTPRequestHandlerTestCase(unittest.TestCase): |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
91 """Test the functionaility of the BaseHTTPServer focussing on |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
92 BaseHTTPRequestHandler. |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
93 """ |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
94 |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
95 HTTPResponseMatch = re.compile('HTTP/1.[0-9]+ 200 OK') |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
96 |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
97 def setUp (self): |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
98 self.handler = SocketlessRequestHandler() |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
99 |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
100 def send_typical_request(self, message): |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
101 input = StringIO(message) |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
102 output = StringIO() |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
103 self.handler.rfile = input |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
104 self.handler.wfile = output |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
105 self.handler.handle_one_request() |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
106 output.seek(0) |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
107 return output.readlines() |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
108 |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
109 def verify_get_called(self): |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
110 self.assertTrue(self.handler.get_called) |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
111 |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
112 def verify_expected_headers(self, headers): |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
113 for fieldName in 'Server: ', 'Date: ', 'Content-Type: ': |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
114 self.assertEqual(sum(h.startswith(fieldName) for h in headers), 1) |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
115 |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
116 def verify_http_server_response(self, response): |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
117 match = self.HTTPResponseMatch.search(response) |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
118 self.assertTrue(match is not None) |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
119 |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
120 def test_http_1_1(self): |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
121 result = self.send_typical_request('GET / HTTP/1.1\r\n\r\n') |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
122 self.verify_http_server_response(result[0]) |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
123 self.verify_expected_headers(result[1:-1]) |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
124 self.verify_get_called() |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
125 self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n') |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
126 |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
127 def test_http_1_0(self): |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
128 result = self.send_typical_request('GET / HTTP/1.0\r\n\r\n') |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
129 self.verify_http_server_response(result[0]) |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
130 self.verify_expected_headers(result[1:-1]) |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
131 self.verify_get_called() |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
132 self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n') |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
133 |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
134 def test_http_0_9(self): |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
135 result = self.send_typical_request('GET / HTTP/0.9\r\n\r\n') |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
136 self.assertEqual(len(result), 1) |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
137 self.assertEqual(result[0], '<html><body>Data</body></html>\r\n') |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
138 self.verify_get_called() |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
139 |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
140 def test_with_continue_1_0(self): |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
141 result = self.send_typical_request('GET / HTTP/1.0\r\nExpect: 100-continue\r\n\r\n') |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
142 self.verify_http_server_response(result[0]) |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
143 self.verify_expected_headers(result[1:-1]) |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
144 self.verify_get_called() |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
145 self.assertEqual(result[-1], '<html><body>Data</body></html>\r\n') |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
146 |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
147 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
148 class BaseHTTPServerTestCase(BaseTestCase): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
149 class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
150 protocol_version = 'HTTP/1.1' |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
151 default_request_version = 'HTTP/1.1' |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
152 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
153 def do_TEST(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
154 self.send_response(204) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
155 self.send_header('Content-Type', 'text/html') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
156 self.send_header('Connection', 'close') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
157 self.end_headers() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
158 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
159 def do_KEEP(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
160 self.send_response(204) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
161 self.send_header('Content-Type', 'text/html') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
162 self.send_header('Connection', 'keep-alive') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
163 self.end_headers() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
164 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
165 def do_KEYERROR(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
166 self.send_error(999) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
167 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
168 def do_CUSTOM(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
169 self.send_response(999) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
170 self.send_header('Content-Type', 'text/html') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
171 self.send_header('Connection', 'close') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
172 self.end_headers() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
173 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
174 def setUp(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
175 BaseTestCase.setUp(self) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
176 self.con = httplib.HTTPConnection('localhost', self.PORT) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
177 self.con.connect() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
178 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
179 def test_command(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
180 self.con.request('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
181 res = self.con.getresponse() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
182 self.assertEqual(res.status, 501) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
183 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
184 def test_request_line_trimming(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
185 self.con._http_vsn_str = 'HTTP/1.1\n' |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
186 self.con.putrequest('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
187 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
188 res = self.con.getresponse() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
189 self.assertEqual(res.status, 501) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
190 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
191 def test_version_bogus(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
192 self.con._http_vsn_str = 'FUBAR' |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
193 self.con.putrequest('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
194 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
195 res = self.con.getresponse() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
196 self.assertEqual(res.status, 400) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
197 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
198 def test_version_digits(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
199 self.con._http_vsn_str = 'HTTP/9.9.9' |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
200 self.con.putrequest('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
201 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
202 res = self.con.getresponse() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
203 self.assertEqual(res.status, 400) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
204 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
205 def test_version_none_get(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
206 self.con._http_vsn_str = '' |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
207 self.con.putrequest('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
208 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
209 res = self.con.getresponse() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
210 self.assertEqual(res.status, 501) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
211 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
212 def test_version_none(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
213 self.con._http_vsn_str = '' |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
214 self.con.putrequest('PUT', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
215 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
216 res = self.con.getresponse() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
217 self.assertEqual(res.status, 400) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
218 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
219 def test_version_invalid(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
220 self.con._http_vsn = 99 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
221 self.con._http_vsn_str = 'HTTP/9.9' |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
222 self.con.putrequest('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
223 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
224 res = self.con.getresponse() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
225 self.assertEqual(res.status, 505) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
226 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
227 def test_send_blank(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
228 self.con._http_vsn_str = '' |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
229 self.con.putrequest('', '') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
230 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
231 res = self.con.getresponse() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
232 self.assertEqual(res.status, 400) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
233 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
234 def test_header_close(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
235 self.con.putrequest('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
236 self.con.putheader('Connection', 'close') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
237 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
238 res = self.con.getresponse() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
239 self.assertEqual(res.status, 501) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
240 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
241 def test_head_keep_alive(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
242 self.con._http_vsn_str = 'HTTP/1.1' |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
243 self.con.putrequest('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
244 self.con.putheader('Connection', 'keep-alive') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
245 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
246 res = self.con.getresponse() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
247 self.assertEqual(res.status, 501) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
248 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
249 def test_handler(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
250 self.con.request('TEST', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
251 res = self.con.getresponse() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
252 self.assertEqual(res.status, 204) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
253 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
254 def test_return_header_keep_alive(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
255 self.con.request('KEEP', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
256 res = self.con.getresponse() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
257 self.assertEqual(res.getheader('Connection'), 'keep-alive') |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
258 self.con.request('TEST', '/') |
|
65789
2e1ad7121e29
Merged revisions 86006 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents:
65077
diff
changeset
|
259 self.addCleanup(self.con.close) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
260 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
261 def test_internal_key_error(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
262 self.con.request('KEYERROR', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
263 res = self.con.getresponse() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
264 self.assertEqual(res.status, 999) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
265 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
266 def test_return_custom_status(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
267 self.con.request('CUSTOM', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
268 res = self.con.getresponse() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
269 self.assertEqual(res.status, 999) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
270 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
271 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
272 class SimpleHTTPServerTestCase(BaseTestCase): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
273 class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
274 pass |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
275 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
276 def setUp(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
277 BaseTestCase.setUp(self) |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
278 self.cwd = os.getcwd() |
|
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
279 basetempdir = tempfile.gettempdir() |
|
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
280 os.chdir(basetempdir) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
281 self.data = 'We are the knights who say Ni!' |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
282 self.tempdir = tempfile.mkdtemp(dir=basetempdir) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
283 self.tempdir_name = os.path.basename(self.tempdir) |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
284 temp = open(os.path.join(self.tempdir, 'test'), 'wb') |
|
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
285 temp.write(self.data) |
|
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
286 temp.close() |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
287 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
288 def tearDown(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
289 try: |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
290 os.chdir(self.cwd) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
291 try: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
292 shutil.rmtree(self.tempdir) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
293 except: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
294 pass |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
295 finally: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
296 BaseTestCase.tearDown(self) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
297 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
298 def check_status_and_reason(self, response, status, data=None): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
299 body = response.read() |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52929
diff
changeset
|
300 self.assertTrue(response) |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
301 self.assertEqual(response.status, status) |
|
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
302 self.assertIsNotNone(response.reason) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
303 if data: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
304 self.assertEqual(data, body) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
305 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
306 def test_get(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
307 #constructs the path relative to the root directory of the HTTPServer |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
308 response = self.request(self.tempdir_name + '/test') |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
309 self.check_status_and_reason(response, 200, data=self.data) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
310 response = self.request(self.tempdir_name + '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
311 self.check_status_and_reason(response, 200) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
312 response = self.request(self.tempdir_name) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
313 self.check_status_and_reason(response, 301) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
314 response = self.request('/ThisDoesNotExist') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
315 self.check_status_and_reason(response, 404) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
316 response = self.request('/' + 'ThisDoesNotExist' + '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
317 self.check_status_and_reason(response, 404) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
318 f = open(os.path.join(self.tempdir_name, 'index.html'), 'w') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
319 response = self.request('/' + self.tempdir_name + '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
320 self.check_status_and_reason(response, 200) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
321 if os.name == 'posix': |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
322 # chmod won't work as expected on Windows platforms |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
323 os.chmod(self.tempdir, 0) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
324 response = self.request(self.tempdir_name + '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
325 self.check_status_and_reason(response, 404) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
326 os.chmod(self.tempdir, 0755) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
327 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
328 def test_head(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
329 response = self.request( |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
330 self.tempdir_name + '/test', method='HEAD') |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
331 self.check_status_and_reason(response, 200) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
332 self.assertEqual(response.getheader('content-length'), |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
333 str(len(self.data))) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
334 self.assertEqual(response.getheader('content-type'), |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
335 'application/octet-stream') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
336 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
337 def test_invalid_requests(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
338 response = self.request('/', method='FOO') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
339 self.check_status_and_reason(response, 501) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
340 # requests must be case sensitive,so this should fail too |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
341 response = self.request('/', method='get') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
342 self.check_status_and_reason(response, 501) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
343 response = self.request('/', method='GETs') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
344 self.check_status_and_reason(response, 501) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
345 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
346 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
347 cgi_file1 = """\ |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
348 #!%s |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
349 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
350 print "Content-type: text/html" |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
351 print |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
352 print "Hello World" |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
353 """ |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
354 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
355 cgi_file2 = """\ |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
356 #!%s |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
357 import cgi |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
358 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
359 print "Content-type: text/html" |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
360 print |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
361 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
362 form = cgi.FieldStorage() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
363 print "%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"), |
|
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
364 form.getfirst("bacon")) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
365 """ |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
366 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
367 class CGIHTTPServerTestCase(BaseTestCase): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
368 class request_handler(NoLogRequestHandler, CGIHTTPRequestHandler): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
369 pass |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
370 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
371 def setUp(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
372 BaseTestCase.setUp(self) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
373 self.parent_dir = tempfile.mkdtemp() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
374 self.cgi_dir = os.path.join(self.parent_dir, 'cgi-bin') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
375 os.mkdir(self.cgi_dir) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
376 |
|
59914
f0014554166f
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59726
diff
changeset
|
377 # The shebang line should be pure ASCII: use symlink if possible. |
|
f0014554166f
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59726
diff
changeset
|
378 # See issue #7668. |
|
f0014554166f
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59726
diff
changeset
|
379 if hasattr(os, 'symlink'): |
|
f0014554166f
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59726
diff
changeset
|
380 self.pythonexe = os.path.join(self.parent_dir, 'python') |
|
f0014554166f
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59726
diff
changeset
|
381 os.symlink(sys.executable, self.pythonexe) |
|
f0014554166f
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59726
diff
changeset
|
382 else: |
|
f0014554166f
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59726
diff
changeset
|
383 self.pythonexe = sys.executable |
|
f0014554166f
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59726
diff
changeset
|
384 |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
385 self.file1_path = os.path.join(self.cgi_dir, 'file1.py') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
386 with open(self.file1_path, 'w') as file1: |
|
59914
f0014554166f
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59726
diff
changeset
|
387 file1.write(cgi_file1 % self.pythonexe) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
388 os.chmod(self.file1_path, 0777) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
389 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
390 self.file2_path = os.path.join(self.cgi_dir, 'file2.py') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
391 with open(self.file2_path, 'w') as file2: |
|
59914
f0014554166f
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59726
diff
changeset
|
392 file2.write(cgi_file2 % self.pythonexe) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
393 os.chmod(self.file2_path, 0777) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
394 |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
395 self.cwd = os.getcwd() |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
396 os.chdir(self.parent_dir) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
397 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
398 def tearDown(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
399 try: |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
400 os.chdir(self.cwd) |
|
59914
f0014554166f
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59726
diff
changeset
|
401 if self.pythonexe != sys.executable: |
|
f0014554166f
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59726
diff
changeset
|
402 os.remove(self.pythonexe) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
403 os.remove(self.file1_path) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
404 os.remove(self.file2_path) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
405 os.rmdir(self.cgi_dir) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
406 os.rmdir(self.parent_dir) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
407 finally: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
408 BaseTestCase.tearDown(self) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
409 |
|
52929
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
410 def test_url_collapse_path_split(self): |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
411 test_vectors = { |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
412 '': ('/', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
413 '..': IndexError, |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
414 '/.//..': IndexError, |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
415 '/': ('/', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
416 '//': ('/', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
417 '/\\': ('/', '\\'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
418 '/.//': ('/', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
419 'cgi-bin/file1.py': ('/cgi-bin', 'file1.py'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
420 '/cgi-bin/file1.py': ('/cgi-bin', 'file1.py'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
421 'a': ('/', 'a'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
422 '/a': ('/', 'a'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
423 '//a': ('/', 'a'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
424 './a': ('/', 'a'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
425 './C:/': ('/C:', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
426 '/a/b': ('/a', 'b'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
427 '/a/b/': ('/a/b', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
428 '/a/b/c/..': ('/a/b', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
429 '/a/b/c/../d': ('/a/b', 'd'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
430 '/a/b/c/../d/e/../f': ('/a/b/d', 'f'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
431 '/a/b/c/../d/e/../../f': ('/a/b', 'f'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
432 '/a/b/c/../d/e/.././././..//f': ('/a/b', 'f'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
433 '../a/b/c/../d/e/.././././..//f': IndexError, |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
434 '/a/b/c/../d/e/../../../f': ('/a', 'f'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
435 '/a/b/c/../d/e/../../../../f': ('/', 'f'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
436 '/a/b/c/../d/e/../../../../../f': IndexError, |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
437 '/a/b/c/../d/e/../../../../f/..': ('/', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
438 } |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
439 for path, expected in test_vectors.iteritems(): |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
440 if isinstance(expected, type) and issubclass(expected, Exception): |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
441 self.assertRaises(expected, |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
442 CGIHTTPServer._url_collapse_path_split, path) |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
443 else: |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
444 actual = CGIHTTPServer._url_collapse_path_split(path) |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
445 self.assertEqual(expected, actual, |
|
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
446 msg='path = %r\nGot: %r\nWanted: %r' % |
|
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
447 (path, actual, expected)) |
|
52929
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
448 |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
449 def test_headers_and_content(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
450 res = self.request('/cgi-bin/file1.py') |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
451 self.assertEqual(('Hello World\n', 'text/html', 200), |
|
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
452 (res.read(), res.getheader('Content-type'), res.status)) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
453 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
454 def test_post(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
455 params = urllib.urlencode({'spam' : 1, 'eggs' : 'python', 'bacon' : 123456}) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
456 headers = {'Content-type' : 'application/x-www-form-urlencoded'} |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
457 res = self.request('/cgi-bin/file2.py', 'POST', params, headers) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
458 |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
459 self.assertEqual(res.read(), '1, python, 123456\n') |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
460 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
461 def test_invaliduri(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
462 res = self.request('/cgi-bin/invalid') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
463 res.read() |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
464 self.assertEqual(res.status, 404) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
465 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
466 def test_authorization(self): |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
467 headers = {'Authorization' : 'Basic %s' % |
|
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
468 base64.b64encode('username:pass')} |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
469 res = self.request('/cgi-bin/file1.py', 'GET', headers=headers) |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
470 self.assertEqual(('Hello World\n', 'text/html', 200), |
|
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
471 (res.read(), res.getheader('Content-type'), res.status)) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
472 |
|
52929
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
473 def test_no_leading_slash(self): |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
474 # http://bugs.python.org/issue2254 |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
475 res = self.request('cgi-bin/file1.py') |
|
59726
07c7664f21a1
Various tests cleanup: check_warnings/check_py3k_warnings, unittest.assert* and setUp/tearDown.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
56870
diff
changeset
|
476 self.assertEqual(('Hello World\n', 'text/html', 200), |
|
52929
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
477 (res.read(), res.getheader('Content-type'), res.status)) |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
478 |
|
65077
f11dc9404d11
Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65030
diff
changeset
|
479 def test_os_environ_is_not_altered(self): |
|
f11dc9404d11
Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65030
diff
changeset
|
480 signature = "Test CGI Server" |
|
f11dc9404d11
Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65030
diff
changeset
|
481 os.environ['SERVER_SOFTWARE'] = signature |
|
f11dc9404d11
Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65030
diff
changeset
|
482 res = self.request('/cgi-bin/file1.py') |
|
f11dc9404d11
Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65030
diff
changeset
|
483 self.assertEqual((b'Hello World\n', 'text/html', 200), |
|
f11dc9404d11
Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65030
diff
changeset
|
484 (res.read(), res.getheader('Content-type'), res.status)) |
|
f11dc9404d11
Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65030
diff
changeset
|
485 self.assertEqual(os.environ['SERVER_SOFTWARE'], signature) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
486 |
|
66973
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
487 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
488 class SocketlessRequestHandler(SimpleHTTPRequestHandler): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
489 def __init__(self): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
490 self.get_called = False |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
491 self.protocol_version = "HTTP/1.1" |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
492 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
493 def do_GET(self): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
494 self.get_called = True |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
495 self.send_response(200) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
496 self.send_header('Content-Type', 'text/html') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
497 self.end_headers() |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
498 self.wfile.write(b'<html><body>Data</body></html>\r\n') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
499 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
500 def log_message(self, format, *args): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
501 pass |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
502 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
503 class RejectingSocketlessRequestHandler(SocketlessRequestHandler): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
504 def handle_expect_100(self): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
505 self.send_error(417) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
506 return False |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
507 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
508 class BaseHTTPRequestHandlerTestCase(unittest.TestCase): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
509 """Test the functionaility of the BaseHTTPServer. |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
510 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
511 Test the support for the Expect 100-continue header. |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
512 """ |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
513 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
514 HTTPResponseMatch = re.compile(b'HTTP/1.[0-9]+ 200 OK') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
515 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
516 def setUp (self): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
517 self.handler = SocketlessRequestHandler() |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
518 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
519 def send_typical_request(self, message): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
520 input = StringIO(message) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
521 output = StringIO() |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
522 self.handler.rfile = input |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
523 self.handler.wfile = output |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
524 self.handler.handle_one_request() |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
525 output.seek(0) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
526 return output.readlines() |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
527 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
528 def verify_get_called(self): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
529 self.assertTrue(self.handler.get_called) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
530 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
531 def verify_expected_headers(self, headers): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
532 for fieldName in b'Server: ', b'Date: ', b'Content-Type: ': |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
533 self.assertEqual(sum(h.startswith(fieldName) for h in headers), 1) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
534 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
535 def verify_http_server_response(self, response): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
536 match = self.HTTPResponseMatch.search(response) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
537 self.assertTrue(match is not None) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
538 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
539 def test_http_1_1(self): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
540 result = self.send_typical_request(b'GET / HTTP/1.1\r\n\r\n') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
541 self.verify_http_server_response(result[0]) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
542 self.verify_expected_headers(result[1:-1]) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
543 self.verify_get_called() |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
544 self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
545 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
546 def test_http_1_0(self): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
547 result = self.send_typical_request(b'GET / HTTP/1.0\r\n\r\n') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
548 self.verify_http_server_response(result[0]) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
549 self.verify_expected_headers(result[1:-1]) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
550 self.verify_get_called() |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
551 self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
552 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
553 def test_http_0_9(self): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
554 result = self.send_typical_request(b'GET / HTTP/0.9\r\n\r\n') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
555 self.assertEqual(len(result), 1) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
556 self.assertEqual(result[0], b'<html><body>Data</body></html>\r\n') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
557 self.verify_get_called() |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
558 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
559 def test_with_continue_1_0(self): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
560 result = self.send_typical_request(b'GET / HTTP/1.0\r\nExpect: 100-continue\r\n\r\n') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
561 self.verify_http_server_response(result[0]) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
562 self.verify_expected_headers(result[1:-1]) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
563 self.verify_get_called() |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
564 self.assertEqual(result[-1], b'<html><body>Data</body></html>\r\n') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
565 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
566 def test_request_length(self): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
567 # Issue #10714: huge request lines are discarded, to avoid Denial |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
568 # of Service attacks. |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
569 result = self.send_typical_request(b'GET ' + b'x' * 65537) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
570 self.assertEqual(result[0], b'HTTP/1.1 414 Request-URI Too Long\r\n') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
571 self.assertFalse(self.handler.get_called) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
572 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
573 class SimpleHTTPRequestHandlerTestCase(unittest.TestCase): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
574 """ Test url parsing """ |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
575 def setUp(self): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
576 self.translated = os.getcwd() |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
577 self.translated = os.path.join(self.translated, 'filename') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
578 self.handler = SocketlessRequestHandler() |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
579 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
580 def test_query_arguments(self): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
581 path = self.handler.translate_path('/filename') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
582 self.assertEqual(path, self.translated) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
583 path = self.handler.translate_path('/filename?foo=bar') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
584 self.assertEqual(path, self.translated) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
585 path = self.handler.translate_path('/filename?a=b&spam=eggs#zot') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
586 self.assertEqual(path, self.translated) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
587 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
588 def test_start_with_double_slash(self): |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
589 path = self.handler.translate_path('//filename') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
590 self.assertEqual(path, self.translated) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
591 path = self.handler.translate_path('//filename?foo=bar') |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
592 self.assertEqual(path, self.translated) |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
593 |
|
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
594 |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
595 def test_main(verbose=None): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
596 try: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
597 cwd = os.getcwd() |
|
65030
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
598 test_support.run_unittest(BaseHTTPRequestHandlerTestCase, |
|
66973
2c429d8c5ee2
Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
65789
diff
changeset
|
599 SimpleHTTPRequestHandlerTestCase, |
|
65030
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
600 BaseHTTPServerTestCase, |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
601 SimpleHTTPServerTestCase, |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
602 CGIHTTPServerTestCase |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
603 ) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
604 finally: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
605 os.chdir(cwd) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
606 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
607 if __name__ == '__main__': |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
608 test_main() |
