• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from paste.errordocument import forward
2from paste.fixture import *
3from paste.recursive import RecursiveMiddleware
4
5def simple_app(environ, start_response):
6    start_response("200 OK", [('Content-type', 'text/plain')])
7    return [b'requested page returned']
8
9def not_found_app(environ, start_response):
10    start_response("404 Not found", [('Content-type', 'text/plain')])
11    return [b'requested page returned']
12
13def test_ok():
14    app = TestApp(simple_app)
15    res = app.get('')
16    assert res.header('content-type') == 'text/plain'
17    assert res.full_status == '200 OK'
18    assert 'requested page returned' in res
19
20def error_docs_app(environ, start_response):
21    if environ['PATH_INFO'] == '/not_found':
22        start_response("404 Not found", [('Content-type', 'text/plain')])
23        return [b'Not found']
24    elif environ['PATH_INFO'] == '/error':
25        start_response("200 OK", [('Content-type', 'text/plain')])
26        return [b'Page not found']
27    else:
28        return simple_app(environ, start_response)
29
30def test_error_docs_app():
31    app = TestApp(error_docs_app)
32    res = app.get('')
33    assert res.header('content-type') == 'text/plain'
34    assert res.full_status == '200 OK'
35    assert 'requested page returned' in res
36    res = app.get('/error')
37    assert res.header('content-type') == 'text/plain'
38    assert res.full_status == '200 OK'
39    assert 'Page not found' in res
40    res = app.get('/not_found', status=404)
41    assert res.header('content-type') == 'text/plain'
42    assert res.full_status == '404 Not found'
43    assert 'Not found' in res
44
45def test_forward():
46    app = forward(error_docs_app, codes={404:'/error'})
47    app = TestApp(RecursiveMiddleware(app))
48    res = app.get('')
49    assert res.header('content-type') == 'text/plain'
50    assert res.full_status == '200 OK'
51    assert 'requested page returned' in res
52    res = app.get('/error')
53    assert res.header('content-type') == 'text/plain'
54    assert res.full_status == '200 OK'
55    assert 'Page not found' in res
56    res = app.get('/not_found', status=404)
57    assert res.header('content-type') == 'text/plain'
58    assert res.full_status == '404 Not found'
59    # Note changed response
60    assert 'Page not found' in res
61
62def auth_required_app(environ, start_response):
63    start_response('401 Unauthorized', [('content-type', 'text/plain'), ('www-authenticate', 'Basic realm="Foo"')])
64    return ['Sign in!']
65
66def auth_docs_app(environ, start_response):
67    if environ['PATH_INFO'] == '/auth':
68        return auth_required_app(environ, start_response)
69    elif environ['PATH_INFO'] == '/auth_doc':
70        start_response("200 OK", [('Content-type', 'text/html')])
71        return [b'<html>Login!</html>']
72    else:
73        return simple_app(environ, start_response)
74
75def test_auth_docs_app():
76    wsgi_app = forward(auth_docs_app, codes={401: '/auth_doc'})
77    app = TestApp(wsgi_app)
78    res = app.get('/auth_doc')
79    assert res.header('content-type') == 'text/html'
80    res = app.get('/auth', status=401)
81    assert res.header('content-type') == 'text/html'
82    assert res.header('www-authenticate') == 'Basic realm="Foo"'
83    assert res.body == b'<html>Login!</html>'
84
85def test_bad_error():
86    def app(environ, start_response):
87        start_response('404 Not Found', [('content-type', 'text/plain')])
88        return ['not found']
89    app = forward(app, {404: '/404.html'})
90    app = TestApp(app)
91    resp = app.get('/test', expect_errors=True)
92    print(resp)
93