Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_httpservers.py @ 65030:b99f527e0d77 2.7
Added BaseHTTPRequestHandler related tests.
| author | Senthil Kumaran <orsenthil@gmail.com> |
|---|---|
| date | Thu, 30 Sep 2010 06:40:56 +0000 |
| parents | 3a0484200034 |
| children | f11dc9404d11 |
| 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', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
259 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
260 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
|
261 self.con.request('KEYERROR', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
262 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
|
263 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
|
264 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
265 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
|
266 self.con.request('CUSTOM', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
267 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
|
268 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
|
269 |
|
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 class SimpleHTTPServerTestCase(BaseTestCase): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
272 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
|
273 pass |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
274 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
275 def setUp(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
276 BaseTestCase.setUp(self) |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
277 self.cwd = os.getcwd() |
|
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
278 basetempdir = tempfile.gettempdir() |
|
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
279 os.chdir(basetempdir) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
280 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
|
281 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
|
282 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
|
283 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
|
284 temp.write(self.data) |
|
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
285 temp.close() |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
286 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
287 def tearDown(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
288 try: |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
289 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
|
290 try: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
291 shutil.rmtree(self.tempdir) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
292 except: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
293 pass |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
294 finally: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
295 BaseTestCase.tearDown(self) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
296 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
297 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
|
298 body = response.read() |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52929
diff
changeset
|
299 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
|
300 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
|
301 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
|
302 if data: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
303 self.assertEqual(data, body) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
304 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
305 def test_get(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
306 #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
|
307 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
|
308 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
|
309 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
|
310 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
|
311 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
|
312 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
|
313 response = self.request('/ThisDoesNotExist') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
314 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
|
315 response = self.request('/' + 'ThisDoesNotExist' + '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
316 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
|
317 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
|
318 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
|
319 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
|
320 if os.name == 'posix': |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
321 # 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
|
322 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
|
323 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
|
324 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
|
325 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
|
326 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
327 def test_head(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
328 response = self.request( |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
329 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
|
330 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
|
331 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
|
332 str(len(self.data))) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
333 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
|
334 'application/octet-stream') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
335 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
336 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
|
337 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
|
338 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
|
339 # 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
|
340 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
|
341 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
|
342 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
|
343 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
|
344 |
|
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 cgi_file1 = """\ |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
347 #!%s |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
348 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
349 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
|
350 print |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
351 print "Hello World" |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
352 """ |
|
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 cgi_file2 = """\ |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
355 #!%s |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
356 import cgi |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
357 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
358 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
|
359 print |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
360 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
361 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
|
362 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
|
363 form.getfirst("bacon")) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
364 """ |
|
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 class CGIHTTPServerTestCase(BaseTestCase): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
367 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
|
368 pass |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
369 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
370 def setUp(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
371 BaseTestCase.setUp(self) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
372 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
|
373 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
|
374 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
|
375 |
|
59914
f0014554166f
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59726
diff
changeset
|
376 # 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
|
377 # 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
|
378 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
|
379 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
|
380 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
|
381 else: |
|
f0014554166f
#7668: Fix test_httpservers failure when sys.executable contains non-ASCII bytes.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59726
diff
changeset
|
382 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
|
383 |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
384 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
|
385 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
|
386 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
|
387 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
|
388 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
389 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
|
390 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
|
391 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
|
392 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
|
393 |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
394 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
|
395 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
|
396 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
397 def tearDown(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
398 try: |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
399 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
|
400 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
|
401 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
|
402 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
|
403 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
|
404 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
|
405 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
|
406 finally: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
407 BaseTestCase.tearDown(self) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
408 |
|
52929
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
409 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
|
410 test_vectors = { |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
411 '': ('/', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
412 '..': IndexError, |
|
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 '/': ('/', ''), |
|
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 '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
|
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 'a': ('/', 'a'), |
|
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 './C:/': ('/C:', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
425 '/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
|
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/c/..': ('/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/../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
|
429 '/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
|
430 '/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
|
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': IndexError, |
|
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': ('/a', 'f'), |
|
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': ('/', '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': IndexError, |
|
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/..': ('/', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
437 } |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
438 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
|
439 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
|
440 self.assertRaises(expected, |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
441 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
|
442 else: |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
443 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
|
444 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
|
445 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
|
446 (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
|
447 |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
448 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
|
449 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
|
450 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
|
451 (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
|
452 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
453 def test_post(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
454 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
|
455 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
|
456 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
|
457 |
|
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
|
458 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
|
459 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
460 def test_invaliduri(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
461 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
|
462 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
|
463 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
|
464 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
465 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
|
466 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
|
467 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
|
468 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
|
469 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
|
470 (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
|
471 |
|
52929
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
472 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
|
473 # 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
|
474 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
|
475 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
|
476 (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
|
477 |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
478 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
479 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
|
480 try: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
481 cwd = os.getcwd() |
|
65030
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
482 test_support.run_unittest(BaseHTTPRequestHandlerTestCase, |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
483 BaseHTTPServerTestCase, |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
484 SimpleHTTPServerTestCase, |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
485 CGIHTTPServerTestCase |
|
b99f527e0d77
Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents:
61046
diff
changeset
|
486 ) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
487 finally: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
488 os.chdir(cwd) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
489 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
490 if __name__ == '__main__': |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
491 test_main() |
