/
test_routing.py
285 lines (219 loc) · 8.47 KB
/
test_routing.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# -*- coding: utf-8 -*-
"""
tests.routing
-------------
Test Emmett routing module
:copyright: (c) 2014-2019 by Giovanni Barillari
:license: BSD, see LICENSE for more details.
"""
import pendulum
import pytest
from contextlib import contextmanager
from helpers import FakeRequestContext
from emmett import App, abort, url
from emmett.ctx import current
from emmett.http import HTTP
from emmett.testing.env import ScopeBuilder
@contextmanager
def current_ctx(app, path):
builder = ScopeBuilder(path)
token = current._init_(FakeRequestContext, app, builder.get_data()[0])
yield current
current._close_(token)
@pytest.fixture(scope='module')
def app():
app = App(__name__)
app.languages = ['en', 'it']
app.language_default = 'en'
app.language_force_on_url = True
@app.route()
def test_route():
return 'Test Router'
@app.route()
def test_404():
abort(404, 'Not found, dude')
@app.route('/test2/<int:a>/<str:b>')
def test_route2(a, b):
return 'Test Router'
@app.route('/test3/<int:a>/foo(/<str:b>)?(.<str:c>)?')
def test_route3(a, b, c):
return 'Test Router'
@app.route('/test_int/<int:a>')
def test_route_int(a):
return 'Test Router'
@app.route('/test_float/<float:a>')
def test_route_float(a):
return 'Test Router'
@app.route('/test_date/<date:a>')
def test_route_date(a):
return 'Test Router'
@app.route('/test_alpha/<alpha:a>')
def test_route_alpha(a):
return 'Test Router'
@app.route('/test_str/<str:a>')
def test_route_str(a):
return 'Test Router'
@app.route('/test_any/<any:a>')
def test_route_any(a):
return 'Test Router'
@app.route(
'/test_complex'
'/<int:a>'
'/<float:b>'
'/<date:c>'
'/<alpha:d>'
'/<str:e>'
'/<any:f>'
)
def test_route_complex(a, b, c, d, e, f):
return 'Test Router'
return app
def test_routing(app):
with current_ctx(app, '/test_int') as ctx:
route, args = app._router_http.match(ctx.request)
assert not route
with current_ctx(app, '/test_int/a') as ctx:
route, args = app._router_http.match(ctx.request)
assert not route
with current_ctx(app, '/test_int/1.1') as ctx:
route, args = app._router_http.match(ctx.request)
assert not route
with current_ctx(app, '/test_int/2000-01-01') as ctx:
route, args = app._router_http.match(ctx.request)
assert not route
with current_ctx(app, '/test_int/1') as ctx:
route, args = app._router_http.match(ctx.request)
assert route.name == 'test_routing.test_route_int'
with current_ctx(app, '/test_float') as ctx:
route, args = app._router_http.match(ctx.request)
assert not route
with current_ctx(app, '/test_float/a.a') as ctx:
route, args = app._router_http.match(ctx.request)
assert not route
with current_ctx(app, '/test_float/1') as ctx:
route, args = app._router_http.match(ctx.request)
assert not route
with current_ctx(app, '/test_float/1.1') as ctx:
route, args = app._router_http.match(ctx.request)
assert route.name == 'test_routing.test_route_float'
with current_ctx(app, '/test_date') as ctx:
route, args = app._router_http.match(ctx.request)
assert not route
with current_ctx(app, '/test_date/2000-01-01') as ctx:
route, args = app._router_http.match(ctx.request)
assert route.name == 'test_routing.test_route_date'
with current_ctx(app, '/test_alpha') as ctx:
route, args = app._router_http.match(ctx.request)
assert not route
with current_ctx(app, '/test_alpha/a1') as ctx:
route, args = app._router_http.match(ctx.request)
assert not route
with current_ctx(app, '/test_alpha/a-a') as ctx:
route, args = app._router_http.match(ctx.request)
assert not route
with current_ctx(app, '/test_alpha/a') as ctx:
route, args = app._router_http.match(ctx.request)
assert route.name == 'test_routing.test_route_alpha'
with current_ctx(app, '/test_str') as ctx:
route, args = app._router_http.match(ctx.request)
assert not route
with current_ctx(app, '/test_str/a/b') as ctx:
route, args = app._router_http.match(ctx.request)
assert not route
with current_ctx(app, '/test_str/a1-') as ctx:
route, args = app._router_http.match(ctx.request)
assert route.name == 'test_routing.test_route_str'
with current_ctx(app, '/test_any') as ctx:
route, args = app._router_http.match(ctx.request)
assert not route
with current_ctx(app, '/test_any/a/b') as ctx:
route, args = app._router_http.match(ctx.request)
assert route.name == 'test_routing.test_route_any'
def test_route_args(app):
with current_ctx(
app, '/test_complex/1/1.2/2000-12-01/foo/foo1/bar/baz'
) as ctx:
route, args = app._router_http.match(ctx.request)
assert route.name == 'test_routing.test_route_complex'
assert args['a'] == 1
assert args['b'] == 1.2
assert args['c'] == pendulum.datetime(2000, 12, 1)
assert args['d'] == 'foo'
assert args['e'] == 'foo1'
assert args['f'] == 'bar/baz'
@pytest.mark.asyncio
async def test_routing_valid_route(app):
with current_ctx(app, '/it/test_route') as ctx:
response = await app._router_http.dispatch()
assert response.status_code == ctx.response.status == 200
assert response.body == 'Test Router'
assert ctx.request.language == 'it'
@pytest.mark.asyncio
async def test_routing_not_found_route(app):
with current_ctx(app, '/'):
with pytest.raises(HTTP) as excinfo:
await app._router_http.dispatch()
assert excinfo.value.status_code == 404
assert excinfo.value.body == 'Resource not found\n'
@pytest.mark.asyncio
async def test_routing_exception_route(app):
with current_ctx(app, '/test_404'):
with pytest.raises(HTTP) as excinfo:
await app._router_http.dispatch()
assert excinfo.value.status_code == 404
assert excinfo.value.body == 'Not found, dude'
def test_static_url(app):
link = url('static', 'file')
assert link == '/static/file'
app.config.static_version_urls = True
app.config.static_version = '1.0.0'
link = url('static', 'js/foo.js', language='it')
assert link == '/it/static/_1.0.0/js/foo.js'
def test_module_url(app):
with current_ctx(app, '/') as ctx:
ctx.request.language = 'it'
link = url('test_route')
assert link == '/it/test_route'
link = url('test_route2')
assert link == '/it/test2'
link = url('test_route2', [2])
assert link == '/it/test2/2'
link = url('test_route2', [2, 'foo'])
assert link == '/it/test2/2/foo'
link = url('test_route3')
assert link == '/it/test3'
link = url('test_route3', [2])
assert link == '/it/test3/2/foo'
link = url('test_route3', [2, 'bar'])
assert link == '/it/test3/2/foo/bar'
link = url('test_route3', [2, 'bar', 'json'])
assert link == '/it/test3/2/foo/bar.json'
link = url(
'test_route3', [2, 'bar', 'json'], {'foo': 'bar', 'bar': 'foo'})
lsplit = link.split('?')
assert lsplit[0] == '/it/test3/2/foo/bar.json'
assert lsplit[1] in ['foo=bar&bar=foo', 'bar=foo&foo=bar']
def test_global_url_prefix(app):
app._router_http._prefix_main = '/foo'
app._router_http._prefix_main_len = 3
with current_ctx(app, '/') as ctx:
app.config.static_version_urls = False
ctx.request.language = 'en'
link = url('test_route')
assert link == '/foo/test_route'
link = url('static', 'js/foo.js')
assert link == '/foo/static/js/foo.js'
app.config.static_version_urls = True
app.config.static_version = '1.0.0'
link = url('static', 'js/foo.js')
assert link == '/foo/static/_1.0.0/js/foo.js'
app.config.static_version_urls = False
ctx.request.language = 'it'
link = url('test_route')
assert link == '/foo/it/test_route'
link = url('static', 'js/foo.js')
assert link == '/foo/it/static/js/foo.js'
app.config.static_version_urls = True
app.config.static_version = '1.0.0'
link = url('static', 'js/foo.js')
assert link == '/foo/it/static/_1.0.0/js/foo.js'