Files
turbosloth/tests/test_router.py

53 lines
1.3 KiB
Python

import pytest
from src.turbosloth.router import Router
def test_router_root_handler():
async def f(_: dict, *args):
pass
async def d(_: dict, *args):
pass
async def a(_: dict, *args):
pass
r = Router()
r.add('GET', '/', f)
r.add('GET', '', f)
assert len(r._root.static_subroutes) == 0
assert len(r._root.handler) == 1
assert r._root.handler['GET'] == f
r.add('POST', '/', a)
assert len(r._root.static_subroutes) == 0
assert len(r._root.handler) == 2
assert r._root.handler['GET'] == f
assert r._root.handler['POST'] == a
r.add('GET', '/asdf', d)
assert len(r._root.static_subroutes) == 1
assert len(r._root.handler) == 2
assert r._root.handler['GET'] == f
assert r._root.handler['POST'] == a
assert len(r._root.static_subroutes['asdf'].static_subroutes) == 0
assert len(r._root.static_subroutes['asdf'].handler) == 1
assert r._root.static_subroutes['asdf'].handler['GET'] == d
def test_router_match():
async def f(_: dict, *args):
pass
async def d(_: dict, *args):
pass
r = Router()
r.add('GET', 'asdf', f)
assert r.match('GET', '/asdf')
with pytest.raises(ValueError, match='404'):
r.match('GET', 'asd')
with pytest.raises(ValueError, match='405'):
r.match('POST', 'asdf')