Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_httpservers.py @ 56564:81433a676a8b legacy-trunk
Revert changes made to environment in test_httpservers
| author | Nick Coghlan <ncoghlan@gmail.com> |
|---|---|
| date | Sat, 17 Oct 2009 15:23:08 +0000 |
| parents | 6768b2350c57 |
| children | b818a7a70a47 |
| 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 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
14 import base64 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
15 import shutil |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
16 import urllib |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
17 import httplib |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
18 import tempfile |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
19 import threading |
|
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 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
22 from test import test_support |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
23 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
24 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
25 class NoLogRequestHandler: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
26 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
|
27 # 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
|
28 pass |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
29 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
30 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
31 class TestServerThread(threading.Thread): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
32 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
|
33 threading.Thread.__init__(self) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
34 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
|
35 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
|
36 self.test_object.lock.acquire() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
37 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
38 def run(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
39 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
|
40 self.test_object.PORT = self.server.socket.getsockname()[1] |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
41 self.test_object.lock.release() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
42 try: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
43 self.server.serve_forever() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
44 finally: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
45 self.server.server_close() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
46 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
47 def stop(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
48 self.server.shutdown() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
49 |
|
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 BaseTestCase(unittest.TestCase): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
52 def setUp(self): |
|
56564
81433a676a8b
Revert changes made to environment in test_httpservers
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
53 os.environ = test_support.EnvironmentVarGuard() |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
54 self.lock = threading.Lock() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
55 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
|
56 self.thread.start() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
57 self.lock.acquire() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
58 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
59 def tearDown(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
60 self.lock.release() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
61 self.thread.stop() |
|
56564
81433a676a8b
Revert changes made to environment in test_httpservers
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
62 os.environ.__exit__() |
|
81433a676a8b
Revert changes made to environment in test_httpservers
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
63 os.environ = os.environ._environ |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
64 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
65 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
|
66 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
|
67 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
|
68 return self.connection.getresponse() |
|
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 BaseHTTPServerTestCase(BaseTestCase): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
76 def do_TEST(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
77 self.send_response(204) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
78 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
|
79 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
|
80 self.end_headers() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
81 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
82 def do_KEEP(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
83 self.send_response(204) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
84 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
|
85 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
|
86 self.end_headers() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
87 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
88 def do_KEYERROR(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
89 self.send_error(999) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
90 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
91 def do_CUSTOM(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
92 self.send_response(999) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
93 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
|
94 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
|
95 self.end_headers() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
96 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
97 def setUp(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
98 BaseTestCase.setUp(self) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
99 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
|
100 self.con.connect() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
101 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
102 def test_command(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
103 self.con.request('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
104 res = self.con.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
105 self.assertEquals(res.status, 501) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
106 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
107 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
|
108 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
|
109 self.con.putrequest('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
110 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
111 res = self.con.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
112 self.assertEquals(res.status, 501) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
113 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
114 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
|
115 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
|
116 self.con.putrequest('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
117 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
118 res = self.con.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
119 self.assertEquals(res.status, 400) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
120 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
121 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
|
122 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
|
123 self.con.putrequest('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
124 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
125 res = self.con.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
126 self.assertEquals(res.status, 400) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
127 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
128 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
|
129 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
|
130 self.con.putrequest('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
131 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
132 res = self.con.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
133 self.assertEquals(res.status, 501) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
134 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
135 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
|
136 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
|
137 self.con.putrequest('PUT', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
138 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
139 res = self.con.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
140 self.assertEquals(res.status, 400) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
141 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
142 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
|
143 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
|
144 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
|
145 self.con.putrequest('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
146 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
147 res = self.con.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
148 self.assertEquals(res.status, 505) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
149 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
150 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
|
151 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
|
152 self.con.putrequest('', '') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
153 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
154 res = self.con.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
155 self.assertEquals(res.status, 400) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
156 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
157 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
|
158 self.con.putrequest('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
159 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
|
160 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
161 res = self.con.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
162 self.assertEquals(res.status, 501) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
163 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
164 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
|
165 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
|
166 self.con.putrequest('GET', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
167 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
|
168 self.con.endheaders() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
169 res = self.con.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
170 self.assertEquals(res.status, 501) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
171 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
172 def test_handler(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
173 self.con.request('TEST', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
174 res = self.con.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
175 self.assertEquals(res.status, 204) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
176 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
177 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
|
178 self.con.request('KEEP', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
179 res = self.con.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
180 self.assertEquals(res.getheader('Connection'), 'keep-alive') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
181 self.con.request('TEST', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
182 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
183 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
|
184 self.con.request('KEYERROR', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
185 res = self.con.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
186 self.assertEquals(res.status, 999) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
187 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
188 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
|
189 self.con.request('CUSTOM', '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
190 res = self.con.getresponse() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
191 self.assertEquals(res.status, 999) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
192 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
193 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
194 class SimpleHTTPServerTestCase(BaseTestCase): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
195 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
|
196 pass |
|
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 setUp(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
199 BaseTestCase.setUp(self) |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
200 self.cwd = os.getcwd() |
|
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
201 basetempdir = tempfile.gettempdir() |
|
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
202 os.chdir(basetempdir) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
203 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
|
204 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
|
205 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
|
206 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
|
207 temp.write(self.data) |
|
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
208 temp.close() |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
209 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
210 def tearDown(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
211 try: |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
212 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
|
213 try: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
214 shutil.rmtree(self.tempdir) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
215 except: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
216 pass |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
217 finally: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
218 BaseTestCase.tearDown(self) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
219 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
220 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
|
221 body = response.read() |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52929
diff
changeset
|
222 self.assertTrue(response) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
223 self.assertEquals(response.status, status) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
52929
diff
changeset
|
224 self.assertTrue(response.reason != None) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
225 if data: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
226 self.assertEqual(data, body) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
227 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
228 def test_get(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
229 #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
|
230 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
|
231 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
|
232 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
|
233 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
|
234 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
|
235 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
|
236 response = self.request('/ThisDoesNotExist') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
237 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
|
238 response = self.request('/' + 'ThisDoesNotExist' + '/') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
239 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
|
240 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
|
241 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
|
242 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
|
243 if os.name == 'posix': |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
244 # 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
|
245 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
|
246 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
|
247 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
|
248 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
|
249 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
250 def test_head(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
251 response = self.request( |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
252 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
|
253 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
|
254 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
|
255 str(len(self.data))) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
256 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
|
257 'application/octet-stream') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
258 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
259 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
|
260 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
|
261 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
|
262 # 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
|
263 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
|
264 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
|
265 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
|
266 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
|
267 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
268 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
269 cgi_file1 = """\ |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
270 #!%s |
|
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 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
|
273 print |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
274 print "Hello World" |
|
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 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
277 cgi_file2 = """\ |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
278 #!%s |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
279 import cgi |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
280 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
281 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
|
282 print |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
283 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
284 form = cgi.FieldStorage() |
|
47179
d17fac896a0b
Activate two more test cases in test_httpservers.
Georg Brandl <georg@python.org>
parents:
47153
diff
changeset
|
285 print "%%s, %%s, %%s" %% (form.getfirst("spam"), form.getfirst("eggs"),\ |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
286 form.getfirst("bacon")) |
|
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 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
289 class CGIHTTPServerTestCase(BaseTestCase): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
290 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
|
291 pass |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
292 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
293 def setUp(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
294 BaseTestCase.setUp(self) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
295 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
|
296 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
|
297 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
|
298 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
299 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
|
300 with open(self.file1_path, 'w') as file1: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
301 file1.write(cgi_file1 % sys.executable) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
302 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
|
303 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
304 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
|
305 with open(self.file2_path, 'w') as file2: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
306 file2.write(cgi_file2 % sys.executable) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
307 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
|
308 |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
309 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
|
310 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
|
311 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
312 def tearDown(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
313 try: |
|
47180
77557216ba32
Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents:
47179
diff
changeset
|
314 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
|
315 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
|
316 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
|
317 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
|
318 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
|
319 finally: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
320 BaseTestCase.tearDown(self) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
321 |
|
52929
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
322 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
|
323 test_vectors = { |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
324 '': ('/', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
325 '..': IndexError, |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
326 '/.//..': IndexError, |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
327 '/': ('/', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
328 '//': ('/', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
329 '/\\': ('/', '\\'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
330 '/.//': ('/', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
331 '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
|
332 '/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
|
333 'a': ('/', 'a'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
334 '/a': ('/', 'a'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
335 '//a': ('/', 'a'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
336 './a': ('/', 'a'), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
337 './C:/': ('/C:', ''), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
338 '/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
|
339 '/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
|
340 '/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
|
341 '/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
|
342 '/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
|
343 '/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
|
344 '/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
|
345 '../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
|
346 '/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
|
347 '/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
|
348 '/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
|
349 '/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
|
350 } |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
351 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
|
352 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
|
353 self.assertRaises(expected, |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
354 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
|
355 else: |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
356 actual = 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
|
357 self.assertEquals(expected, actual, |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
358 msg='path = %r\nGot: %r\nWanted: %r' % ( |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
359 path, actual, expected)) |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
360 |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
361 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
|
362 res = self.request('/cgi-bin/file1.py') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
363 self.assertEquals(('Hello World\n', 'text/html', 200), \ |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
364 (res.read(), res.getheader('Content-type'), res.status)) |
|
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 def test_post(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
367 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
|
368 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
|
369 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
|
370 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
371 self.assertEquals(res.read(), '1, python, 123456\n') |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
372 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
373 def test_invaliduri(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
374 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
|
375 res.read() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
376 self.assertEquals(res.status, 404) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
377 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
378 def test_authorization(self): |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
379 headers = {'Authorization' : 'Basic %s' % \ |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
380 base64.b64encode('username:pass')} |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
381 res = self.request('/cgi-bin/file1.py', 'GET', headers=headers) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
382 self.assertEquals(('Hello World\n', 'text/html', 200), \ |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
383 (res.read(), res.getheader('Content-type'), res.status)) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
384 |
|
52929
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
385 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
|
386 # 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
|
387 res = self.request('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
|
388 self.assertEquals(('Hello World\n', 'text/html', 200), |
|
c6c4398293bd
- Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents:
47180
diff
changeset
|
389 (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
|
390 |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
391 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
392 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
|
393 try: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
394 cwd = os.getcwd() |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
395 test_support.run_unittest(BaseHTTPServerTestCase, |
|
56564
81433a676a8b
Revert changes made to environment in test_httpservers
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
396 SimpleHTTPServerTestCase, |
|
81433a676a8b
Revert changes made to environment in test_httpservers
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
397 CGIHTTPServerTestCase |
|
81433a676a8b
Revert changes made to environment in test_httpservers
Nick Coghlan <ncoghlan@gmail.com>
parents:
55002
diff
changeset
|
398 ) |
|
47153
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
399 finally: |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
400 os.chdir(cwd) |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
401 |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
402 if __name__ == '__main__': |
|
efe22eef26be
GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
403 test_main() |
