annotate Lib/test/test_httpservers.py @ 76219:89eeaa18700f 2.7

fix the incorrect changes made for PATH_INFO value - Issue10484
author Senthil Kumaran <senthil@uthcode.com>
date Wed, 11 Apr 2012 03:07:57 +0800
parents bab9f29c93fd
children c67efb8ffca4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
66975
4d2b428d7882 svnmerge fooled me. That test class already existed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66973
diff changeset
34 class SocketlessRequestHandler(SimpleHTTPRequestHandler):
65030
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()
66975
4d2b428d7882 svnmerge fooled me. That test class already existed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66973
diff changeset
44 self.wfile.write(b'<html><body>Data</body></html>\r\n')
65030
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
47153
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 class TestServerThread(threading.Thread):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
51 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
52 threading.Thread.__init__(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
53 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
54 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
55
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
56 def run(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
57 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
58 self.test_object.PORT = self.server.socket.getsockname()[1]
60969
c02144b1c98c Merged revisions 80480 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59953
diff changeset
59 self.test_object.server_started.set()
c02144b1c98c Merged revisions 80480 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59953
diff changeset
60 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
61 try:
60969
c02144b1c98c Merged revisions 80480 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59953
diff changeset
62 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
63 finally:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
64 self.server.server_close()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
65
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
66 def stop(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
67 self.server.shutdown()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
68
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 class BaseTestCase(unittest.TestCase):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
71 def setUp(self):
56870
269f1d9c86cd (Hopefully) suppress transient refleaks in test_httpservers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56565
diff changeset
72 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
73 os.environ = test_support.EnvironmentVarGuard()
60969
c02144b1c98c Merged revisions 80480 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59953
diff changeset
74 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
75 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
76 self.thread.start()
60969
c02144b1c98c Merged revisions 80480 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59953
diff changeset
77 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
78
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
79 def tearDown(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
80 self.thread.stop()
56564
81433a676a8b Revert changes made to environment in test_httpservers
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
81 os.environ.__exit__()
56870
269f1d9c86cd (Hopefully) suppress transient refleaks in test_httpservers.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56565
diff changeset
82 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
83
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
84 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
85 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
86 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
87 return self.connection.getresponse()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
88
65030
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
89 class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
68588
cd8d3c2d3bce #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 68256
diff changeset
90 """Test the functionality of the BaseHTTPServer focussing on
65030
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
91 BaseHTTPRequestHandler.
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
92 """
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 HTTPResponseMatch = re.compile('HTTP/1.[0-9]+ 200 OK')
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
95
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
96 def setUp (self):
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
97 self.handler = SocketlessRequestHandler()
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
98
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
99 def send_typical_request(self, message):
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
100 input = StringIO(message)
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
101 output = StringIO()
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
102 self.handler.rfile = input
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
103 self.handler.wfile = output
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
104 self.handler.handle_one_request()
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
105 output.seek(0)
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
106 return output.readlines()
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
107
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
108 def verify_get_called(self):
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
109 self.assertTrue(self.handler.get_called)
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
110
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
111 def verify_expected_headers(self, headers):
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
112 for fieldName in 'Server: ', 'Date: ', 'Content-Type: ':
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
113 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
114
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
115 def verify_http_server_response(self, response):
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
116 match = self.HTTPResponseMatch.search(response)
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
117 self.assertTrue(match is not None)
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
118
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
119 def test_http_1_1(self):
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
120 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
121 self.verify_http_server_response(result[0])
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
122 self.verify_expected_headers(result[1:-1])
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
123 self.verify_get_called()
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
124 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
125
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
126 def test_http_1_0(self):
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
127 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
128 self.verify_http_server_response(result[0])
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
129 self.verify_expected_headers(result[1:-1])
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
130 self.verify_get_called()
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
131 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
132
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
133 def test_http_0_9(self):
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
134 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
135 self.assertEqual(len(result), 1)
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
136 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
137 self.verify_get_called()
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
138
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
139 def test_with_continue_1_0(self):
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
140 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
141 self.verify_http_server_response(result[0])
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
142 self.verify_expected_headers(result[1:-1])
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
143 self.verify_get_called()
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
144 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
145
66975
4d2b428d7882 svnmerge fooled me. That test class already existed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66973
diff changeset
146 def test_request_length(self):
4d2b428d7882 svnmerge fooled me. That test class already existed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66973
diff changeset
147 # Issue #10714: huge request lines are discarded, to avoid Denial
4d2b428d7882 svnmerge fooled me. That test class already existed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66973
diff changeset
148 # of Service attacks.
4d2b428d7882 svnmerge fooled me. That test class already existed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66973
diff changeset
149 result = self.send_typical_request(b'GET ' + b'x' * 65537)
4d2b428d7882 svnmerge fooled me. That test class already existed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66973
diff changeset
150 self.assertEqual(result[0], b'HTTP/1.1 414 Request-URI Too Long\r\n')
4d2b428d7882 svnmerge fooled me. That test class already existed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66973
diff changeset
151 self.assertFalse(self.handler.get_called)
4d2b428d7882 svnmerge fooled me. That test class already existed.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66973
diff changeset
152
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
154 class BaseHTTPServerTestCase(BaseTestCase):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
155 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
156 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
157 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
158
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
159 def do_TEST(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', 'close')
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_KEEP(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
166 self.send_response(204)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
167 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
168 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
169 self.end_headers()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
170
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
171 def do_KEYERROR(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
172 self.send_error(999)
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 do_CUSTOM(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
175 self.send_response(999)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
176 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
177 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
178 self.end_headers()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
179
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
180 def setUp(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
181 BaseTestCase.setUp(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
182 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
183 self.con.connect()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
184
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
185 def test_command(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
186 self.con.request('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
187 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
188 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
189
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
190 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
191 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
192 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
193 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
194 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
195 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
196
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
197 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
198 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
199 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
200 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
201 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
202 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
203
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
204 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
205 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
206 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
207 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
208 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
209 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
210
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
211 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
212 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
213 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
214 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
215 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
216 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
217
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
218 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
219 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
220 self.con.putrequest('PUT', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
221 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
222 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
223 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
224
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
225 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
226 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
227 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
228 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
229 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
230 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
231 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
232
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
233 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
234 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
235 self.con.putrequest('', '')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
236 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
237 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
238 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
239
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
240 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
241 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
242 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
243 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
244 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
245 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
246
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
247 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
248 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
249 self.con.putrequest('GET', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
250 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
251 self.con.endheaders()
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
252 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
253 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
254
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
255 def test_handler(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
256 self.con.request('TEST', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
257 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
258 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
259
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
260 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
261 self.con.request('KEEP', '/')
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.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
264 self.con.request('TEST', '/')
65789
2e1ad7121e29 Merged revisions 86006 via svnmerge from
Brian Curtin <brian.curtin@gmail.com>
parents: 65077
diff changeset
265 self.addCleanup(self.con.close)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
266
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
267 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
268 self.con.request('KEYERROR', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
269 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
270 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
271
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
272 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
273 self.con.request('CUSTOM', '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
274 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
275 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
276
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
277
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
278 class SimpleHTTPServerTestCase(BaseTestCase):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
279 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
280 pass
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
281
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
282 def setUp(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
283 BaseTestCase.setUp(self)
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
284 self.cwd = os.getcwd()
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
285 basetempdir = tempfile.gettempdir()
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
286 os.chdir(basetempdir)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
287 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
288 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
289 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
290 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
291 temp.write(self.data)
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
292 temp.close()
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
293
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
294 def tearDown(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
295 try:
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
296 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
297 try:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
298 shutil.rmtree(self.tempdir)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
299 except:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
300 pass
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
301 finally:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
302 BaseTestCase.tearDown(self)
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 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
305 body = response.read()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52929
diff changeset
306 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
307 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
308 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
309 if data:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
310 self.assertEqual(data, body)
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 test_get(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
313 #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
314 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
315 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
316 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
317 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
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, 301)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
320 response = self.request('/ThisDoesNotExist')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
321 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
322 response = self.request('/' + 'ThisDoesNotExist' + '/')
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
323 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
324 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
325 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
326 self.check_status_and_reason(response, 200)
73306
d1cde7081bf5 Issue #13308: Fix test_httpservers failures when run as root.
Charles-François Natali <neologix@free.fr>
parents: 68588
diff changeset
327
d1cde7081bf5 Issue #13308: Fix test_httpservers failures when run as root.
Charles-François Natali <neologix@free.fr>
parents: 68588
diff changeset
328 # chmod() doesn't work as expected on Windows, and filesystem
d1cde7081bf5 Issue #13308: Fix test_httpservers failures when run as root.
Charles-François Natali <neologix@free.fr>
parents: 68588
diff changeset
329 # permissions are ignored by root on Unix.
d1cde7081bf5 Issue #13308: Fix test_httpservers failures when run as root.
Charles-François Natali <neologix@free.fr>
parents: 68588
diff changeset
330 if os.name == 'posix' and os.geteuid() != 0:
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
331 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
332 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
333 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
334 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
335
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
336 def test_head(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(
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
338 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
339 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
340 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
341 str(len(self.data)))
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
342 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
343 'application/octet-stream')
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 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
346 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
347 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
348 # 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
349 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
350 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
351 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
352 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
353
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
354
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
355 cgi_file1 = """\
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
356 #!%s
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
357
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 print "Hello World"
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
361 """
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
362
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
363 cgi_file2 = """\
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
364 #!%s
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
365 import cgi
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
366
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
367 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
368 print
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 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
371 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
372 form.getfirst("bacon"))
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
373 """
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
374
73306
d1cde7081bf5 Issue #13308: Fix test_httpservers failures when run as root.
Charles-François Natali <neologix@free.fr>
parents: 68588
diff changeset
375
d1cde7081bf5 Issue #13308: Fix test_httpservers failures when run as root.
Charles-François Natali <neologix@free.fr>
parents: 68588
diff changeset
376 @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
d1cde7081bf5 Issue #13308: Fix test_httpservers failures when run as root.
Charles-François Natali <neologix@free.fr>
parents: 68588
diff changeset
377 "This test can't be run reliably as root (issue #13308).")
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
378 class CGIHTTPServerTestCase(BaseTestCase):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
379 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
380 pass
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
381
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
382 def setUp(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
383 BaseTestCase.setUp(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
384 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
385 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
386 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
387
59953
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
388 # The shebang line should be pure ASCII: use symlink if possible.
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
389 # See issue #7668.
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
390 if hasattr(os, 'symlink'):
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
391 self.pythonexe = os.path.join(self.parent_dir, 'python')
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
392 os.symlink(sys.executable, self.pythonexe)
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
393 else:
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
394 self.pythonexe = sys.executable
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
395
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
396 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
397 with open(self.file1_path, 'w') as file1:
59953
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
398 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
399 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
400
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
401 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
402 with open(self.file2_path, 'w') as file2:
59953
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
403 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
404 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
405
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
406 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
407 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
408
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
409 def tearDown(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
410 try:
47180
77557216ba32 Argh, this is the *actual* test that works under Windows.
Georg Brandl <georg@python.org>
parents: 47179
diff changeset
411 os.chdir(self.cwd)
59953
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
412 if self.pythonexe != sys.executable:
f84f83963a78 Merged revisions 79297,79307 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 57084
diff changeset
413 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
414 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
415 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
416 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
417 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
418 finally:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
419 BaseTestCase.tearDown(self)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
420
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
421 def test_url_collapse_path_split(self):
76219
89eeaa18700f fix the incorrect changes made for PATH_INFO value - Issue10484
Senthil Kumaran <senthil@uthcode.com>
parents: 75726
diff changeset
422 # verify tail is the last portion and head is the rest on proper urls
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
423 test_vectors = {
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
424 '': ('/', ''),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
425 '..': IndexError,
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
426 '/.//..': IndexError,
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
427 '/': ('/', ''),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
428 '//': ('/', ''),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
429 '/\\': ('/', '\\'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
430 '/.//': ('/', ''),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
431 '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
432 '/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
433 'a': ('/', 'a'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
434 '/a': ('/', 'a'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
435 '//a': ('/', 'a'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
436 './a': ('/', 'a'),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
437 './C:/': ('/C:', ''),
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
438 '/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
439 '/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
440 '/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
441 '/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
442 '/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
443 '/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
444 '/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
445 '../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
446 '/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
447 '/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
448 '/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
449 '/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
450 }
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
451 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
452 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
453 self.assertRaises(expected,
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
454 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
455 else:
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
456 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
457 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
458 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
459 (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
460
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
461 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
462 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
463 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
464 (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
465
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
466 def test_post(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
467 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
468 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
469 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
470
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
471 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
472
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
473 def test_invaliduri(self):
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
474 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
475 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
476 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
477
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
478 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
479 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
480 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
481 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
482 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
483 (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
484
52929
c6c4398293bd - Issue #2254: Fix CGIHTTPServer information disclosure. Relative paths are
Gregory P. Smith <greg@mad-scientist.com>
parents: 47180
diff changeset
485 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
486 # 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
487 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
488 self.assertEqual(('Hello World\n', 'text/html', 200),
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
489 (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
490
65077
f11dc9404d11 Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65030
diff changeset
491 def test_os_environ_is_not_altered(self):
f11dc9404d11 Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65030
diff changeset
492 signature = "Test CGI Server"
f11dc9404d11 Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65030
diff changeset
493 os.environ['SERVER_SOFTWARE'] = signature
f11dc9404d11 Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65030
diff changeset
494 res = self.request('/cgi-bin/file1.py')
f11dc9404d11 Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65030
diff changeset
495 self.assertEqual((b'Hello World\n', 'text/html', 200),
f11dc9404d11 Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65030
diff changeset
496 (res.read(), res.getheader('Content-type'), res.status))
f11dc9404d11 Merged revisions 85202 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents: 65030
diff changeset
497 self.assertEqual(os.environ['SERVER_SOFTWARE'], signature)
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
498
66973
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
499
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
500 class SimpleHTTPRequestHandlerTestCase(unittest.TestCase):
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
501 """ Test url parsing """
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
502 def setUp(self):
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
503 self.translated = os.getcwd()
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
504 self.translated = os.path.join(self.translated, 'filename')
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
505 self.handler = SocketlessRequestHandler()
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
506
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
507 def test_query_arguments(self):
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
508 path = self.handler.translate_path('/filename')
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
509 self.assertEqual(path, self.translated)
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
510 path = self.handler.translate_path('/filename?foo=bar')
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
511 self.assertEqual(path, self.translated)
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
512 path = self.handler.translate_path('/filename?a=b&spam=eggs#zot')
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
513 self.assertEqual(path, self.translated)
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
514
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
515 def test_start_with_double_slash(self):
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
516 path = self.handler.translate_path('//filename')
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
517 self.assertEqual(path, self.translated)
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
518 path = self.handler.translate_path('//filename?foo=bar')
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
519 self.assertEqual(path, self.translated)
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
520
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
521
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
522 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
523 try:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
524 cwd = os.getcwd()
65030
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
525 test_support.run_unittest(BaseHTTPRequestHandlerTestCase,
66973
2c429d8c5ee2 Merged revisions 87317 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65789
diff changeset
526 SimpleHTTPRequestHandlerTestCase,
65030
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
527 BaseHTTPServerTestCase,
47179
d17fac896a0b Activate two more test cases in test_httpservers.
Georg Brandl <georg@python.org>
parents: 47153
diff changeset
528 SimpleHTTPServerTestCase,
d17fac896a0b Activate two more test cases in test_httpservers.
Georg Brandl <georg@python.org>
parents: 47153
diff changeset
529 CGIHTTPServerTestCase
65030
b99f527e0d77 Added BaseHTTPRequestHandler related tests.
Senthil Kumaran <orsenthil@gmail.com>
parents: 61046
diff changeset
530 )
47153
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
531 finally:
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
532 os.chdir(cwd)
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
533
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
534 if __name__ == '__main__':
efe22eef26be GHOP #134, #171, #137: unit tests for the three HTTPServer modules.
Georg Brandl <georg@python.org>
parents:
diff changeset
535 test_main()