forked from Neoteroi/BlackSheep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_router.py
856 lines (631 loc) · 20 KB
/
test_router.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
import pytest
from blacksheep.server.application import Application
from blacksheep.server.routing import (
InvalidValuePatternName,
MountRegistry,
Route,
RouteDuplicate,
RouteException,
RouteMethod,
Router,
)
FAKE = b"FAKE"
MATCHING_ROUTES = [
("head", b"", b"/"),
("get", b"", b"/"),
("head", b"/", b"/"),
("get", b"/", b"/"),
("get", b"/:a", b"/foo"),
("get", b"/foo", b"/foo"),
("get", b"/foo", b"/Foo"),
("get", b"/:a/:b", b"/foo/oof"),
("post", b"/", b"/"),
("post", b"/:id", b"/123"),
("put", b"/", b"/"),
("delete", b"/", b"/"),
]
NON_MATCHING_ROUTE = [
("head", b"/", b"/foo"),
("get", b"/", b"/foo"),
("post", b"/", b"/foo"),
("post", b"/foo", b"/123"),
("put", b"/a/b/c/d", b"/a/b/c/"),
("put", b"/a/b/c/d", b"/a/b/c/d/e"),
("delete", b"/", b"/a"),
]
def mock_handler():
return None
class MockHandler:
def __init__(self, request_handler, auth_handler):
self.request_handler = request_handler
self.auth_handler = auth_handler
@pytest.mark.parametrize(
"pattern,url,expected_values",
[
(b"/foo/:id", b"/foo/123", {"id": "123"}),
(b"/foo/{id}", b"/foo/123", {"id": "123"}),
(b"/foo/<id>", b"/foo/123", {"id": "123"}),
("/foo/:id", b"/foo/123", {"id": "123"}),
("/foo/{id}", b"/foo/123", {"id": "123"}),
("/foo/<id>", b"/foo/123", {"id": "123"}),
(b"/foo/:id/ufo/:b", b"/foo/223/ufo/a13", {"id": "223", "b": "a13"}),
(b"/foo/{id}/ufo/{b}", b"/foo/223/ufo/a13", {"id": "223", "b": "a13"}),
(b"/foo/<id>/ufo/<b>", b"/foo/223/ufo/a13", {"id": "223", "b": "a13"}),
("/foo/:id/ufo/:b", b"/foo/223/ufo/a13", {"id": "223", "b": "a13"}),
("/foo/{id}/ufo/{b}", b"/foo/223/ufo/a13", {"id": "223", "b": "a13"}),
(b"/foo/:id/ufo/:b", b"/Foo/223/Ufo/a13", {"id": "223", "b": "a13"}),
(b"/:a", b"/Something", {"a": "Something"}),
(b"/{a}", b"/Something", {"a": "Something"}),
(b"/<a>", b"/Something", {"a": "Something"}),
(b"/alive", b"/alive", None),
],
)
def test_route_good_matches(pattern, url, expected_values):
route = Route(pattern, mock_handler)
print(route.full_pattern)
match = route.match(url)
assert match is not None
assert match.values == expected_values
@pytest.mark.parametrize(
"pattern,url,expected_values",
[
("/foo/{string:foo}", b"/foo/Hello", {"foo": "Hello"}),
("/foo/{string:foo}/ufo", b"/foo/Hello/ufo", {"foo": "Hello"}),
("/foo/{str:foo}", b"/foo/Hello", {"foo": "Hello"}),
("/foo/{str:foo}/ufo", b"/foo/Hello/ufo", {"foo": "Hello"}),
("/foo/{int:id}", b"/foo/123", {"id": "123"}),
("/foo/<int:id>", b"/foo/123", {"id": "123"}),
("/foo/{float:a}", b"/foo/123", {"a": "123"}),
("/foo/{float:a}", b"/foo/123.15", {"a": "123.15"}),
("/foo/<int:a>/<float:b>", b"/foo/123/777.77", {"a": "123", "b": "777.77"}),
(
"/foo/{uuid:id}",
b"/foo/52464abf-f583-4b32-80f8-704bcb9e36a2",
{"id": "52464abf-f583-4b32-80f8-704bcb9e36a2"},
),
(
"/public/{path:file}",
b"/public/",
{"file": ""},
),
(
"/public/{path:file}",
b"/public/js/home/home.js",
{"file": "js/home/home.js"},
),
(
"/public/{path:file}",
b"/public/home.js",
{"file": "home.js"},
),
(
"/public/<path:file>",
b"/public/js/home/home.js",
{"file": "js/home/home.js"},
),
(
"/<path:filepath>",
b"/public/js/home/home.js",
{"filepath": "public/js/home/home.js"},
),
],
)
def test_route_good_matches_with_parameter_patterns(pattern, url, expected_values):
route = Route(pattern, mock_handler)
match = route.match(url)
assert match is not None
assert match.values == expected_values
@pytest.mark.parametrize(
"pattern,url",
[
(b"/foo/{int:id}", b"/foo/abc"),
(b"/foo/<int:id>", b"/foo/X"),
(b"/foo/{float:a}", b"/foo/false"),
(b"/foo/{float:a}", b"/foo/123.15.31"),
(b"/foo/{float:a}", b"/foo/123,50"),
(b"/foo/{uuid:a}", b"/foo/not-a-guid"),
],
)
def test_route_bad_matches_with_parameter_patterns(pattern, url):
route = Route(pattern, mock_handler)
match = route.match(url)
assert match is None
@pytest.mark.parametrize(
"pattern",
(
"/public/<path:file>",
"/<path:file>",
"/public/*",
"/*",
"*",
),
)
def test_sort_routes_path_pattern(pattern):
router = Router()
catch_all_route = Route(pattern, mock_handler)
router.add_route("GET", catch_all_route)
router.add_route("GET", Route("/cat/:cat_id", mock_handler))
router.add_route("GET", Route("/index", mock_handler))
router.add_route("GET", Route("/about", mock_handler))
assert router.routes[b"GET"][0] is catch_all_route
router.sort_routes()
assert router.routes[b"GET"][-1] is catch_all_route
@pytest.mark.parametrize(
"pattern,invalid_pattern_name",
[
(b"/foo/{xxx:id}", "xxx"),
(b"/foo/{ind:id}", "ind"),
],
)
def test_route_raises_for_invalid_parameter_name(pattern, invalid_pattern_name):
with pytest.raises(InvalidValuePatternName):
Route(pattern, mock_handler)
@pytest.mark.parametrize(
"pattern",
["/a", "/a/b", "/a/b/c", "/cats/:cat_id", "/cats/:cat_id/friends"],
)
def test_route_repr(pattern: str):
route = Route(pattern, mock_handler)
assert repr(route) == f"<Route {pattern}>"
@pytest.mark.parametrize(
"pattern,url",
[
(b"/foo/:id", b"/fo/123"),
(b"/foo/:id/ufo/:b", b"/foo/223/uof/a13"),
(b"/:a", b"/"),
],
)
def test_route_bad_matches(pattern, url):
route = Route(pattern, mock_handler)
match = route.match(url)
assert match is None
@pytest.mark.parametrize("pattern", [b"/:a/:a", b"/foo/:a/ufo/:a", b"/:foo/a/:foo"])
def test_invalid_route_repeated_group_name(pattern):
with pytest.raises(ValueError):
Route(pattern, mock_handler)
def test_route_handler_can_be_anything():
def request_handler():
pass
def auth_handler():
pass
handler = MockHandler(request_handler, auth_handler)
route = Route(b"/", handler)
match = route.match(b"/")
assert match is not None
assert match.handler.request_handler is request_handler
assert match.handler.auth_handler is auth_handler
@pytest.mark.parametrize("method,pattern,url", MATCHING_ROUTES)
def test_router_add_method(method, pattern, url):
router = Router()
router.add(method, pattern, mock_handler)
match = router.get_match(method, url)
assert match is not None
assert match.handler is mock_handler
route = router.get_matching_route(method, url)
assert route is not None
match = router.get_match(FAKE, url)
assert match is None
route = router.get_matching_route(FAKE, url)
assert route is None
@pytest.mark.parametrize("method,pattern,url", NON_MATCHING_ROUTE)
def test_router_not_matching_routes(method, pattern, url):
router = Router()
router.add(method, pattern, mock_handler)
route = router.get_match(method, url)
assert route is None
@pytest.mark.parametrize("method,pattern,url", MATCHING_ROUTES)
def test_router_add_shortcuts(method, pattern, url):
router = Router()
fn = getattr(router, f"add_{method}")
def home():
return "Hello, World"
fn(pattern, home)
route = router.get_match(method.upper(), url)
assert route is not None
assert route.handler is home
value = route.handler()
assert value == "Hello, World"
route = router.get_match(FAKE, url)
assert route is None
@pytest.mark.parametrize("decorator,pattern,url", MATCHING_ROUTES)
def test_router_decorator(decorator, pattern, url):
router = Router()
method = getattr(router, decorator)
@method(pattern)
def home():
return "Hello, World"
route = router.get_match(decorator.upper(), url)
assert route is not None
assert route.handler is home
value = route.handler()
assert value == "Hello, World"
route = router.get_match(FAKE, url)
assert route is None
def test_router_match_any_by_extension():
router = Router()
def a():
...
def b():
...
router.add_get("/a/*.js", a)
router.add_get("/b/*.css", b)
m = router.get_match(RouteMethod.GET, b"/a/anything/really")
assert m is None
m = router.get_match(RouteMethod.GET, b"/a/anything/really.js")
assert m is not None
assert m.handler is a
assert m.values.get("tail") == "anything/really"
m = router.get_match(RouteMethod.GET, b"/b/anything/really.css")
assert m is not None
assert m.handler is b
assert m.values.get("tail") == "anything/really"
def test_router_match_any_below():
router = Router()
def a():
...
def b():
...
def c():
...
def d():
...
router.add_get("/a/*", a)
router.add_get("/b/*", b)
router.add_get("/c/*", c)
router.add_get("/d/*", d)
m = router.get_match(RouteMethod.GET, b"/a")
assert m is not None
assert m.handler is a
assert m.values.get("tail") == ""
m = router.get_match(RouteMethod.GET, b"/a/")
assert m is not None
assert m.handler is a
assert m.values.get("tail") == ""
m = router.get_match(RouteMethod.GET, b"/a/anything/really")
assert m is not None
assert m.handler is a
assert m.values.get("tail") == "anything/really"
m = router.get_match(RouteMethod.GET, b"/b/anything/really")
assert m is not None
assert m.handler is b
assert m.values.get("tail") == "anything/really"
m = router.get_match(RouteMethod.GET, b"/c/anything/really")
assert m is not None
assert m.handler is c
assert m.values.get("tail") == "anything/really"
m = router.get_match(RouteMethod.GET, b"/d/anything/really")
assert m is not None
assert m.handler is d
assert m.values.get("tail") == "anything/really"
m = router.get_match(RouteMethod.POST, b"/a/anything/really")
assert m is None
m = router.get_match(RouteMethod.POST, b"/b/anything/really")
assert m is None
m = router.get_match(RouteMethod.POST, b"/c/anything/really")
assert m is None
m = router.get_match(RouteMethod.POST, b"/d/anything/really")
assert m is None
def test_router_match_among_many():
router = Router()
def home():
...
def home_verbose():
...
def home_options():
...
def home_connect():
...
def get_foo():
...
def create_foo():
...
def patch_foo():
...
def delete_foo():
...
def ws():
...
router.add_trace("/", home_verbose)
router.add_options("/", home_options)
router.add_connect("/", home_connect)
router.add_get("/", home)
router.add_get("/foo", get_foo)
router.add_patch("/foo", patch_foo)
router.add_post("/foo", create_foo)
router.add_delete("/foo", delete_foo)
router.add_ws("/ws", ws)
m = router.get_match(RouteMethod.GET, b"/")
assert m is not None
assert m.handler is home
m = router.get_match(RouteMethod.TRACE, b"/")
assert m is not None
assert m.handler is home_verbose
m = router.get_match(RouteMethod.CONNECT, b"/")
assert m is not None
assert m.handler is home_connect
m = router.get_match(RouteMethod.OPTIONS, b"/")
assert m is not None
assert m.handler is home_options
m = router.get_match(RouteMethod.POST, b"/")
assert m is None
m = router.get_match(RouteMethod.GET, b"/foo")
assert m is not None
assert m.handler is get_foo
m = router.get_match(RouteMethod.POST, b"/foo")
assert m is not None
assert m.handler is create_foo
m = router.get_match(RouteMethod.PATCH, b"/foo")
assert m is not None
assert m.handler is patch_foo
m = router.get_match(RouteMethod.DELETE, b"/foo")
assert m is not None
assert m.handler is delete_foo
m = router.get_match(RouteMethod.GET_WS, b"/ws")
assert m is not None
assert m.handler is ws
def test_router_match_ws_get_sharing_path():
router = Router()
def home():
...
def ws():
...
router.add_get("/", home)
router.add_ws("/", ws)
m = router.get_match(RouteMethod.GET, b"/")
assert m is not None
assert m.handler is home
m = router.get_match(RouteMethod.GET_WS, b"/")
assert m is not None
assert m.handler is ws
def test_router_match_among_many_decorators():
router = Router()
@router.get("/")
def home():
...
@router.trace("/")
def home_verbose():
...
@router.options("/")
def home_options():
...
@router.connect("/")
def home_connect():
...
@router.get("/foo")
def get_foo():
...
@router.post("/foo")
def create_foo():
...
@router.patch("/foo")
def patch_foo():
...
@router.delete("/foo")
def delete_foo():
...
m = router.get_match(RouteMethod.GET, b"/")
assert m is not None
assert m.handler is home
m = router.get_match(RouteMethod.TRACE, b"/")
assert m is not None
assert m.handler is home_verbose
m = router.get_match(RouteMethod.CONNECT, b"/")
assert m is not None
assert m.handler is home_connect
m = router.get_match(RouteMethod.OPTIONS, b"/")
assert m is not None
assert m.handler is home_options
m = router.get_match(RouteMethod.POST, b"/")
assert m is None
m = router.get_match(RouteMethod.GET, b"/foo")
assert m is not None
assert m.handler is get_foo
m = router.get_match(RouteMethod.POST, b"/foo")
assert m is not None
assert m.handler is create_foo
m = router.get_match(RouteMethod.PATCH, b"/foo")
assert m is not None
assert m.handler is patch_foo
m = router.get_match(RouteMethod.DELETE, b"/foo")
assert m is not None
assert m.handler is delete_foo
def test_router_match_with_trailing_slash():
router = Router()
def get_foo():
...
def create_foo():
...
router.add_get("/foo", get_foo)
router.add_post("/foo", create_foo)
m = router.get_match(RouteMethod.GET, b"/foo/")
assert m is not None
assert m.handler is get_foo
m = router.get_match(RouteMethod.POST, b"/foo/")
assert m is not None
assert m.handler is create_foo
m = router.get_match(RouteMethod.POST, b"/foo//")
assert m is None
def test_fallback_route():
router = Router()
def not_found_handler():
pass
router.fallback = not_found_handler
assert isinstance(router.fallback, Route)
assert router.fallback.handler is not_found_handler
m = router.get_match(RouteMethod.POST, b"/")
assert m is not None
assert m.handler is not_found_handler
def test_fallback_route_must_be_callable_or_route():
router = Router()
def not_found_handler():
pass
router.fallback = Route("*", not_found_handler)
router.fallback = not_found_handler
class Example:
def __call__(self):
pass
router.fallback = Example()
with pytest.raises(ValueError):
router.fallback = False
with pytest.raises(ValueError):
router.fallback = "Something"
@pytest.mark.parametrize(
"first_route,second_route",
[
("/", "/"),
(b"/", b"/"),
(b"/", "/"),
("/", b"/"),
("/home/", "/home"),
(b"/home/", b"/home"),
("/home", "/home/"),
(b"/home", b"/home/"),
("/home", "/home//"),
(b"/home", b"/home//"),
("/hello/world", "/hello/world/"),
(b"/hello/world", b"/hello/world//"),
("/a/b", "/a/b"),
],
)
def test_duplicate_pattern_raises(first_route, second_route):
router = Router()
def home():
...
def another():
...
router.add_get(first_route, home)
with pytest.raises(RouteDuplicate):
router.add_get(second_route, another)
def test_duplicate_pattern_star_raises():
router = Router()
def home():
...
def another():
...
router.add_get("*", home)
with pytest.raises(RouteDuplicate):
router.add_get("*", another)
def test_more_than_one_star_raises():
router = Router()
def home():
...
with pytest.raises(RouteException):
router.add_get("*/*", home)
def test_automatic_pattern_with_ellipsis():
router = Router()
@router.get(...)
def home():
...
@router.get(...)
def another():
...
match = router.get_match("GET", "/")
assert match is None
match = router.get_match("GET", "/home")
assert match is not None
assert match.handler is home
match = router.get_match("GET", "/another")
assert match is not None
assert match.handler is another
def test_automatic_pattern_with_ellipsis_name_normalization():
router = Router()
@router.get(...)
def hello_world():
...
match = router.get_match("GET", "/hello_world")
assert match is None
match = router.get_match("GET", "/hello-world")
assert match is not None
assert match.handler is hello_world
def test_automatic_pattern_with_ellipsis_index_name():
router = Router()
@router.get(...)
def index():
...
match = router.get_match("GET", "/")
assert match is not None
assert match.handler is index
def test_router_iterable():
router = Router()
@router.get("/")
def home():
...
@router.trace("/")
def home_verbose():
...
@router.options("/")
def home_options():
...
routes = list(router)
assert len(routes) == 3
handlers = {home, home_verbose, home_options}
for route in routes:
assert route.handler in handlers
def fallback():
...
router.fallback = fallback
routes = list(router)
assert len(routes) == 4
handlers = {home, home_verbose, home_options, fallback}
for route in routes:
assert route.handler in handlers
@pytest.mark.parametrize(
"route_pattern,expected_pattern",
[
["/", "/"],
["/api/v1/help", "/api/v1/help"],
["/api/cats/:cat_id", "/api/cats/{cat_id}"],
["/api/cats/:cat_id/friends", "/api/cats/{cat_id}/friends"],
[
"/api/cats/:cat_id/friends/:friend_id",
"/api/cats/{cat_id}/friends/{friend_id}",
],
],
)
def test_route_to_openapi_pattern(route_pattern, expected_pattern):
route = Route(route_pattern, object())
assert route.mustache_pattern == expected_pattern
@pytest.mark.parametrize(
"mount_path,expected_scope_path",
[
("", "/*"),
("/", "/*"),
("/*", "/*"),
("/foo", "/foo/*"),
("/foo*", "/foo/*"),
("/foo/", "/foo/*"),
("/foo/*", "/foo/*"),
("/admin", "/admin/*"),
("/admin*", "/admin/*"),
("/a/b/c", "/a/b/c/*"),
("/a/b/c/", "/a/b/c/*"),
("/a/b/c*", "/a/b/c/*"),
("/a/b/c/*", "/a/b/c/*"),
],
)
def test_mount_add_method(mount_path, expected_scope_path):
class ASGIHandler:
def __call__(self, *args, **kwargs):
pass
app = ASGIHandler()
mount = MountRegistry()
mount.mount(mount_path, app)
assert any(
mount_route.pattern.decode() == expected_scope_path
for mount_route in mount.mounted_apps
)
assert any(mount_route.handler is app for mount_route in mount.mounted_apps)
def test_mount_mounted_paths():
mount = MountRegistry()
assert mount.mounted_paths == set()
mount.mount("/foo", Application())
assert mount.mounted_paths == {"/foo"}
mount.mount("/oFo", Application())
assert mount.mounted_paths == {"/foo", "/ofo"}
mount.mount("/ooF", Application())
assert mount.mounted_paths == {"/foo", "/ofo", "/oof"}
def test_mount_add_raise_error_if_path_exist():
with pytest.raises(AssertionError):
mount = MountRegistry()
mount.mount("/foo", None)
mount.mount("/foo", None)