PLBA2-4. Доработка регистрации http эндпоинтов
PLBA package CI / Python tests (push) Successful in 23s
PLBA package CI / Package version increment (push) Skipped

This commit is contained in:
2026-10-11 10:44:59 +03:00
parent a0d2db6bd0
commit d46993a092
30 changed files with 1666 additions and 583 deletions
+45 -12
View File
@@ -12,7 +12,7 @@ from plba.health import (
HealthContributorRegistry,
RuntimeHealthInputProvider,
)
from plba.http import ControlApi
from plba.http import ControlApi, HttpRoute
from plba.runtime import ApplicationLifecycle, ApplicationRuntime, RuntimeContext
from plba.runtime.worker import (
ComponentHealth,
@@ -126,33 +126,65 @@ class ControlApiTests(unittest.IsolatedAsyncioTestCase):
provider = RuntimeHealthInputProvider(
self.runtime, self.supervisor, HealthContributorRegistry()
)
self.api = ControlApi(self.runtime, HealthAggregator(provider))
self.health_aggregator = HealthAggregator(provider)
self.api = ControlApi(self.runtime, self.health_aggregator)
async def test_status_start_health_and_stop_routes(self) -> None:
status = await self.request("GET", "/runtime/status")
status = await self.request("GET", "/api/v1/runtime/status")
self.assertEqual(status[0], 200)
self.assertEqual(status[1], {"status": "new"})
started = await self.request("POST", "/runtime/start")
started = await self.request("POST", "/api/v1/runtime/start")
self.assertEqual(started[0], 200)
self.assertEqual(started[1], {"status": "running"})
health = await self.request("GET", "/health")
health = await self.request("GET", "/api/v1/health")
self.assertEqual(health[0], 200)
self.assertEqual(health[1]["application_id"], "service-http")
self.assertEqual(health[1]["status"], "ok")
stopped = await self.request("POST", "/runtime/stop", b"{}")
stopped = await self.request("POST", "/api/v1/runtime/stop", b"{}")
self.assertEqual(stopped, (200, {"status": "stopped"}))
async def test_application_route_uses_control_api_and_receives_path_params(self) -> None:
async def get_item(scope, receive, send) -> None:
body = json.dumps({"item_id": scope["path_params"]["item_id"]}).encode()
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [(b"content-type", b"application/json")],
}
)
await send({"type": "http.response.body", "body": body})
self.api = ControlApi(
self.runtime,
self.health_aggregator,
(HttpRoute("GET", "/api/v1/items/{item_id}", get_item),),
)
response = await self.request("GET", "/api/v1/items/sku-42")
self.assertEqual(response, (200, {"item_id": "sku-42"}))
async def test_router_errors_use_versioned_api_codes(self) -> None:
missing = await self.request("GET", "/api/v1/missing")
method_not_allowed = await self.request("POST", "/api/v1/health")
self.assertEqual(missing[0], 404)
self.assertEqual(missing[1]["error"], "HTTP_ROUTE_NOT_FOUND")
self.assertEqual(method_not_allowed[0], 405)
self.assertEqual(method_not_allowed[1]["error"], "HTTP_METHOD_NOT_ALLOWED")
async def test_invalid_stop_request_is_http_400(self) -> None:
missing = await self.request("POST", "/runtime/stop")
malformed = await self.request("POST", "/runtime/stop", b"not-json")
missing = await self.request("POST", "/api/v1/runtime/stop")
malformed = await self.request("POST", "/api/v1/runtime/stop", b"not-json")
invalid_timeout = await self.request(
"POST", "/runtime/stop", b'{"timeout_seconds": 0}'
"POST", "/api/v1/runtime/stop", b'{"timeout_seconds": 0}'
)
null_timeout = await self.request(
"POST", "/runtime/stop", b'{"timeout_seconds": null}'
"POST", "/api/v1/runtime/stop", b'{"timeout_seconds": null}'
)
self.assertEqual(missing[0], 400)
@@ -178,12 +210,13 @@ class ControlApiTests(unittest.IsolatedAsyncioTestCase):
provider = RuntimeHealthInputProvider(
self.runtime, self.supervisor, HealthContributorRegistry()
)
self.api = ControlApi(self.runtime, HealthAggregator(provider))
self.health_aggregator = HealthAggregator(provider)
self.api = ControlApi(self.runtime, self.health_aggregator)
await self.runtime.start()
await asyncio.wait_for(active.wait(), timeout=1)
result = await self.request(
"POST", "/runtime/stop", b'{"timeout_seconds": 0.01}'
"POST", "/api/v1/runtime/stop", b'{"timeout_seconds": 0.01}'
)
self.assertEqual(result, (202, {"status": "stopping"}))
+89
View File
@@ -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()