Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1 | from __future__ import nested_scopes # Backward compat for 2.1 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2 | from unittest import TestCase |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3 | from wsgiref.util import setup_testing_defaults |
| 4 | from wsgiref.headers import Headers |
| 5 | from wsgiref.handlers import BaseHandler, BaseCGIHandler |
| 6 | from wsgiref import util |
| 7 | from wsgiref.validate import validator |
| 8 | from wsgiref.simple_server import WSGIServer, WSGIRequestHandler, demo_app |
| 9 | from wsgiref.simple_server import make_server |
Jeremy Hylton | e6b59c5 | 2007-08-10 19:13:33 +0000 | [diff] [blame] | 10 | from io import StringIO, BytesIO, BufferedReader |
Alexandre Vassalotti | ce26195 | 2008-05-12 02:31:37 +0000 | [diff] [blame] | 11 | from socketserver import BaseServer |
Senthil Kumaran | a5e0eaf | 2012-07-07 14:29:58 -0700 | [diff] [blame] | 12 | from platform import python_implementation |
| 13 | |
Antoine Pitrou | 8f5731b | 2009-11-03 17:20:10 +0000 | [diff] [blame] | 14 | import os |
| 15 | import re |
| 16 | import sys |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 17 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 18 | from test import support |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 19 | |
| 20 | class MockServer(WSGIServer): |
| 21 | """Non-socket HTTP server""" |
| 22 | |
| 23 | def __init__(self, server_address, RequestHandlerClass): |
| 24 | BaseServer.__init__(self, server_address, RequestHandlerClass) |
| 25 | self.server_bind() |
| 26 | |
| 27 | def server_bind(self): |
| 28 | host, port = self.server_address |
| 29 | self.server_name = host |
| 30 | self.server_port = port |
| 31 | self.setup_environ() |
| 32 | |
| 33 | |
| 34 | class MockHandler(WSGIRequestHandler): |
| 35 | """Non-socket HTTP handler""" |
| 36 | def setup(self): |
| 37 | self.connection = self.request |
| 38 | self.rfile, self.wfile = self.connection |
| 39 | |
| 40 | def finish(self): |
| 41 | pass |
| 42 | |
| 43 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 44 | def hello_app(environ,start_response): |
| 45 | start_response("200 OK", [ |
| 46 | ('Content-Type','text/plain'), |
| 47 | ('Date','Mon, 05 Jun 2006 18:49:54 GMT') |
| 48 | ]) |
Phillip J. Eby | e159422 | 2010-11-02 22:28:59 +0000 | [diff] [blame] | 49 | return [b"Hello, world!"] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 6a10e02 | 2007-08-08 17:01:45 +0000 | [diff] [blame] | 51 | def run_amock(app=hello_app, data=b"GET / HTTP/1.0\n\n"): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 52 | server = make_server("", 80, app, MockServer, MockHandler) |
Jeremy Hylton | e6b59c5 | 2007-08-10 19:13:33 +0000 | [diff] [blame] | 53 | inp = BufferedReader(BytesIO(data)) |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 54 | out = BytesIO() |
Jeremy Hylton | e6b59c5 | 2007-08-10 19:13:33 +0000 | [diff] [blame] | 55 | olderr = sys.stderr |
| 56 | err = sys.stderr = StringIO() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 57 | |
| 58 | try: |
Jeremy Hylton | e6b59c5 | 2007-08-10 19:13:33 +0000 | [diff] [blame] | 59 | server.finish_request((inp, out), ("127.0.0.1",8888)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 60 | finally: |
| 61 | sys.stderr = olderr |
| 62 | |
| 63 | return out.getvalue(), err.getvalue() |
| 64 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 65 | def compare_generic_iter(make_it,match): |
| 66 | """Utility to compare a generic 2.1/2.2+ iterator with an iterable |
| 67 | |
| 68 | If running under Python 2.2+, this tests the iterator using iter()/next(), |
| 69 | as well as __getitem__. 'make_it' must be a function returning a fresh |
| 70 | iterator to be tested (since this may test the iterator twice).""" |
| 71 | |
| 72 | it = make_it() |
| 73 | n = 0 |
| 74 | for item in match: |
| 75 | if not it[n]==item: raise AssertionError |
| 76 | n+=1 |
| 77 | try: |
| 78 | it[n] |
| 79 | except IndexError: |
| 80 | pass |
| 81 | else: |
| 82 | raise AssertionError("Too many items from __getitem__",it) |
| 83 | |
| 84 | try: |
| 85 | iter, StopIteration |
| 86 | except NameError: |
| 87 | pass |
| 88 | else: |
| 89 | # Only test iter mode under 2.2+ |
| 90 | it = make_it() |
| 91 | if not iter(it) is it: raise AssertionError |
| 92 | for item in match: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 93 | if not next(it) == item: raise AssertionError |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 94 | try: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 95 | next(it) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 96 | except StopIteration: |
| 97 | pass |
| 98 | else: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 99 | raise AssertionError("Too many items from .__next__()", it) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 100 | |
| 101 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 102 | class IntegrationTests(TestCase): |
| 103 | |
| 104 | def check_hello(self, out, has_length=True): |
Senthil Kumaran | a5e0eaf | 2012-07-07 14:29:58 -0700 | [diff] [blame] | 105 | pyver = (python_implementation() + "/" + |
| 106 | sys.version.split()[0]) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 107 | self.assertEqual(out, |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 108 | ("HTTP/1.0 200 OK\r\n" |
Senthil Kumaran | a5e0eaf | 2012-07-07 14:29:58 -0700 | [diff] [blame] | 109 | "Server: WSGIServer/0.2 " + pyver +"\r\n" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 110 | "Content-Type: text/plain\r\n" |
| 111 | "Date: Mon, 05 Jun 2006 18:49:54 GMT\r\n" + |
| 112 | (has_length and "Content-Length: 13\r\n" or "") + |
| 113 | "\r\n" |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 114 | "Hello, world!").encode("iso-8859-1") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 115 | ) |
| 116 | |
| 117 | def test_plain_hello(self): |
| 118 | out, err = run_amock() |
| 119 | self.check_hello(out) |
| 120 | |
| 121 | def test_validated_hello(self): |
| 122 | out, err = run_amock(validator(hello_app)) |
| 123 | # the middleware doesn't support len(), so content-length isn't there |
| 124 | self.check_hello(out, has_length=False) |
| 125 | |
| 126 | def test_simple_validation_error(self): |
| 127 | def bad_app(environ,start_response): |
| 128 | start_response("200 OK", ('Content-Type','text/plain')) |
| 129 | return ["Hello, world!"] |
| 130 | out, err = run_amock(validator(bad_app)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 131 | self.assertTrue(out.endswith( |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 132 | b"A server error occurred. Please contact the administrator." |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 133 | )) |
| 134 | self.assertEqual( |
| 135 | err.splitlines()[-2], |
| 136 | "AssertionError: Headers (('Content-Type', 'text/plain')) must" |
Martin v. Löwis | 250ad61 | 2008-04-07 05:43:42 +0000 | [diff] [blame] | 137 | " be of type list: <class 'tuple'>" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 138 | ) |
| 139 | |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 140 | def test_wsgi_input(self): |
| 141 | def bad_app(e,s): |
| 142 | e["wsgi.input"].read() |
Phillip J. Eby | e159422 | 2010-11-02 22:28:59 +0000 | [diff] [blame] | 143 | s("200 OK", [("Content-Type", "text/plain; charset=utf-8")]) |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 144 | return [b"data"] |
| 145 | out, err = run_amock(validator(bad_app)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 146 | self.assertTrue(out.endswith( |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 147 | b"A server error occurred. Please contact the administrator." |
| 148 | )) |
| 149 | self.assertEqual( |
| 150 | err.splitlines()[-2], "AssertionError" |
| 151 | ) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 152 | |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 153 | def test_bytes_validation(self): |
| 154 | def app(e, s): |
Phillip J. Eby | e159422 | 2010-11-02 22:28:59 +0000 | [diff] [blame] | 155 | s("200 OK", [ |
| 156 | ("Content-Type", "text/plain; charset=utf-8"), |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 157 | ("Date", "Wed, 24 Dec 2008 13:29:32 GMT"), |
| 158 | ]) |
| 159 | return [b"data"] |
| 160 | out, err = run_amock(validator(app)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 161 | self.assertTrue(err.endswith('"GET / HTTP/1.0" 200 4\n')) |
Antoine Pitrou | 5f81741 | 2009-01-03 18:49:41 +0000 | [diff] [blame] | 162 | ver = sys.version.split()[0].encode('ascii') |
Senthil Kumaran | a5e0eaf | 2012-07-07 14:29:58 -0700 | [diff] [blame] | 163 | py = python_implementation().encode('ascii') |
| 164 | pyver = py + b"/" + ver |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 165 | self.assertEqual( |
| 166 | b"HTTP/1.0 200 OK\r\n" |
Senthil Kumaran | a5e0eaf | 2012-07-07 14:29:58 -0700 | [diff] [blame] | 167 | b"Server: WSGIServer/0.2 "+ pyver + b"\r\n" |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 168 | b"Content-Type: text/plain; charset=utf-8\r\n" |
| 169 | b"Date: Wed, 24 Dec 2008 13:29:32 GMT\r\n" |
| 170 | b"\r\n" |
| 171 | b"data", |
| 172 | out) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 173 | |
| 174 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 175 | class UtilityTests(TestCase): |
| 176 | |
| 177 | def checkShift(self,sn_in,pi_in,part,sn_out,pi_out): |
| 178 | env = {'SCRIPT_NAME':sn_in,'PATH_INFO':pi_in} |
| 179 | util.setup_testing_defaults(env) |
| 180 | self.assertEqual(util.shift_path_info(env),part) |
| 181 | self.assertEqual(env['PATH_INFO'],pi_out) |
| 182 | self.assertEqual(env['SCRIPT_NAME'],sn_out) |
| 183 | return env |
| 184 | |
| 185 | def checkDefault(self, key, value, alt=None): |
| 186 | # Check defaulting when empty |
| 187 | env = {} |
| 188 | util.setup_testing_defaults(env) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 189 | if isinstance(value, StringIO): |
| 190 | self.assertIsInstance(env[key], StringIO) |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 191 | elif isinstance(value,BytesIO): |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 192 | self.assertIsInstance(env[key],BytesIO) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 193 | else: |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 194 | self.assertEqual(env[key], value) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 195 | |
| 196 | # Check existing value |
| 197 | env = {key:alt} |
| 198 | util.setup_testing_defaults(env) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 199 | self.assertTrue(env[key] is alt) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 200 | |
| 201 | def checkCrossDefault(self,key,value,**kw): |
| 202 | util.setup_testing_defaults(kw) |
| 203 | self.assertEqual(kw[key],value) |
| 204 | |
| 205 | def checkAppURI(self,uri,**kw): |
| 206 | util.setup_testing_defaults(kw) |
| 207 | self.assertEqual(util.application_uri(kw),uri) |
| 208 | |
| 209 | def checkReqURI(self,uri,query=1,**kw): |
| 210 | util.setup_testing_defaults(kw) |
| 211 | self.assertEqual(util.request_uri(kw,query),uri) |
| 212 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 213 | def checkFW(self,text,size,match): |
| 214 | |
| 215 | def make_it(text=text,size=size): |
| 216 | return util.FileWrapper(StringIO(text),size) |
| 217 | |
| 218 | compare_generic_iter(make_it,match) |
| 219 | |
| 220 | it = make_it() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 221 | self.assertFalse(it.filelike.closed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 222 | |
| 223 | for item in it: |
| 224 | pass |
| 225 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 226 | self.assertFalse(it.filelike.closed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 227 | |
| 228 | it.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 229 | self.assertTrue(it.filelike.closed) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 230 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 231 | def testSimpleShifts(self): |
| 232 | self.checkShift('','/', '', '/', '') |
| 233 | self.checkShift('','/x', 'x', '/x', '') |
| 234 | self.checkShift('/','', None, '/', '') |
| 235 | self.checkShift('/a','/x/y', 'x', '/a/x', '/y') |
| 236 | self.checkShift('/a','/x/', 'x', '/a/x', '/') |
| 237 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 238 | def testNormalizedShifts(self): |
| 239 | self.checkShift('/a/b', '/../y', '..', '/a', '/y') |
| 240 | self.checkShift('', '/../y', '..', '', '/y') |
| 241 | self.checkShift('/a/b', '//y', 'y', '/a/b/y', '') |
| 242 | self.checkShift('/a/b', '//y/', 'y', '/a/b/y', '/') |
| 243 | self.checkShift('/a/b', '/./y', 'y', '/a/b/y', '') |
| 244 | self.checkShift('/a/b', '/./y/', 'y', '/a/b/y', '/') |
| 245 | self.checkShift('/a/b', '///./..//y/.//', '..', '/a', '/y/') |
| 246 | self.checkShift('/a/b', '///', '', '/a/b/', '') |
| 247 | self.checkShift('/a/b', '/.//', '', '/a/b/', '') |
| 248 | self.checkShift('/a/b', '/x//', 'x', '/a/b/x', '/') |
| 249 | self.checkShift('/a/b', '/.', None, '/a/b', '') |
| 250 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 251 | def testDefaults(self): |
| 252 | for key, value in [ |
| 253 | ('SERVER_NAME','127.0.0.1'), |
| 254 | ('SERVER_PORT', '80'), |
| 255 | ('SERVER_PROTOCOL','HTTP/1.0'), |
| 256 | ('HTTP_HOST','127.0.0.1'), |
| 257 | ('REQUEST_METHOD','GET'), |
| 258 | ('SCRIPT_NAME',''), |
| 259 | ('PATH_INFO','/'), |
| 260 | ('wsgi.version', (1,0)), |
| 261 | ('wsgi.run_once', 0), |
| 262 | ('wsgi.multithread', 0), |
| 263 | ('wsgi.multiprocess', 0), |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 264 | ('wsgi.input', BytesIO()), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 265 | ('wsgi.errors', StringIO()), |
| 266 | ('wsgi.url_scheme','http'), |
| 267 | ]: |
| 268 | self.checkDefault(key,value) |
| 269 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 270 | def testCrossDefaults(self): |
| 271 | self.checkCrossDefault('HTTP_HOST',"foo.bar",SERVER_NAME="foo.bar") |
| 272 | self.checkCrossDefault('wsgi.url_scheme',"https",HTTPS="on") |
| 273 | self.checkCrossDefault('wsgi.url_scheme',"https",HTTPS="1") |
| 274 | self.checkCrossDefault('wsgi.url_scheme',"https",HTTPS="yes") |
| 275 | self.checkCrossDefault('wsgi.url_scheme',"http",HTTPS="foo") |
| 276 | self.checkCrossDefault('SERVER_PORT',"80",HTTPS="foo") |
| 277 | self.checkCrossDefault('SERVER_PORT',"443",HTTPS="on") |
| 278 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 279 | def testGuessScheme(self): |
| 280 | self.assertEqual(util.guess_scheme({}), "http") |
| 281 | self.assertEqual(util.guess_scheme({'HTTPS':"foo"}), "http") |
| 282 | self.assertEqual(util.guess_scheme({'HTTPS':"on"}), "https") |
| 283 | self.assertEqual(util.guess_scheme({'HTTPS':"yes"}), "https") |
| 284 | self.assertEqual(util.guess_scheme({'HTTPS':"1"}), "https") |
| 285 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 286 | def testAppURIs(self): |
| 287 | self.checkAppURI("http://127.0.0.1/") |
| 288 | self.checkAppURI("http://127.0.0.1/spam", SCRIPT_NAME="/spam") |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 289 | self.checkAppURI("http://127.0.0.1/sp%C3%A4m", SCRIPT_NAME="/späm") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 290 | self.checkAppURI("http://spam.example.com:2071/", |
| 291 | HTTP_HOST="spam.example.com:2071", SERVER_PORT="2071") |
| 292 | self.checkAppURI("http://spam.example.com/", |
| 293 | SERVER_NAME="spam.example.com") |
| 294 | self.checkAppURI("http://127.0.0.1/", |
| 295 | HTTP_HOST="127.0.0.1", SERVER_NAME="spam.example.com") |
| 296 | self.checkAppURI("https://127.0.0.1/", HTTPS="on") |
| 297 | self.checkAppURI("http://127.0.0.1:8000/", SERVER_PORT="8000", |
| 298 | HTTP_HOST=None) |
| 299 | |
| 300 | def testReqURIs(self): |
| 301 | self.checkReqURI("http://127.0.0.1/") |
| 302 | self.checkReqURI("http://127.0.0.1/spam", SCRIPT_NAME="/spam") |
Guido van Rossum | 52dbbb9 | 2008-08-18 21:44:30 +0000 | [diff] [blame] | 303 | self.checkReqURI("http://127.0.0.1/sp%C3%A4m", SCRIPT_NAME="/späm") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 304 | self.checkReqURI("http://127.0.0.1/spammity/spam", |
| 305 | SCRIPT_NAME="/spammity", PATH_INFO="/spam") |
Senthil Kumaran | 299fa4c | 2010-12-29 06:25:42 +0000 | [diff] [blame] | 306 | self.checkReqURI("http://127.0.0.1/spammity/spam;ham", |
| 307 | SCRIPT_NAME="/spammity", PATH_INFO="/spam;ham") |
| 308 | self.checkReqURI("http://127.0.0.1/spammity/spam;cookie=1234,5678", |
| 309 | SCRIPT_NAME="/spammity", PATH_INFO="/spam;cookie=1234,5678") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 310 | self.checkReqURI("http://127.0.0.1/spammity/spam?say=ni", |
| 311 | SCRIPT_NAME="/spammity", PATH_INFO="/spam",QUERY_STRING="say=ni") |
| 312 | self.checkReqURI("http://127.0.0.1/spammity/spam", 0, |
| 313 | SCRIPT_NAME="/spammity", PATH_INFO="/spam",QUERY_STRING="say=ni") |
| 314 | |
| 315 | def testFileWrapper(self): |
| 316 | self.checkFW("xyz"*50, 120, ["xyz"*40,"xyz"*10]) |
| 317 | |
| 318 | def testHopByHop(self): |
| 319 | for hop in ( |
| 320 | "Connection Keep-Alive Proxy-Authenticate Proxy-Authorization " |
| 321 | "TE Trailers Transfer-Encoding Upgrade" |
| 322 | ).split(): |
| 323 | for alt in hop, hop.title(), hop.upper(), hop.lower(): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 324 | self.assertTrue(util.is_hop_by_hop(alt)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 325 | |
| 326 | # Not comprehensive, just a few random header names |
| 327 | for hop in ( |
| 328 | "Accept Cache-Control Date Pragma Trailer Via Warning" |
| 329 | ).split(): |
| 330 | for alt in hop, hop.title(), hop.upper(), hop.lower(): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 331 | self.assertFalse(util.is_hop_by_hop(alt)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 332 | |
| 333 | class HeaderTests(TestCase): |
| 334 | |
| 335 | def testMappingInterface(self): |
| 336 | test = [('x','y')] |
| 337 | self.assertEqual(len(Headers([])),0) |
| 338 | self.assertEqual(len(Headers(test[:])),1) |
| 339 | self.assertEqual(Headers(test[:]).keys(), ['x']) |
| 340 | self.assertEqual(Headers(test[:]).values(), ['y']) |
| 341 | self.assertEqual(Headers(test[:]).items(), test) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 342 | self.assertFalse(Headers(test).items() is test) # must be copy! |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 343 | |
| 344 | h=Headers([]) |
| 345 | del h['foo'] # should not raise an error |
| 346 | |
| 347 | h['Foo'] = 'bar' |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 348 | for m in h.__contains__, h.get, h.get_all, h.__getitem__: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 349 | self.assertTrue(m('foo')) |
| 350 | self.assertTrue(m('Foo')) |
| 351 | self.assertTrue(m('FOO')) |
| 352 | self.assertFalse(m('bar')) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 353 | |
| 354 | self.assertEqual(h['foo'],'bar') |
| 355 | h['foo'] = 'baz' |
| 356 | self.assertEqual(h['FOO'],'baz') |
| 357 | self.assertEqual(h.get_all('foo'),['baz']) |
| 358 | |
| 359 | self.assertEqual(h.get("foo","whee"), "baz") |
| 360 | self.assertEqual(h.get("zoo","whee"), "whee") |
| 361 | self.assertEqual(h.setdefault("foo","whee"), "baz") |
| 362 | self.assertEqual(h.setdefault("zoo","whee"), "whee") |
| 363 | self.assertEqual(h["foo"],"baz") |
| 364 | self.assertEqual(h["zoo"],"whee") |
| 365 | |
| 366 | def testRequireList(self): |
| 367 | self.assertRaises(TypeError, Headers, "foo") |
| 368 | |
| 369 | |
| 370 | def testExtras(self): |
| 371 | h = Headers([]) |
| 372 | self.assertEqual(str(h),'\r\n') |
| 373 | |
| 374 | h.add_header('foo','bar',baz="spam") |
| 375 | self.assertEqual(h['foo'], 'bar; baz="spam"') |
| 376 | self.assertEqual(str(h),'foo: bar; baz="spam"\r\n\r\n') |
| 377 | |
| 378 | h.add_header('Foo','bar',cheese=None) |
| 379 | self.assertEqual(h.get_all('foo'), |
| 380 | ['bar; baz="spam"', 'bar; cheese']) |
| 381 | |
| 382 | self.assertEqual(str(h), |
| 383 | 'foo: bar; baz="spam"\r\n' |
| 384 | 'Foo: bar; cheese\r\n' |
| 385 | '\r\n' |
| 386 | ) |
| 387 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 388 | class ErrorHandler(BaseCGIHandler): |
| 389 | """Simple handler subclass for testing BaseHandler""" |
| 390 | |
Antoine Pitrou | 8f5731b | 2009-11-03 17:20:10 +0000 | [diff] [blame] | 391 | # BaseHandler records the OS environment at import time, but envvars |
| 392 | # might have been changed later by other tests, which trips up |
| 393 | # HandlerTests.testEnviron(). |
| 394 | os_environ = dict(os.environ.items()) |
| 395 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 396 | def __init__(self,**kw): |
| 397 | setup_testing_defaults(kw) |
| 398 | BaseCGIHandler.__init__( |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 399 | self, BytesIO(), BytesIO(), StringIO(), kw, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 400 | multithread=True, multiprocess=True |
| 401 | ) |
| 402 | |
| 403 | class TestHandler(ErrorHandler): |
| 404 | """Simple handler subclass for testing BaseHandler, w/error passthru""" |
| 405 | |
| 406 | def handle_error(self): |
| 407 | raise # for testing, we want to see what's happening |
| 408 | |
| 409 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 410 | class HandlerTests(TestCase): |
| 411 | |
| 412 | def checkEnvironAttrs(self, handler): |
| 413 | env = handler.environ |
| 414 | for attr in [ |
| 415 | 'version','multithread','multiprocess','run_once','file_wrapper' |
| 416 | ]: |
| 417 | if attr=='file_wrapper' and handler.wsgi_file_wrapper is None: |
| 418 | continue |
| 419 | self.assertEqual(getattr(handler,'wsgi_'+attr),env['wsgi.'+attr]) |
| 420 | |
| 421 | def checkOSEnviron(self,handler): |
| 422 | empty = {}; setup_testing_defaults(empty) |
| 423 | env = handler.environ |
| 424 | from os import environ |
| 425 | for k,v in environ.items(): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 426 | if k not in empty: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 427 | self.assertEqual(env[k],v) |
| 428 | for k,v in empty.items(): |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 429 | self.assertIn(k, env) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 430 | |
| 431 | def testEnviron(self): |
| 432 | h = TestHandler(X="Y") |
| 433 | h.setup_environ() |
| 434 | self.checkEnvironAttrs(h) |
| 435 | self.checkOSEnviron(h) |
| 436 | self.assertEqual(h.environ["X"],"Y") |
| 437 | |
| 438 | def testCGIEnviron(self): |
| 439 | h = BaseCGIHandler(None,None,None,{}) |
| 440 | h.setup_environ() |
| 441 | for key in 'wsgi.url_scheme', 'wsgi.input', 'wsgi.errors': |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 442 | self.assertIn(key, h.environ) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 443 | |
| 444 | def testScheme(self): |
| 445 | h=TestHandler(HTTPS="on"); h.setup_environ() |
| 446 | self.assertEqual(h.environ['wsgi.url_scheme'],'https') |
| 447 | h=TestHandler(); h.setup_environ() |
| 448 | self.assertEqual(h.environ['wsgi.url_scheme'],'http') |
| 449 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 450 | def testAbstractMethods(self): |
| 451 | h = BaseHandler() |
| 452 | for name in [ |
| 453 | '_flush','get_stdin','get_stderr','add_cgi_vars' |
| 454 | ]: |
| 455 | self.assertRaises(NotImplementedError, getattr(h,name)) |
| 456 | self.assertRaises(NotImplementedError, h._write, "test") |
| 457 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 458 | def testContentLength(self): |
| 459 | # Demo one reason iteration is better than write()... ;) |
| 460 | |
| 461 | def trivial_app1(e,s): |
| 462 | s('200 OK',[]) |
Phillip J. Eby | e159422 | 2010-11-02 22:28:59 +0000 | [diff] [blame] | 463 | return [e['wsgi.url_scheme'].encode('iso-8859-1')] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 464 | |
| 465 | def trivial_app2(e,s): |
Phillip J. Eby | e159422 | 2010-11-02 22:28:59 +0000 | [diff] [blame] | 466 | s('200 OK',[])(e['wsgi.url_scheme'].encode('iso-8859-1')) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 467 | return [] |
| 468 | |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 469 | def trivial_app3(e,s): |
| 470 | s('200 OK',[]) |
| 471 | return ['\u0442\u0435\u0441\u0442'.encode("utf-8")] |
| 472 | |
Antoine Pitrou | b715fac | 2011-01-06 17:17:04 +0000 | [diff] [blame] | 473 | def trivial_app4(e,s): |
| 474 | # Simulate a response to a HEAD request |
| 475 | s('200 OK',[('Content-Length', '12345')]) |
| 476 | return [] |
| 477 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 478 | h = TestHandler() |
| 479 | h.run(trivial_app1) |
| 480 | self.assertEqual(h.stdout.getvalue(), |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 481 | ("Status: 200 OK\r\n" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 482 | "Content-Length: 4\r\n" |
| 483 | "\r\n" |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 484 | "http").encode("iso-8859-1")) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 485 | |
| 486 | h = TestHandler() |
| 487 | h.run(trivial_app2) |
| 488 | self.assertEqual(h.stdout.getvalue(), |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 489 | ("Status: 200 OK\r\n" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 490 | "\r\n" |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 491 | "http").encode("iso-8859-1")) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 492 | |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 493 | h = TestHandler() |
| 494 | h.run(trivial_app3) |
| 495 | self.assertEqual(h.stdout.getvalue(), |
| 496 | b'Status: 200 OK\r\n' |
| 497 | b'Content-Length: 8\r\n' |
| 498 | b'\r\n' |
| 499 | b'\xd1\x82\xd0\xb5\xd1\x81\xd1\x82') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 500 | |
Antoine Pitrou | b715fac | 2011-01-06 17:17:04 +0000 | [diff] [blame] | 501 | h = TestHandler() |
| 502 | h.run(trivial_app4) |
| 503 | self.assertEqual(h.stdout.getvalue(), |
| 504 | b'Status: 200 OK\r\n' |
| 505 | b'Content-Length: 12345\r\n' |
| 506 | b'\r\n') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 507 | |
| 508 | def testBasicErrorOutput(self): |
| 509 | |
| 510 | def non_error_app(e,s): |
| 511 | s('200 OK',[]) |
| 512 | return [] |
| 513 | |
| 514 | def error_app(e,s): |
| 515 | raise AssertionError("This should be caught by handler") |
| 516 | |
| 517 | h = ErrorHandler() |
| 518 | h.run(non_error_app) |
| 519 | self.assertEqual(h.stdout.getvalue(), |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 520 | ("Status: 200 OK\r\n" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 521 | "Content-Length: 0\r\n" |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 522 | "\r\n").encode("iso-8859-1")) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 523 | self.assertEqual(h.stderr.getvalue(),"") |
| 524 | |
| 525 | h = ErrorHandler() |
| 526 | h.run(error_app) |
| 527 | self.assertEqual(h.stdout.getvalue(), |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 528 | ("Status: %s\r\n" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 529 | "Content-Type: text/plain\r\n" |
| 530 | "Content-Length: %d\r\n" |
Phillip J. Eby | e159422 | 2010-11-02 22:28:59 +0000 | [diff] [blame] | 531 | "\r\n" % (h.error_status,len(h.error_body))).encode('iso-8859-1') |
| 532 | + h.error_body) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 533 | |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 534 | self.assertIn("AssertionError", h.stderr.getvalue()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 535 | |
| 536 | def testErrorAfterOutput(self): |
Phillip J. Eby | e159422 | 2010-11-02 22:28:59 +0000 | [diff] [blame] | 537 | MSG = b"Some output has been sent" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 538 | def error_app(e,s): |
| 539 | s("200 OK",[])(MSG) |
| 540 | raise AssertionError("This should be caught by handler") |
| 541 | |
| 542 | h = ErrorHandler() |
| 543 | h.run(error_app) |
| 544 | self.assertEqual(h.stdout.getvalue(), |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 545 | ("Status: 200 OK\r\n" |
Phillip J. Eby | e159422 | 2010-11-02 22:28:59 +0000 | [diff] [blame] | 546 | "\r\n".encode("iso-8859-1")+MSG)) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 547 | self.assertIn("AssertionError", h.stderr.getvalue()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 548 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 549 | def testHeaderFormats(self): |
| 550 | |
| 551 | def non_error_app(e,s): |
| 552 | s('200 OK',[]) |
| 553 | return [] |
| 554 | |
| 555 | stdpat = ( |
| 556 | r"HTTP/%s 200 OK\r\n" |
| 557 | r"Date: \w{3}, [ 0123]\d \w{3} \d{4} \d\d:\d\d:\d\d GMT\r\n" |
| 558 | r"%s" r"Content-Length: 0\r\n" r"\r\n" |
| 559 | ) |
| 560 | shortpat = ( |
| 561 | "Status: 200 OK\r\n" "Content-Length: 0\r\n" "\r\n" |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 562 | ).encode("iso-8859-1") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 563 | |
| 564 | for ssw in "FooBar/1.0", None: |
| 565 | sw = ssw and "Server: %s\r\n" % ssw or "" |
| 566 | |
| 567 | for version in "1.0", "1.1": |
| 568 | for proto in "HTTP/0.9", "HTTP/1.0", "HTTP/1.1": |
| 569 | |
| 570 | h = TestHandler(SERVER_PROTOCOL=proto) |
| 571 | h.origin_server = False |
| 572 | h.http_version = version |
| 573 | h.server_software = ssw |
| 574 | h.run(non_error_app) |
| 575 | self.assertEqual(shortpat,h.stdout.getvalue()) |
| 576 | |
| 577 | h = TestHandler(SERVER_PROTOCOL=proto) |
| 578 | h.origin_server = True |
| 579 | h.http_version = version |
| 580 | h.server_software = ssw |
| 581 | h.run(non_error_app) |
| 582 | if proto=="HTTP/0.9": |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 583 | self.assertEqual(h.stdout.getvalue(),b"") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 584 | else: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 585 | self.assertTrue( |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 586 | re.match((stdpat%(version,sw)).encode("iso-8859-1"), |
| 587 | h.stdout.getvalue()), |
| 588 | ((stdpat%(version,sw)).encode("iso-8859-1"), |
| 589 | h.stdout.getvalue()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 590 | ) |
| 591 | |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 592 | def testBytesData(self): |
| 593 | def app(e, s): |
Phillip J. Eby | e159422 | 2010-11-02 22:28:59 +0000 | [diff] [blame] | 594 | s("200 OK", [ |
| 595 | ("Content-Type", "text/plain; charset=utf-8"), |
Antoine Pitrou | 38a66ad | 2009-01-03 18:41:49 +0000 | [diff] [blame] | 596 | ]) |
| 597 | return [b"data"] |
| 598 | |
| 599 | h = TestHandler() |
| 600 | h.run(app) |
| 601 | self.assertEqual(b"Status: 200 OK\r\n" |
| 602 | b"Content-Type: text/plain; charset=utf-8\r\n" |
| 603 | b"Content-Length: 4\r\n" |
| 604 | b"\r\n" |
| 605 | b"data", |
| 606 | h.stdout.getvalue()) |
| 607 | |
Antoine Pitrou | ae247a5 | 2012-10-21 14:09:05 +0200 | [diff] [blame] | 608 | def testCloseOnError(self): |
| 609 | side_effects = {'close_called': False} |
| 610 | MSG = b"Some output has been sent" |
| 611 | def error_app(e,s): |
| 612 | s("200 OK",[])(MSG) |
| 613 | class CrashyIterable(object): |
| 614 | def __iter__(self): |
| 615 | while True: |
| 616 | yield b'blah' |
| 617 | raise AssertionError("This should be caught by handler") |
| 618 | def close(self): |
| 619 | side_effects['close_called'] = True |
| 620 | return CrashyIterable() |
| 621 | |
| 622 | h = ErrorHandler() |
| 623 | h.run(error_app) |
| 624 | self.assertEqual(side_effects['close_called'], True) |
| 625 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 626 | |
| 627 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 628 | support.run_unittest(__name__) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 629 | |
| 630 | if __name__ == "__main__": |
| 631 | test_main() |