PLBA2-4. Доработка регистрации http эндпоинтов
This commit is contained in:
+45
-12
@@ -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"}))
|
||||
|
||||
Reference in New Issue
Block a user