summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthejimmyg <devnull@localhost>2006-07-20 18:56:13 +0000
committerthejimmyg <devnull@localhost>2006-07-20 18:56:13 +0000
commit69c574a0888df92cc7049f18fee47ec8fa3713cf (patch)
tree385a16ea2d9a8bca368b65b634b501873e4bcfa1
parent6dd80f403e7a96963d710a31ba0f40e00e51e844 (diff)
downloadpaste-69c574a0888df92cc7049f18fee47ec8fa3713cf.tar.gz
Fixed a bug in paste.errordocuments.forward and updated unit tests
-rw-r--r--paste/errordocument.py1
-rw-r--r--tests/test_errordocument.py64
-rw-r--r--tests/test_recursive.py105
3 files changed, 152 insertions, 18 deletions
diff --git a/paste/errordocument.py b/paste/errordocument.py
index b376fea..172ae11 100644
--- a/paste/errordocument.py
+++ b/paste/errordocument.py
@@ -95,6 +95,7 @@ def forward(app, codes):
raise TypeError('All status codes should be type int. '
'%s is not valid'%repr(code))
def error_codes_mapper(code, message, environ, global_conf, codes):
+ codes = codes['codes']
if codes.has_key(code):
return codes[code]
else:
diff --git a/tests/test_errordocument.py b/tests/test_errordocument.py
index 502b44d..57a01ab 100644
--- a/tests/test_errordocument.py
+++ b/tests/test_errordocument.py
@@ -1,21 +1,6 @@
-"""
-Error Document Support Test
-+++++++++++++++++++++++++++
-
-WARNING: These tests aren't yet finished. A call to test_ok() using
-not_found_app rather than simple_app currently fails complaining of
-start_response not having been called before content is returned.
-
-This isn't the full story since start_response will have been called
-by the original response but I need advice on how to modify the
-test suite to be able to test this.
-
-I also need to find out how to test that another response was
-correctly requested by the middleware.
-"""
-import os
-from paste.errordocument import forward, custom_forward
+from paste.errordocument import forward
from paste.fixture import *
+from paste.recursive import RecursiveMiddleware
def simple_app(environ, start_response):
start_response("200 OK", [('Content-type', 'text/plain')])
@@ -26,8 +11,51 @@ def not_found_app(environ, start_response):
return ['requested page returned']
def test_ok():
- app = TestApp(forward(simple_app, codes={404:'/error'}))
+ app = TestApp(simple_app)
res = app.get('')
assert res.header('content-type') == 'text/plain'
assert res.full_status == '200 OK'
assert 'requested page returned' in res
+
+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']
+ else:
+ return simple_app(environ, start_response)
+
+def test_error_docs_app():
+ app = TestApp(error_docs_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'
+ assert 'Not found' in res
+
+def test_forward():
+ app = forward(error_docs_app, codes={404:'/error'})
+ 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'
+ # Note changed response
+ assert 'Page not found' in res
+
diff --git a/tests/test_recursive.py b/tests/test_recursive.py
new file mode 100644
index 0000000..f00577a
--- /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:
+ 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')
+ 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))