| 1 |
import json |
| 2 |
|
| 3 |
from webtest import TestApp |
| 4 |
|
| 5 |
import pecan |
| 6 |
from pecan.middleware.errordocument import ErrorDocumentMiddleware |
| 7 |
from pecan.middleware.recursive import RecursiveMiddleware |
| 8 |
from pecan.tests import PecanTestCase |
| 9 |
|
| 10 |
|
| 11 |
def four_oh_four_app(environ, start_response): |
| 12 |
if environ['PATH_INFO'].startswith('/error'): |
| 13 |
code = environ['PATH_INFO'].split('/')[2].encode('utf-8') |
| 14 |
start_response("200 OK", [('Content-type', 'text/plain')]) |
| 15 |
|
| 16 |
body = b"Error: %s" % code |
| 17 |
if environ['QUERY_STRING']: |
| 18 |
body += b"\nQS: %s" % environ['QUERY_STRING'].encode('utf-8') |
| 19 |
return [body] |
| 20 |
start_response("404 Not Found", [('Content-type', 'text/plain')]) |
| 21 |
return [] |
| 22 |
|
| 23 |
|
| 24 |
class TestErrorDocumentMiddleware(PecanTestCase): |
| 25 |
|
| 26 |
def setUp(self): |
| 27 |
super(TestErrorDocumentMiddleware, self).setUp() |
| 28 |
self.app = TestApp(RecursiveMiddleware(ErrorDocumentMiddleware( |
| 29 |
four_oh_four_app, {404: '/error/404'} |
| 30 |
))) |
| 31 |
|
| 32 |
def test_hit_error_page(self): |
| 33 |
r = self.app.get('/error/404') |
| 34 |
assert r.status_int == 200 |
| 35 |
assert r.body == b'Error: 404' |
| 36 |
|
| 37 |
def test_middleware_routes_to_404_message(self): |
| 38 |
r = self.app.get('/', expect_errors=True) |
| 39 |
assert r.status_int == 404 |
| 40 |
assert r.body == b'Error: 404' |
| 41 |
|
| 42 |
def test_error_endpoint_with_query_string(self): |
| 43 |
app = TestApp(RecursiveMiddleware(ErrorDocumentMiddleware( |
| 44 |
four_oh_four_app, {404: '/error/404?foo=bar'} |
| 45 |
))) |
| 46 |
r = app.get('/', expect_errors=True) |
| 47 |
assert r.status_int == 404 |
| 48 |
assert r.body == b'Error: 404\nQS: foo=bar' |
| 49 |
|
| 50 |
def test_error_with_recursion_loop(self): |
| 51 |
app = TestApp(RecursiveMiddleware(ErrorDocumentMiddleware( |
| 52 |
four_oh_four_app, {404: '/'} |
| 53 |
))) |
| 54 |
r = app.get('/', expect_errors=True) |
| 55 |
assert r.status_int == 404 |
| 56 |
assert r.body == ( |
| 57 |
b'Error: 404 Not Found. (Error page could not be fetched)' |
| 58 |
) |
| 59 |
|
| 60 |
def test_original_exception(self): |
| 61 |
|
| 62 |
class RootController(object): |
| 63 |
|
| 64 |
@pecan.expose() |
| 65 |
def index(self): |
| 66 |
if pecan.request.method != 'POST': |
| 67 |
pecan.abort(405, 'You have to POST, dummy!') |
| 68 |
return 'Hello, World!' |
| 69 |
|
| 70 |
@pecan.expose('json') |
| 71 |
def error(self, status): |
| 72 |
return dict( |
| 73 |
status=int(status), |
| 74 |
reason=pecan.request.environ[ |
| 75 |
'pecan.original_exception' |
| 76 |
].detail |
| 77 |
) |
| 78 |
|
| 79 |
app = pecan.Pecan(RootController()) |
| 80 |
app = RecursiveMiddleware(ErrorDocumentMiddleware(app, { |
| 81 |
405: '/error/405' |
| 82 |
})) |
| 83 |
app = TestApp(app) |
| 84 |
|
| 85 |
assert app.post('/').status_int == 200 |
| 86 |
r = app.get('/', expect_errors=405) |
| 87 |
assert r.status_int == 405 |
| 88 |
|
| 89 |
resp = json.loads(r.body.decode()) |
| 90 |
assert resp['status'] == 405 |
| 91 |
assert resp['reason'] == 'You have to POST, dummy!' |