/usr/share/pyshared/twisted/web2/test/test_wsgi.py is in python-twisted-web2 8.1.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | # Copyright (c) 2005-2007 Twisted Matrix Laboratories.
# See LICENSE for details.
import time
from twisted.web2.test.test_server import BaseCase
from twisted.internet import reactor, interfaces, defer
from twisted.python import log
if interfaces.IReactorThreads(reactor, None) is not None:
from twisted.web2.wsgi import WSGIResource as WSGI
else:
WSGI = None
class TestError(Exception):
pass
class TestContainer(BaseCase):
"""
Tests various applications with the WSGI container.
"""
def flushErrors(self, result, error):
"""
Flush the specified C{error] and forward C{result}.
"""
self.flushLoggedErrors(error)
return result
def test_getContainedResource(self):
"""
Test that non-blocking WSGI applications render properly.
"""
def application(environ, start_response):
status = '200 OK'
response_headers = [('Content-type','text/html')]
writer = start_response(status, response_headers)
writer('<html>')
return ['<h1>Some HTML</h1>',
'</html>']
return self.assertResponse(
(WSGI(application), 'http://host/'),
(200, {"Content-Length": None}, '<html><h1>Some HTML</h1></html>'))
def test_getBlockingResource(self):
"""
Test that blocking WSGI applications render properly.
"""
def application(environ, start_response):
"""
Simplest possible application object.
"""
status = '200 OK'
response_headers = [('Content-type','text/html')]
writer = start_response(status, response_headers)
writer('<h1>A little bit')
time.sleep(1)
writer(' of HTML</h1>')
time.sleep(1)
return ['<p>Hello!</p>']
return self.assertResponse(
(WSGI(application), 'http://host/'),
(200, {"Content-Length": None},
'<h1>A little bit of HTML</h1><p>Hello!</p>'))
def test_responseCode(self):
"""
Test that WSGIResource handles strange response codes properly.
"""
def application(environ, start_response):
status = '314'
response_headers = [('Content-type','text/html')]
writer = start_response(status, response_headers)
return []
return self.assertResponse(
(WSGI(application), 'http://host/'),
(314, {"Content-Length": 0}, ''))
def test_errorfulResource(self):
def application(environ, start_response):
raise TestError("This is an expected error")
return self.assertResponse(
(WSGI(application), 'http://host/'),
(500, {}, None)).addBoth(self.flushErrors, TestError)
def test_errorfulResource2(self):
def application(environ, start_response):
write = start_response("200 OK", {})
write("Foo")
raise TestError("This is an expected error")
return self.assertResponse(
(WSGI(application), 'http://host/'),
(200, {"Content-Length": None}, "Foo"), failure=True
).addBoth(self.flushErrors, TestError)
def test_errorfulIterator(self):
def iterator():
raise TestError("This is an expected error")
def application(environ, start_response):
start_response("200 OK", {})
return iterator()
return self.assertResponse(
(WSGI(application), 'http://host/'),
(500, {}, None)).addBoth(self.flushErrors, TestError)
def test_errorfulIterator2(self):
def iterator():
yield "Foo"
yield "Bar"
raise TestError("This is also expected")
def application(environ, start_response):
start_response("200 OK", {})
return iterator()
return self.assertResponse(
(WSGI(application), 'http://host/'),
(200, {"Content-Length": None}, "FooBar"), failure=True
).addBoth(self.flushErrors, TestError)
def test_didntCallStartResponse(self):
def application(environ, start_response):
return ["Foo"]
return self.assertResponse(
(WSGI(application), 'http://host/'),
(500, {}, None)).addBoth(self.flushErrors, RuntimeError)
def test_calledStartResponseLate(self):
def application(environ, start_response):
start_response("200 OK", {})
yield "Foo"
return self.assertResponse(
(WSGI(application), 'http://host/'),
(200, {"Content-Length": None}, "Foo"))
def test_returnList(self):
def application(environ, start_response):
write = start_response("200 OK", {})
return ["Foo", "Bar"]
return self.assertResponse(
(WSGI(application), 'http://host/'),
(200, {"Content-Length": 6}, "FooBar"))
def test_readAllInput(self):
def application(environ, start_response):
input = environ['wsgi.input']
out = input.read(-1)
start_response("200 OK", {})
return [out]
return self.assertResponse(
(WSGI(application), 'http://host/', {}, None, None, '',
"This is some content"),
(200, {"Content-Length": 20}, "This is some content"))
def test_readInputLines(self):
def application(environ, start_response):
input = environ['wsgi.input']
out = 'X'.join(input.readlines())
start_response("200 OK", {})
return [out]
d = self.assertResponse(
(WSGI(application), 'http://host/', {}, None, None, '', "a\nb\nc"),
(200, {"Content-Length": 7}, "a\nXb\nXc"))
d.addCallback(lambda d: self.assertResponse(
(WSGI(application), 'http://host/', {}, None, None, '', "a\nb\n"),
(200, {"Content-Length": 5}, "a\nXb\n")))
return d
def test_readInputLineSizeNegZero(self):
"""
Test that calling wsgi.input.readline works with -1 and 0 and none.
"""
def application(environ, start_response):
input = environ['wsgi.input']
out = [input.read(5)] # 'Line '
out.extend(["X", input.readline(-1)]) # 'blah blah\n'
out.extend(["X", input.readline(0)]) # ''
out.extend(["X", input.readline(None)]) # 'Oh Line\n'
out.extend(["X", input.readline()]) # ''
start_response("200 OK", {})
return out
return self.assertResponse(
(WSGI(application), 'http://host/', {}, None, None, '',
"Line blah blah\nOh Line\n"),
(200, {"Content-Length": 27},
"Line Xblah blah\nXXOh Line\nX"))
def test_readInputLineSize(self):
"""
Test that readline() with a size works.
"""
def application(environ, start_response):
input = environ['wsgi.input']
out = [input.read(5)] # 'Line '
out.extend(["X", input.readline(5)]) # 'blah '
out.extend(["X", input.readline()]) # 'blah\n'
out.extend(["X", input.readline(1)]) # 'O'
out.extend(["X", input.readline()]) # 'h Line\n'
start_response("200 OK", {})
return out
return self.assertResponse(
(WSGI(application), 'http://host/', {}, None, None, '',
"Line blah blah\nOh Line\n"),
(200, {"Content-Length": 27},
"Line Xblah Xblah\nXOXh Line\n"))
def test_readInputMixed(self):
def application(environ, start_response):
input = environ['wsgi.input']
out = [input.read(5)]
out.extend(["X", input.readline()])
out.extend(["X", input.read(1)])
out.extend(["X", input.readline()])
start_response("200 OK", {})
return out
return self.assertResponse(
(WSGI(application), 'http://host/', {}, None, None, '',
"Line blah blah\nOh Line\n"),
(200, {"Content-Length": 26}, "Line Xblah blah\nXOXh Line\n"))
def test_readiter(self):
"""
Test that using wsgi.input as an iterator works.
"""
def application(environ, start_response):
input = environ['wsgi.input']
out = 'X'.join(input)
start_response("200 OK", {})
return [out]
return self.assertResponse(
(WSGI(application), 'http://host/', {}, None, None, '',
"Line blah blah\nOh Line\n"),
(200, {"Content-Length": 24}, "Line blah blah\nXOh Line\n"))
class TestWSGIEnvironment(BaseCase):
"""
Test that the WSGI container does everything we expect it to do
with the WSGI environment dictionary.
"""
def envApp(self, *varnames):
"""
Return a WSGI application that writes environment variables.
"""
def _app(environ, start_response):
status = '200'
response_headers = [('Content-type','text/html')]
writer = start_response(status, response_headers)
return ['%s=%r;' % (k, environ.get(k, '')) for k in varnames]
return _app
def assertEnv(self, uri, env, version=None, prepath=''):
"""
Check the value of the rendering envirnment against
the string returned by the testing WSGIApp.
"""
keys = env.keys()
keys.sort()
envstring = ''.join(['%s=%r;' % (k, v) for k, v in env.items()])
return self.assertResponse(
(WSGI(self.envApp(*keys)), uri, None, None, version, prepath),
(200, {}, envstring))
def test_wsgi_url_scheme(self):
"""
Check the value of C{wsgi.url_scheme} variable for the http and the
https cases.
"""
return defer.gatherResults([
self.assertEnv('https://host/', {'wsgi.url_scheme': 'https'}),
self.assertEnv('http://host/', {'wsgi.url_scheme': 'http'})
])
def test_SERVER_PROTOCOL(self):
"""
Check the value the C{SERVER_PROTOCOL} variable in the WSGI environment.
"""
return self.assertEnv('http://host/', {'SERVER_PROTOCOL': 'HTTP/1.1'})
def test_SERVER_PORT(self):
"""
Check the value of the C{SERVER_PORT} variable in the WSGI environment,
for different kind of URLs.
"""
return defer.gatherResults([
self.assertEnv('http://host/', {'SERVER_PORT': '80'}),
self.assertEnv('http://host:523/', {'SERVER_PORT': '523'}),
self.assertEnv('https://host/', {'SERVER_PORT': '443'}),
self.assertEnv('https://host:523/', {'SERVER_PORT': '523'}),
self.assertEnv('/foo', {'SERVER_PORT': '80'}, version=(1,0))
])
def test_SCRIPT_NAME(self):
"""
Check the value of C{SCRIPT_NAME}, depending of the prepath field.
"""
return defer.gatherResults([
self.assertEnv('http://host/', {'SCRIPT_NAME': ''}),
self.assertEnv('http://host/myscript/foobar',
{'SCRIPT_NAME': '/myscript', 'PATH_INFO': '/foobar'}, prepath='/myscript'),
self.assertEnv('http://host/myscript/foobar/',
{'SCRIPT_NAME': '/myscript/foobar', 'PATH_INFO': '/'}, prepath='/myscript/foobar'),
self.assertEnv('http://host/myscript/foobar?bar=baz',
{'SCRIPT_NAME': '/myscript', 'PATH_INFO': '/foobar'}, prepath='/myscript')
])
if WSGI is None:
for cls in (TestContainer, TestWSGIEnvironment):
setattr(cls, 'skip', 'Required thread support is missing, skipping')
|