/usr/lib/python2.7/dist-packages/pecan/tests/middleware/test_recursive.py is in python-pecan 0.6.1-2.
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 | from webtest import TestApp
from six import b as b_
from pecan.middleware.recursive import (RecursiveMiddleware,
ForwardRequestException)
from pecan.tests import PecanTestCase
def simple_app(environ, start_response):
start_response("200 OK", [('Content-type', 'text/plain')])
return [b_('requested page returned')]
def error_docs_app(environ, start_response):
if environ['PATH_INFO'] == '/not_found':
start_response("404 Not found", [('Content-type', 'text/plain')])
return [b_('Not found')]
elif environ['PATH_INFO'] == '/error':
start_response("200 OK", [('Content-type', 'text/plain')])
return [b_('Page not found')]
elif environ['PATH_INFO'] == '/recurse':
raise ForwardRequestException('/recurse')
else:
return simple_app(environ, start_response)
class Middleware(object):
def __init__(self, app, url='/error'):
self.app = app
self.url = url
def __call__(self, environ, start_response):
raise ForwardRequestException(self.url)
def forward(app):
app = TestApp(RecursiveMiddleware(app))
res = app.get('')
assert res.headers['content-type'] == 'text/plain'
assert res.status == '200 OK'
assert 'requested page returned' in res
res = app.get('/error')
assert res.headers['content-type'] == 'text/plain'
assert res.status == '200 OK'
assert 'Page not found' in res
res = app.get('/not_found')
assert res.headers['content-type'] == 'text/plain'
assert res.status == '200 OK'
assert 'Page not found' in res
try:
res = app.get('/recurse')
except AssertionError as e:
if str(e).startswith('Forwarding loop detected'):
pass
else:
raise AssertionError('Failed to detect forwarding loop')
class TestRecursiveMiddleware(PecanTestCase):
def test_ForwardRequest_url(self):
class TestForwardRequestMiddleware(Middleware):
def __call__(self, environ, start_response):
if environ['PATH_INFO'] != '/not_found':
return self.app(environ, start_response)
raise ForwardRequestException(self.url)
forward(TestForwardRequestMiddleware(error_docs_app))
def test_ForwardRequest_url_with_params(self):
class TestForwardRequestMiddleware(Middleware):
def __call__(self, environ, start_response):
if environ['PATH_INFO'] != '/not_found':
return self.app(environ, start_response)
raise ForwardRequestException(self.url + '?q=1')
forward(TestForwardRequestMiddleware(error_docs_app))
def test_ForwardRequest_environ(self):
class TestForwardRequestMiddleware(Middleware):
def __call__(self, environ, start_response):
if environ['PATH_INFO'] != '/not_found':
return self.app(environ, start_response)
environ['PATH_INFO'] = self.url
raise ForwardRequestException(environ=environ)
forward(TestForwardRequestMiddleware(error_docs_app))
def test_ForwardRequest_factory(self):
class TestForwardRequestMiddleware(Middleware):
def __call__(self, environ, start_response):
if environ['PATH_INFO'] != '/not_found':
return self.app(environ, start_response)
environ['PATH_INFO'] = self.url
def factory(app):
class WSGIApp(object):
def __init__(self, app):
self.app = app
def __call__(self, e, start_response):
def keep_status_start_response(status, headers,
exc_info=None):
return start_response(
'404 Not Found', headers, exc_info
)
return self.app(e, keep_status_start_response)
return WSGIApp(app)
raise ForwardRequestException(factory=factory)
app = TestForwardRequestMiddleware(error_docs_app)
app = TestApp(RecursiveMiddleware(app))
res = app.get('')
assert res.headers['content-type'] == 'text/plain'
assert res.status == '200 OK'
assert 'requested page returned' in res
res = app.get('/error')
assert res.headers['content-type'] == 'text/plain'
assert res.status == '200 OK'
assert 'Page not found' in res
res = app.get('/not_found', status=404)
assert res.headers['content-type'] == 'text/plain'
assert res.status == '404 Not Found' # Different status
assert 'Page not found' in res
try:
res = app.get('/recurse')
except AssertionError as e:
if str(e).startswith('Forwarding loop detected'):
pass
else:
raise AssertionError('Failed to detect forwarding loop')
def test_ForwardRequestException(self):
class TestForwardRequestExceptionMiddleware(Middleware):
def __call__(self, environ, start_response):
if environ['PATH_INFO'] != '/not_found':
return self.app(environ, start_response)
raise ForwardRequestException(path_info=self.url)
forward(TestForwardRequestExceptionMiddleware(error_docs_app))
|