forked from morepath/morepath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_request.py
More file actions
76 lines (61 loc) · 2.18 KB
/
test_request.py
File metadata and controls
76 lines (61 loc) · 2.18 KB
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
from morepath import App, render_json
from webtest import TestApp as Client
def test_request_reset():
# In order to verify the behaviour of Request.reset we issue the
# same request three times:
#
# 1. The server responds succesfully (generate_error = False)
#
# 2. The server responds with an error (generate_error = True) and
# returns the request as it was when the exception was thrown
# (reset_request = False).
#
# 3. The server responds with an error (generate_error = True) and
# returns the request as it was when it was received
# (reset_request = True).
generate_error = False
reset_request = False
class RootApp(App):
pass
@RootApp.tween_factory()
def report_error(app, handler):
def tween(request):
try:
response = handler(request)
response.headers['Tween-Header'] = 'FOO'
return response
except RuntimeError:
if reset_request:
request.reset()
response = render_json(
{'app': repr(type(request.app)),
'unconsumed': request.unconsumed},
request)
response.status_code = 500
return response
return tween
class MountedApp(App):
pass
@MountedApp.path(path='catalog')
class Catalog(object):
pass
@MountedApp.view(model=Catalog, name='text')
def view_catalog(self, request):
if generate_error:
raise RuntimeError("Error!")
return "The catalog"
RootApp.mount(app=MountedApp, path='mount')(MountedApp)
c = Client(RootApp())
response = c.get('/mount/catalog/text')
assert response.text == 'The catalog'
assert response.headers['Tween-Header'] == 'FOO'
generate_error = True
response = c.get('/mount/catalog/text', status=500)
assert response.json == {
'app': repr(MountedApp),
'unconsumed': ['text']}
reset_request = True
response = c.get('/mount/catalog/text', status=500)
assert response.json == {
'app': repr(RootApp),
'unconsumed': ['text', 'catalog', 'mount']}