Add router add_subroute method

This commit is contained in:
2025-07-19 05:02:14 +03:00
parent af46710017
commit f4201b405f
2 changed files with 103 additions and 8 deletions

View File

@@ -79,3 +79,32 @@ def test_router_pattern_match():
with pytest.raises(NotFoundException, match='404\tNot Found: asd'):
r.match('GET', 'asd/basdf')
def test_subroutes():
async def f(*args):
pass
async def d(*args):
pass
r1 = Router()
r2 = Router()
r1.add('GET', '/asdf', f)
r2.add('GET', '/asdf/a', d)
r1.add_subroute('', r2)
assert r1.match('GET', '/asdf') == ({}, f)
assert r1.match('GET', '/asdf/a') == ({}, d)
r1.add_subroute('/asdf', r2)
assert r1.match('GET', '/asdf/asdf/a') == ({}, d)
r1.add_subroute('/asdf' * 5, r2)
assert r1.match('GET', '/asdf' * 5 + '/asdf/a') == ({}, d)
with pytest.raises(NotFoundException):
r1.match('GET', '/asdf/' * 5 + '/asdf/a')
r1.add_subroute('/asdf/' * 5, r2)
assert r1.match('GET', '/asdf/' * 5 + '/asdf/a') == ({}, d)