import pytest from src.turbosloth.router import Router from src.turbosloth.exceptions import NotFoundException, MethodNotAllowedException 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(NotFoundException, match='404\tNot Found: asd'): r.match('GET', 'asd') with pytest.raises(MethodNotAllowedException, match=f'405\tMethod Not Allowed: POST /asdf, allowed: GET'): r.match('POST', 'asdf') with pytest.raises(MethodNotAllowedException, match=f'405\tMethod Not Allowed: POST /, allowed: none'): r.match('POST', '')