Skip to content

Commit 6a36cab

Browse files
[CDAPI-118]: Include correlation-id for status endpoint
1 parent ec5708e commit 6a36cab

File tree

5 files changed

+24
-21
lines changed

5 files changed

+24
-21
lines changed

infrastructure/images/api-gateway-mock/resources/server.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@
3737
def forward_request(path_params):
3838
app.logger.info("received request with data: %s", request.get_data(as_text=True))
3939

40+
x_correlation_id = request.headers.get("X-Correlation-ID")
41+
forwarded_headers = {k.lower(): v for k, v in request.headers.items()}
42+
forwarded_headers["nhsd-correlation-id"] = x_correlation_id
43+
4044
response = requests.post(
4145
"http://pathology-api:8080/2015-03-31" # NOSONAR python:S5332
4246
"/functions/function/invocations",
4347
json={
4448
"body": request.get_data(as_text=True).replace("\n", "").replace(" ", ""),
45-
"headers": {k.lower(): v for k, v in request.headers.items()},
49+
"headers": forwarded_headers,
4650
"requestContext": {
4751
"http": {
4852
"path": f"/{path_params}",

pathology-api/lambda_handler.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,13 @@ def handler(data: dict[str, Any], context: LambdaContext) -> dict[str, Any]:
136136
headers = data.get("headers", {}) or {}
137137
correlation_id = headers.get(_CORRELATION_ID_HEADER)
138138
if not correlation_id:
139-
raw_path = (data.get("rawPath") or "").lstrip("/")
140-
if raw_path != "_status":
141-
return {
142-
"statusCode": 400,
143-
"headers": {"Content-Type": "application/fhir+json"},
144-
"body": OperationOutcome.create_validation_error(
145-
f"Missing required header: {_CORRELATION_ID_HEADER}"
146-
).model_dump_json(by_alias=True, exclude_none=True),
147-
}
148-
return app.resolve(data, context)
139+
return {
140+
"statusCode": 500,
141+
"headers": {"Content-Type": "application/fhir+json"},
142+
"body": OperationOutcome.create_server_error(
143+
f"Missing required header: {_CORRELATION_ID_HEADER}"
144+
).model_dump_json(by_alias=True, exclude_none=True),
145+
}
149146
with set_correlation_id(correlation_id):
150147
response = app.resolve(data, context)
151148
response.setdefault("headers", {})[_CORRELATION_ID_HEADER] = correlation_id

pathology-api/tests/conftest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ def client(request: pytest.FixtureRequest, base_url: str) -> Client:
247247
if env == "local":
248248
return LocalClient(
249249
lambda_url=base_url,
250-
headers={"nhsd-correlation-id": "local-test-correlation-id"},
251250
)
252251
elif env == "remote":
253252
return _create_remote_client(request)

pathology-api/tests/integration/test_endpoints.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ def test_bundle_returns_200(self, client: Client) -> None:
3131
data=bundle.model_dump_json(by_alias=True),
3232
path="FHIR/R4/Bundle",
3333
request_method="POST",
34-
headers={"nhsd-correlation-id": "test-nhsd-correlation-id-555666777"},
34+
headers={"X-Correlation-ID": "test-correlation-id-555666777"},
3535
)
3636

3737
assert response.status_code == 200
3838
assert response.headers["Content-Type"] == "application/fhir+json"
39-
assert response.headers.get("nhsd-correlation-id") is not None
39+
assert response.headers["nhsd-correlation-id"].startswith(
40+
".test-correlation-id-555666777"
41+
)
4042

4143
response_data = response.json()
4244
response_bundle = Bundle.model_validate(response_data, by_alias=True)
@@ -258,9 +260,16 @@ class TestStatusEndpoint:
258260

259261
@pytest.mark.status_auth_headers
260262
def test_status_returns_200(self, client: Client) -> None:
261-
response = client.send_without_payload(request_method="GET", path="_status")
263+
response = client.send_without_payload(
264+
request_method="GET",
265+
path="_status",
266+
headers={"X-Correlation-ID": "test-correlation-id-555666777"},
267+
)
262268
assert response.status_code == 200
263269
assert response.headers["Content-Type"] == "application/json"
270+
assert response.headers["nhsd-correlation-id"].startswith(
271+
".test-correlation-id-555666777"
272+
)
264273

265274
parsed = StatusResponse.model_validate(response.json())
266275

pathology-api/tests/schema/test_openapi_schema.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,5 @@ def test_api_schema_compliance(case: Case, base_url: str) -> None:
3333
- Validates inputs properly
3434
- Returns appropriate status codes
3535
"""
36-
# In local testing there is no APIM proxy to inject nhsd-correlation-id,
37-
# so we add it explicitly before calling the API.
38-
if case.headers is None:
39-
case.headers = {"nhsd-correlation-id": "local-test-correlation-id"}
40-
else:
41-
case.headers["nhsd-correlation-id"] = "local-test-correlation-id"
4236
# Call the API and validate the response against the schema
4337
case.call_and_validate(base_url=base_url, timeout=30)

0 commit comments

Comments
 (0)