| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 | from http import HTTPStatusfrom uuid import UUIDfrom uuid import uuid4import pytestfrom fastapi.testclient import TestClientfrom clean_python import ctxfrom clean_python import InMemoryGatewayfrom clean_python.fastapi import getfrom clean_python.fastapi import Resourcefrom clean_python.fastapi import Servicefrom clean_python.fastapi import vclass FooResource(Resource, version=v(1), name="testing"):    @get("/context")    def context(self):        return {            "path": str(ctx.path),            "user": ctx.user,            "tenant": ctx.tenant,            "correlation_id": str(ctx.correlation_id),        }@pytest.fixturedef app():    return Service(FooResource()).create_app(        title="test",        description="testing",        hostname="testserver",        access_logger_gateway=InMemoryGateway([]),    )@pytest.fixturedef client(app):    return TestClient(app)def test_default_context(app, client: TestClient):    response = client.get(app.url_path_for("v1/context"))    assert response.status_code == HTTPStatus.OK    body = response.json()    assert body["path"] == "http://testserver/v1/context"    assert body["user"] == {"id": "DEV", "name": "dev"}    assert body["tenant"] is None    UUID(body["correlation_id"])  # randomly generated uuid    assert ctx.correlation_id is Nonedef test_x_correlation_id_header(app, client: TestClient):    uid = str(uuid4())    response = client.get(        app.url_path_for("v1/context"),        headers={"X-Correlation-Id": uid},    )    assert response.status_code == HTTPStatus.OK    body = response.json()    assert body["correlation_id"] == uid    assert ctx.correlation_id is None
 |