PLBA2-4. Доработка регистрации http эндпоинтов
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import unittest
|
||||
|
||||
from plba.http.routes import (
|
||||
HttpRoute,
|
||||
HttpRouter,
|
||||
MethodNotAllowed,
|
||||
RouteNotFound,
|
||||
)
|
||||
|
||||
|
||||
async def unused_handler(scope, receive, send) -> None:
|
||||
return None
|
||||
|
||||
|
||||
class HttpRouteDefinitionTests(unittest.TestCase):
|
||||
def test_requires_versioned_path_with_endpoint_segment(self) -> None:
|
||||
for path in (
|
||||
"/health",
|
||||
"/api/v0/health",
|
||||
"/api/v1",
|
||||
"/api/v1//health",
|
||||
"/api/v1/health/",
|
||||
"/api/v1/{bad-name}",
|
||||
"/api/v1/{item_id}/{item_id}",
|
||||
):
|
||||
with self.subTest(path=path), self.assertRaises(ValueError):
|
||||
HttpRoute("GET", path, unused_handler)
|
||||
|
||||
def test_requires_uppercase_http_method_and_callable_handler(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
HttpRoute("get", "/api/v1/items", unused_handler)
|
||||
with self.assertRaises(ValueError):
|
||||
HttpRoute("GET", "/api/v1/items", None) # type: ignore[arg-type]
|
||||
|
||||
|
||||
class HttpRouterTests(unittest.TestCase):
|
||||
def test_resolves_named_parameters(self) -> None:
|
||||
route = HttpRoute("GET", "/api/v1/items/{item_id}", unused_handler)
|
||||
|
||||
match = HttpRouter((route,)).resolve("GET", "/api/v1/items/sku-42")
|
||||
|
||||
self.assertIs(match.route, route)
|
||||
self.assertEqual(match.path_params, {"item_id": "sku-42"})
|
||||
|
||||
def test_static_path_has_priority_over_parameter_path(self) -> None:
|
||||
parameter_route = HttpRoute(
|
||||
"GET", "/api/v1/items/{item_id}", unused_handler
|
||||
)
|
||||
static_route = HttpRoute("GET", "/api/v1/items/by-id", unused_handler)
|
||||
router = HttpRouter((parameter_route, static_route))
|
||||
|
||||
match = router.resolve("GET", "/api/v1/items/by-id")
|
||||
|
||||
self.assertIs(match.route, static_route)
|
||||
self.assertEqual(match.path_params, {})
|
||||
|
||||
def test_rejects_duplicate_and_ambiguous_routes(self) -> None:
|
||||
routes = (
|
||||
("/api/v1/items/{item_id}", "/api/v1/items/{item_id}"),
|
||||
("/api/v1/{kind}/items", "/api/v1/items/{name}"),
|
||||
)
|
||||
for first_path, second_path in routes:
|
||||
with self.subTest(paths=(first_path, second_path)):
|
||||
first = HttpRoute("GET", first_path, unused_handler)
|
||||
second = HttpRoute("GET", second_path, unused_handler)
|
||||
with self.assertRaises(ValueError):
|
||||
HttpRouter((first, second))
|
||||
|
||||
def test_known_path_with_wrong_method_reports_allowed_methods(self) -> None:
|
||||
router = HttpRouter(
|
||||
(HttpRoute("GET", "/api/v1/items", unused_handler),)
|
||||
)
|
||||
|
||||
with self.assertRaises(MethodNotAllowed) as caught:
|
||||
router.resolve("POST", "/api/v1/items")
|
||||
|
||||
self.assertEqual(caught.exception.allowed_methods, ("GET",))
|
||||
|
||||
def test_unknown_path_is_not_found(self) -> None:
|
||||
router = HttpRouter(
|
||||
(HttpRoute("GET", "/api/v1/items", unused_handler),)
|
||||
)
|
||||
|
||||
with self.assertRaises(RouteNotFound):
|
||||
router.resolve("GET", "/api/v1/unknown")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user