Add an app add subroute fn
This commit is contained in:
@@ -12,7 +12,7 @@ from .interfaces.serialize_selector import SerializeSelector
|
|||||||
from .interfaces.serialized import SerializedResponse, SerializedRequest
|
from .interfaces.serialized import SerializedResponse, SerializedRequest
|
||||||
from .interfaces.serialized.text import TextSerializedResponse
|
from .interfaces.serialized.text import TextSerializedResponse
|
||||||
from .req_schema import UnwrappedRequest
|
from .req_schema import UnwrappedRequest
|
||||||
from .router import Router
|
from .router import Router, Route
|
||||||
from .types import HandlerType, InternalHandlerType, ContentType
|
from .types import HandlerType, InternalHandlerType, ContentType
|
||||||
from .internal_types import Scope, Receive, Send, MethodType, QTYPE, BTYPE, PTYPE
|
from .internal_types import Scope, Receive, Send, MethodType, QTYPE, BTYPE, PTYPE
|
||||||
from breakshaft.convertor import ConvRepo
|
from breakshaft.convertor import ConvRepo
|
||||||
@@ -26,6 +26,9 @@ class ASGIApp(Protocol):
|
|||||||
def route(self, method: MethodType, path_pattern: str):
|
def route(self, method: MethodType, path_pattern: str):
|
||||||
raise RuntimeError('stub!')
|
raise RuntimeError('stub!')
|
||||||
|
|
||||||
|
def add_subroute(self, subr: Route | Router, basepath: str) -> None:
|
||||||
|
self.router.add_subroute(subr, basepath)
|
||||||
|
|
||||||
|
|
||||||
class HTTPApp(ASGIApp):
|
class HTTPApp(ASGIApp):
|
||||||
serialize_selector: SerializeSelector
|
serialize_selector: SerializeSelector
|
||||||
|
|||||||
@@ -177,7 +177,7 @@ class Router:
|
|||||||
raise NotFoundException(path or '/')
|
raise NotFoundException(path or '/')
|
||||||
return h
|
return h
|
||||||
|
|
||||||
def add_subroute(self, basepath: str, subr: Route | Router):
|
def add_subroute(self, subr: Route | Router, basepath: str):
|
||||||
if isinstance(subr, Router):
|
if isinstance(subr, Router):
|
||||||
subr = subr._root
|
subr = subr._root
|
||||||
|
|
||||||
|
|||||||
@@ -93,18 +93,18 @@ def test_subroutes():
|
|||||||
|
|
||||||
r1.add('GET', '/asdf', f)
|
r1.add('GET', '/asdf', f)
|
||||||
r2.add('GET', '/asdf/a', d)
|
r2.add('GET', '/asdf/a', d)
|
||||||
r1.add_subroute('', r2)
|
r1.add_subroute(r2, '')
|
||||||
|
|
||||||
assert r1.match('GET', '/asdf') == ({}, f)
|
assert r1.match('GET', '/asdf') == ({}, f)
|
||||||
assert r1.match('GET', '/asdf/a') == ({}, d)
|
assert r1.match('GET', '/asdf/a') == ({}, d)
|
||||||
r1.add_subroute('/asdf', r2)
|
r1.add_subroute(r2, '/asdf')
|
||||||
assert r1.match('GET', '/asdf/asdf/a') == ({}, d)
|
assert r1.match('GET', '/asdf/asdf/a') == ({}, d)
|
||||||
|
|
||||||
r1.add_subroute('/asdf' * 5, r2)
|
r1.add_subroute(r2, '/asdf' * 5)
|
||||||
assert r1.match('GET', '/asdf' * 5 + '/asdf/a') == ({}, d)
|
assert r1.match('GET', '/asdf' * 5 + '/asdf/a') == ({}, d)
|
||||||
|
|
||||||
with pytest.raises(NotFoundException):
|
with pytest.raises(NotFoundException):
|
||||||
r1.match('GET', '/asdf/' * 5 + '/asdf/a')
|
r1.match('GET', '/asdf/' * 5 + '/asdf/a')
|
||||||
|
|
||||||
r1.add_subroute('/asdf/' * 5, r2)
|
r1.add_subroute(r2, '/asdf/' * 5)
|
||||||
assert r1.match('GET', '/asdf/' * 5 + '/asdf/a') == ({}, d)
|
assert r1.match('GET', '/asdf/' * 5 + '/asdf/a') == ({}, d)
|
||||||
|
|||||||
Reference in New Issue
Block a user