summaryrefslogtreecommitdiff
path: root/tests/test_recursive.py
diff options
context:
space:
mode:
authormatt <matt@xcolour.net>2013-01-28 11:32:18 -0500
committermatt <matt@xcolour.net>2013-01-28 11:32:18 -0500
commit1afcb52d73271bbbd78f885451aa1b0e78c09871 (patch)
tree9145840d6036fcbc0b6647c88f679a567fa8c54d /tests/test_recursive.py
downloadpaste-git-stringio.tar.gz
Import StringIO so it can be used.stringio
Diffstat (limited to 'tests/test_recursive.py')
-rw-r--r--tests/test_recursive.py105
1 files changed, 105 insertions, 0 deletions
diff --git a/tests/test_recursive.py b/tests/test_recursive.py
new file mode 100644
index 0000000..e79d586
--- /dev/null
+++ b/tests/test_recursive.py
@@ -0,0 +1,105 @@
+from test_errordocument import error_docs_app, test_error_docs_app, simple_app
+from paste.fixture import *
+from paste.recursive import RecursiveMiddleware, ForwardRequestException
+
+def error_docs_app(environ, start_response):
+ if environ['PATH_INFO'] == '/not_found':
+ start_response("404 Not found", [('Content-type', 'text/plain')])
+ return ['Not found']
+ elif environ['PATH_INFO'] == '/error':
+ start_response("200 OK", [('Content-type', 'text/plain')])
+ return ['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.header('content-type') == 'text/plain'
+ assert res.full_status == '200 OK'
+ assert 'requested page returned' in res
+ res = app.get('/error')
+ assert res.header('content-type') == 'text/plain'
+ assert res.full_status == '200 OK'
+ assert 'Page not found' in res
+ res = app.get('/not_found')
+ assert res.header('content-type') == 'text/plain'
+ assert res.full_status == '200 OK'
+ assert 'Page not found' in res
+ try:
+ res = app.get('/recurse')
+ except AssertionError, e:
+ if str(e).startswith('Forwarding loop detected'):
+ pass
+ else:
+ raise AssertionError('Failed to detect forwarding loop')
+
+def test_ForwardRequest_url():
+ 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_environ():
+ 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():
+
+ from paste.errordocument import StatusKeeper
+
+ 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):
+ return StatusKeeper(app, status='404 Not Found', url='/error', headers=[])
+ raise ForwardRequestException(factory=factory)
+
+ app = TestForwardRequestMiddleware(error_docs_app)
+ app = TestApp(RecursiveMiddleware(app))
+ res = app.get('')
+ assert res.header('content-type') == 'text/plain'
+ assert res.full_status == '200 OK'
+ assert 'requested page returned' in res
+ res = app.get('/error')
+ assert res.header('content-type') == 'text/plain'
+ assert res.full_status == '200 OK'
+ assert 'Page not found' in res
+ res = app.get('/not_found', status=404)
+ assert res.header('content-type') == 'text/plain'
+ assert res.full_status == '404 Not Found' # Different status
+ assert 'Page not found' in res
+ try:
+ res = app.get('/recurse')
+ except AssertionError, e:
+ if str(e).startswith('Forwarding loop detected'):
+ pass
+ else:
+ raise AssertionError('Failed to detect forwarding loop')
+
+# Test Deprecated Code
+def test_ForwardRequestException():
+ 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))