forked from django/channels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_routing.py
237 lines (208 loc) · 7.52 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
from unittest.mock import MagicMock
import pytest
from django.conf.urls import url
from django.core.exceptions import ImproperlyConfigured
from channels.http import AsgiHandler
from channels.routing import ChannelNameRouter, ProtocolTypeRouter, URLRouter
def test_protocol_type_router():
"""
Tests the ProtocolTypeRouter
"""
# Test basic operation
router = ProtocolTypeRouter(
{
"websocket": MagicMock(return_value="ws"),
"http": MagicMock(return_value="http"),
}
)
assert router({"type": "websocket"}) == "ws"
assert router({"type": "http"}) == "http"
# Test defaulting to AsgiHandler
router = ProtocolTypeRouter({"websocket": MagicMock(return_value="ws")})
assert isinstance(router({"type": "http"}), AsgiHandler)
# Test an unmatched type
with pytest.raises(ValueError):
router({"type": "aprs"})
# Test a scope with no type
with pytest.raises(KeyError):
router({"tyyyype": "http"})
def test_channel_name_router():
"""
Tests the ChannelNameRouter
"""
# Test basic operation
router = ChannelNameRouter(
{"test": MagicMock(return_value=1), "other_test": MagicMock(return_value=2)}
)
assert router({"channel": "test"}) == 1
assert router({"channel": "other_test"}) == 2
# Test an unmatched channel
with pytest.raises(ValueError):
router({"channel": "chat"})
# Test a scope with no channel
with pytest.raises(ValueError):
router({"type": "http"})
def test_url_router():
"""
Tests the URLRouter
"""
posarg_app = MagicMock(return_value=4)
kwarg_app = MagicMock(return_value=5)
defaultkwarg_app = MagicMock(return_value=6)
router = URLRouter(
[
url(r"^$", MagicMock(return_value=1)),
url(r"^foo/$", MagicMock(return_value=2)),
url(r"^bar", MagicMock(return_value=3)),
url(r"^posarg/(\d+)/$", posarg_app),
url(r"^kwarg/(?P<name>\w+)/$", kwarg_app),
url(r"^defaultkwargs/$", defaultkwarg_app, kwargs={"default": 42}),
]
)
# Valid basic matches
assert router({"type": "http", "path": "/"}) == 1
assert router({"type": "http", "path": "/foo/"}) == 2
assert router({"type": "http", "path": "/bar/"}) == 3
assert router({"type": "http", "path": "/bar/baz/"}) == 3
# Valid positional matches
assert router({"type": "http", "path": "/posarg/123/"}) == 4
assert posarg_app.call_args[0][0]["url_route"] == {"args": ("123",), "kwargs": {}}
assert router({"type": "http", "path": "/posarg/456/"}) == 4
assert posarg_app.call_args[0][0]["url_route"] == {"args": ("456",), "kwargs": {}}
# Valid keyword argument matches
assert router({"type": "http", "path": "/kwarg/hello/"}) == 5
assert kwarg_app.call_args[0][0]["url_route"] == {
"args": tuple(),
"kwargs": {"name": "hello"},
}
assert router({"type": "http", "path": "/kwarg/hellothere/"}) == 5
assert kwarg_app.call_args[0][0]["url_route"] == {
"args": tuple(),
"kwargs": {"name": "hellothere"},
}
# Valid default keyword arguments
assert router({"type": "http", "path": "/defaultkwargs/"}) == 6
assert defaultkwarg_app.call_args[0][0]["url_route"] == {
"args": tuple(),
"kwargs": {"default": 42},
}
# Invalid matches
with pytest.raises(ValueError):
router({"type": "http", "path": "/nonexistent/"})
def test_url_router_nesting():
"""
Tests that nested URLRouters add their keyword captures together.
"""
test_app = MagicMock(return_value=1)
inner_router = URLRouter(
[
url(r"^book/(?P<book>[\w\-]+)/page/(?P<page>\d+)/$", test_app),
url(r"^test/(\d+)/$", test_app),
]
)
outer_router = URLRouter(
[
url(r"^universe/(?P<universe>\d+)/author/(?P<author>\w+)/", inner_router),
url(r"^positional/(\w+)/", inner_router),
]
)
assert (
outer_router(
{
"type": "http",
"path": "/universe/42/author/andrewgodwin/book/channels-guide/page/10/",
}
)
== 1
)
assert test_app.call_args[0][0]["url_route"] == {
"args": (),
"kwargs": {
"book": "channels-guide",
"author": "andrewgodwin",
"page": "10",
"universe": "42",
},
}
assert outer_router({"type": "http", "path": "/positional/foo/test/3/"}) == 1
assert test_app.call_args[0][0]["url_route"] == {"args": ("foo", "3"), "kwargs": {}}
def test_url_router_nesting_path():
"""
Tests that nested URLRouters add their keyword captures together when used
with path().
"""
from django.urls import path
test_app = MagicMock(return_value=1)
inner_router = URLRouter([path("test/<int:page>/", test_app)])
def asgi_middleware(inner):
# Some middleware which hides the fact that we have an inner URLRouter
def app(scope):
return inner(scope)
app._path_routing = True
return app
outer_router = URLRouter(
[path("number/<int:number>/", asgi_middleware(inner_router))]
)
assert inner_router({"type": "http", "path": "/test/3/"}) == 1
assert outer_router({"type": "http", "path": "/number/42/test/3/"}) == 1
assert test_app.call_args[0][0]["url_route"] == {
"args": (),
"kwargs": {"number": 42, "page": 3},
}
with pytest.raises(ValueError):
assert outer_router({"type": "http", "path": "/number/42/test/3/bla/"})
with pytest.raises(ValueError):
assert outer_router({"type": "http", "path": "/number/42/blub/"})
def test_url_router_path():
"""
Tests that URLRouter also works with path()
"""
from django.urls import path
kwarg_app = MagicMock(return_value=3)
router = URLRouter(
[
path("", MagicMock(return_value=1)),
path("foo/", MagicMock(return_value=2)),
path("author/<name>/", kwarg_app),
path("year/<int:year>/", kwarg_app),
]
)
# Valid basic matches
assert router({"type": "http", "path": "/"}) == 1
assert router({"type": "http", "path": "/foo/"}) == 2
# Named without typecasting
assert router({"type": "http", "path": "/author/andrewgodwin/"}) == 3
assert kwarg_app.call_args[0][0]["url_route"] == {
"args": tuple(),
"kwargs": {"name": "andrewgodwin"},
}
# Named with typecasting
assert router({"type": "http", "path": "/year/2012/"}) == 3
assert kwarg_app.call_args[0][0]["url_route"] == {
"args": tuple(),
"kwargs": {"year": 2012},
}
# Invalid matches
with pytest.raises(ValueError):
router({"type": "http", "path": "/nonexistent/"})
# @pytest.mark.xfail
def test_path_remaining():
"""
Resolving continues in outer router if an inner router has no matching
routes
"""
inner_router = URLRouter([url(r"^no-match/$", MagicMock(return_value=1))])
test_app = MagicMock(return_value=2)
outer_router = URLRouter(
[url(r"^prefix/", inner_router), url(r"^prefix/stuff/$", test_app)]
)
outermost_router = URLRouter([url(r"", outer_router)])
assert outermost_router({"type": "http", "path": "/prefix/stuff/"}) == 2
def test_invalid_routes():
"""
Test URLRouter route validation
"""
from django.urls import include
with pytest.raises(ImproperlyConfigured) as exc:
URLRouter([url(r"^$", include([]))])
assert "include() is not supported in URLRouter." in str(exc)