88from pathology_api .exception import ValidationError
99from pathology_api .fhir .r4 .elements import LogicalReference , PatientIdentifier
1010from pathology_api .fhir .r4 .resources import Bundle , Composition , OperationOutcome
11+ from pathology_api .request_context import get_correlation_id
1112
1213
1314class TestHandler :
@@ -16,9 +17,11 @@ def _create_test_event(
1617 body : str | None = None ,
1718 path_params : str | None = None ,
1819 request_method : str | None = None ,
20+ headers : dict [str , str ] | None = None ,
1921 ) -> dict [str , Any ]:
2022 return {
2123 "body" : body ,
24+ "headers" : headers or {},
2225 "requestContext" : {
2326 "http" : {
2427 "path" : f"/{ path_params } " ,
@@ -58,6 +61,7 @@ def test_create_test_result_success(self) -> None:
5861 body = bundle .model_dump_json (by_alias = True ),
5962 path_params = "FHIR/R4/Bundle" ,
6063 request_method = "POST" ,
64+ headers = {"nhsd-correlation-id" : "test-correlation-id" },
6165 )
6266 context = LambdaContext ()
6367
@@ -76,9 +80,72 @@ def test_create_test_result_success(self) -> None:
7680 # A UUID value so can only check its presence.
7781 assert response_bundle .id is not None
7882
83+ def test_correlation_id_is_set_from_request_header (self ) -> None :
84+ correlation_id = "test-correlation-id-abc-123"
85+ bundle = Bundle .create (
86+ type = "document" ,
87+ entry = [
88+ Bundle .Entry (
89+ fullUrl = "composition" ,
90+ resource = Composition .create (
91+ subject = LogicalReference (
92+ PatientIdentifier .from_nhs_number ("nhs_number" )
93+ )
94+ ),
95+ )
96+ ],
97+ )
98+ event = self ._create_test_event (
99+ body = bundle .model_dump_json (by_alias = True ),
100+ path_params = "FHIR/R4/Bundle" ,
101+ request_method = "POST" ,
102+ headers = {"nhsd-correlation-id" : correlation_id },
103+ )
104+ context = LambdaContext ()
105+
106+ handler (event , context )
107+
108+ assert get_correlation_id () == correlation_id
109+
110+ def test_missing_correlation_id_header_returns_400 (self ) -> None :
111+ bundle = Bundle .create (
112+ type = "document" ,
113+ entry = [
114+ Bundle .Entry (
115+ fullUrl = "composition" ,
116+ resource = Composition .create (
117+ subject = LogicalReference (
118+ PatientIdentifier .from_nhs_number ("nhs_number" )
119+ )
120+ ),
121+ )
122+ ],
123+ )
124+ event = self ._create_test_event (
125+ body = bundle .model_dump_json (by_alias = True ),
126+ path_params = "FHIR/R4/Bundle" ,
127+ request_method = "POST" ,
128+ )
129+ context = LambdaContext ()
130+
131+ response = handler (event , context )
132+
133+ assert response ["statusCode" ] == 400
134+ assert response ["headers" ] == {"Content-Type" : "application/fhir+json" }
135+
136+ returned_issue = self ._parse_returned_issue (response ["body" ])
137+ assert returned_issue ["severity" ] == "error"
138+ assert returned_issue ["code" ] == "invalid"
139+ assert (
140+ returned_issue ["diagnostics" ]
141+ == "Missing required header: nhsd-correlation-id"
142+ )
143+
79144 def test_create_test_result_no_payload (self ) -> None :
80145 event = self ._create_test_event (
81- path_params = "FHIR/R4/Bundle" , request_method = "POST"
146+ path_params = "FHIR/R4/Bundle" ,
147+ request_method = "POST" ,
148+ headers = {"nhsd-correlation-id" : "test-correlation-id" },
82149 )
83150 context = LambdaContext ()
84151
@@ -98,7 +165,10 @@ def test_create_test_result_no_payload(self) -> None:
98165
99166 def test_create_test_result_empty_payload (self ) -> None :
100167 event = self ._create_test_event (
101- body = "{}" , path_params = "FHIR/R4/Bundle" , request_method = "POST"
168+ body = "{}" ,
169+ path_params = "FHIR/R4/Bundle" ,
170+ request_method = "POST" ,
171+ headers = {"nhsd-correlation-id" : "test-correlation-id" },
102172 )
103173 context = LambdaContext ()
104174
@@ -118,7 +188,10 @@ def test_create_test_result_empty_payload(self) -> None:
118188
119189 def test_create_test_result_invalid_json (self ) -> None :
120190 event = self ._create_test_event (
121- body = "invalid json" , path_params = "FHIR/R4/Bundle" , request_method = "POST"
191+ body = "invalid json" ,
192+ path_params = "FHIR/R4/Bundle" ,
193+ request_method = "POST" ,
194+ headers = {"nhsd-correlation-id" : "test-correlation-id" },
122195 )
123196 context = LambdaContext ()
124197
@@ -169,6 +242,7 @@ def test_create_test_result_processing_error(
169242 body = bundle .model_dump_json (by_alias = True ),
170243 path_params = "FHIR/R4/Bundle" ,
171244 request_method = "POST" ,
245+ headers = {"nhsd-correlation-id" : "test-correlation-id" },
172246 )
173247 context = LambdaContext ()
174248
@@ -207,6 +281,7 @@ def test_create_test_result_model_validate_error(
207281 body = bundle .model_dump_json (by_alias = True ),
208282 path_params = "FHIR/R4/Bundle" ,
209283 request_method = "POST" ,
284+ headers = {"nhsd-correlation-id" : "test-correlation-id" },
210285 )
211286 context = LambdaContext ()
212287
0 commit comments