diff --git a/src/main/java/uk/gov/hmcts/reform/ccd/test/stubs/service/controllers/StubResponseController.java b/src/main/java/uk/gov/hmcts/reform/ccd/test/stubs/service/controllers/StubResponseController.java index 9f054c83..b3323f17 100644 --- a/src/main/java/uk/gov/hmcts/reform/ccd/test/stubs/service/controllers/StubResponseController.java +++ b/src/main/java/uk/gov/hmcts/reform/ccd/test/stubs/service/controllers/StubResponseController.java @@ -60,6 +60,8 @@ public class StubResponseController { private static final Logger LOG = LoggerFactory.getLogger(StubResponseController.class); static final String WIREMOCK_STUB_MAPPINGS_ENDPOINT = "/__admin/mappings"; + static final List CUSTOM_HEADERS = List.of("Client-Context"); + @Value("${wiremock.server.host}") private String mockHttpServerHost; @@ -150,7 +152,7 @@ public ResponseEntity forwardGetRequests(HttpServletRequest request) thr HttpRequest httpRequest = HttpRequest.newBuilder(uri) .build(); HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); - return new ResponseEntity(httpResponse.body().toString(), + return new ResponseEntity<>(httpResponse.body().toString(), HttpStatus.valueOf(httpResponse.statusCode())); } catch (IOException e) { LOG.error("Error occurred", e); @@ -160,8 +162,6 @@ public ResponseEntity forwardGetRequests(HttpServletRequest request) thr } } - - /** * Forward POST requests to Wiremock Server and return POST responses to Test Stub Client. */ @@ -174,8 +174,16 @@ public ResponseEntity forwardPostRequests(HttpServletRequest request) th .POST(HttpRequest.BodyPublishers.ofString(requestBody)) .build(); HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); - return new ResponseEntity(httpResponse.body().toString(), - HttpStatus.valueOf(httpResponse.statusCode())); + HttpHeaders customHeaders = getCustomHeaders(httpResponse.headers()); + + if (customHeaders.size() > 0) { + return new ResponseEntity<>(httpResponse.body().toString(), + customHeaders, + HttpStatus.valueOf(httpResponse.statusCode())); + } else { + return new ResponseEntity<>(httpResponse.body().toString(), + HttpStatus.valueOf(httpResponse.statusCode())); + } } catch (IOException e) { LOG.error("Error occurred", e); return ResponseEntity @@ -196,7 +204,7 @@ public ResponseEntity forwardPutRequests(HttpServletRequest request) thr .PUT(HttpRequest.BodyPublishers.ofString(requestBody)) .build(); HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); - return new ResponseEntity(httpResponse.body().toString(), + return new ResponseEntity<>(httpResponse.body().toString(), HttpStatus.valueOf(httpResponse.statusCode())); } catch (IOException e) { LOG.error("Error occurred", e); @@ -217,7 +225,7 @@ public ResponseEntity forwardDeleteRequests(HttpServletRequest request) .DELETE() .build(); HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); - return new ResponseEntity(httpResponse.body().toString(), + return new ResponseEntity<>(httpResponse.body().toString(), HttpStatus.valueOf(httpResponse.statusCode())); } catch (IOException e) { LOG.error("Error occurred", e); @@ -246,7 +254,7 @@ public ResponseEntity configureUser(@RequestBody IdamUserInfo userInfo) .POST(HttpRequest.BodyPublishers.ofString(request)) .build(); HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); - ResponseEntity stringResponseEntity = new ResponseEntity(httpResponse.body().toString(), + ResponseEntity stringResponseEntity = new ResponseEntity<>(httpResponse.body().toString(), HttpStatus.valueOf(httpResponse.statusCode())); stringResponseEntity.getStatusCodeValue(); @@ -260,6 +268,18 @@ public ResponseEntity configureUser(@RequestBody IdamUserInfo userInfo) } } + private HttpHeaders getCustomHeaders(java.net.http.HttpHeaders originalHeaders) { + + HttpHeaders springHeaders = new HttpHeaders(); + CUSTOM_HEADERS.forEach(context -> { + if (null != originalHeaders && originalHeaders.map().containsKey(context.toLowerCase())) { + springHeaders.put(context, originalHeaders.map().get(context.toLowerCase())); + } + }); + + return springHeaders; + } + private String asJson(Object object) throws JsonProcessingException { return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object); } diff --git a/src/test/java/uk/gov/hmcts/reform/ccd/test/stubs/service/controllers/StubResponseControllerTest.java b/src/test/java/uk/gov/hmcts/reform/ccd/test/stubs/service/controllers/StubResponseControllerTest.java index 0fd10220..7e230bad 100644 --- a/src/test/java/uk/gov/hmcts/reform/ccd/test/stubs/service/controllers/StubResponseControllerTest.java +++ b/src/test/java/uk/gov/hmcts/reform/ccd/test/stubs/service/controllers/StubResponseControllerTest.java @@ -14,10 +14,12 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import uk.gov.hmcts.reform.ccd.test.stubs.service.mock.server.MockHttpServer; +import uk.gov.hmcts.reform.ccd.test.stubs.service.util.HeadersProvider; import java.io.IOException; import java.net.http.HttpClient; import java.net.http.HttpResponse; +import java.util.Collections; import java.util.Map; import javax.servlet.http.HttpServletRequest; @@ -115,6 +117,27 @@ void shouldReturnStatusOK_ForwardPostRequests() throws IOException, InterruptedE assertThat(responseEntityReturned.getStatusCode(), is(HttpStatus.OK)); } + /** + * Forward POST requests unit test with status OK. + */ + @Test + @DisplayName("Test for forwardPostRequests() status OK - empty custom headers") + void shouldReturnStatusOK_ForwardPostRequestsForEmptyCustomHeaders() throws IOException, InterruptedException { + HttpServletRequest mockRequest = mock(HttpServletRequest.class); + HttpResponse mockResponse = mock(HttpResponse.class); + Mockito.doReturn(mockResponse).when(mockHttpClient).send(any(), any()); + when(mockResponse.body()).thenReturn("MOCK BODY"); + + HeadersProvider mockHeadersProvider = mock(HeadersProvider.class); + when(mockHeadersProvider.getHeaders()).thenReturn(Collections.emptyMap()); + + when(mockResponse.statusCode()).thenReturn(200); + + ResponseEntity responseEntityReturned = stubResponseController.forwardPostRequests(mockRequest); + assertNotNull(responseEntityReturned); + assertThat(responseEntityReturned.getStatusCode(), is(HttpStatus.OK)); + } + /** * Forward POST requests unit test with exception thrown. */ diff --git a/src/test/java/uk/gov/hmcts/reform/ccd/test/stubs/service/util/HeadersProvider.java b/src/test/java/uk/gov/hmcts/reform/ccd/test/stubs/service/util/HeadersProvider.java new file mode 100644 index 00000000..6f51e44e --- /dev/null +++ b/src/test/java/uk/gov/hmcts/reform/ccd/test/stubs/service/util/HeadersProvider.java @@ -0,0 +1,8 @@ +package uk.gov.hmcts.reform.ccd.test.stubs.service.util; + +import java.util.List; +import java.util.Map; + +public interface HeadersProvider { + Map> getHeaders(); +} diff --git a/src/test/java/uk/gov/hmcts/reform/ccd/test/stubs/service/util/HttpHeadersProvider.java b/src/test/java/uk/gov/hmcts/reform/ccd/test/stubs/service/util/HttpHeadersProvider.java new file mode 100644 index 00000000..74bf968f --- /dev/null +++ b/src/test/java/uk/gov/hmcts/reform/ccd/test/stubs/service/util/HttpHeadersProvider.java @@ -0,0 +1,18 @@ +package uk.gov.hmcts.reform.ccd.test.stubs.service.util; + +import java.net.http.HttpHeaders; +import java.util.List; +import java.util.Map; + +public class HttpHeadersProvider implements HeadersProvider { + private final HttpHeaders httpHeaders; + + public HttpHeadersProvider(HttpHeaders httpHeaders) { + this.httpHeaders = httpHeaders; + } + + @Override + public Map> getHeaders() { + return httpHeaders.map(); + } +} diff --git a/wiremock/mappings/ccd/callback_about_to_submit.json b/wiremock/mappings/ccd/callback_about_to_submit.json index 3b10ccb2..611ab9e4 100644 --- a/wiremock/mappings/ccd/callback_about_to_submit.json +++ b/wiremock/mappings/ccd/callback_about_to_submit.json @@ -6,7 +6,8 @@ "response": { "status": 200, "headers": { - "Content-Type": "application/json" + "Content-Type": "application/json", + "Client-Context": "ewogICJ1c2VyX3Rhc2siOiB7CiAgICAidGFza19kYXRhIjogewogICAgICAgInRhc2sxIjogIkluaXRpYWwgY2FsbCIsCiAgICAgICAidGFzazIiOiAiQWJvdXQgdG8gc3VibWl0IC0gY29tcGxldGVkIiwKICAgICAgICJ0YXNrMyI6ICJTdWJtaXR0ZWQgLSBwZW5kaW5nIgogICAgfQogIH0sCiAgImZpZWxkMSI6ICJTdHJpbmciCn0=" }, "jsonBody": { "data": { diff --git a/wiremock/mappings/ccd/callback_about_to_submit_party_info_modified.json b/wiremock/mappings/ccd/callback_about_to_submit_party_info_modified.json index b340584b..3b1c9f74 100644 --- a/wiremock/mappings/ccd/callback_about_to_submit_party_info_modified.json +++ b/wiremock/mappings/ccd/callback_about_to_submit_party_info_modified.json @@ -6,7 +6,8 @@ "response": { "status": 200, "headers": { - "Content-Type": "application/json" + "Content-Type": "application/json", + "Client-Context": "ewogICJ1c2VyX3Rhc2siOiB7CiAgICAidGFza19kYXRhIjogewogICAgICAgInRhc2sxIjogIkluaXRpYWwgY2FsbCIsCiAgICAgICAidGFzazIiOiAiQWJvdXQgdG8gc3VibWl0IC0gY29tcGxldGVkIiwKICAgICAgICJ0YXNrMyI6ICJTdWJtaXR0ZWQgLSBwZW5kaW5nIgogICAgfQogIH0sCiAgImZpZWxkMSI6ICJTdHJpbmciCn0=" }, "jsonBody": { "data": { diff --git a/wiremock/mappings/ccd/callback_submitted.json b/wiremock/mappings/ccd/callback_submitted.json index 2518e672..58de868f 100644 --- a/wiremock/mappings/ccd/callback_submitted.json +++ b/wiremock/mappings/ccd/callback_submitted.json @@ -6,6 +6,7 @@ "response": { "status": 200, "headers": { + "Client-Context": "ewogICJ1c2VyX3Rhc2siOiB7CiAgICAgICAgInRhc2tfZGF0YSI6IHsKCSAgICAgICAgInRhc2sxIjogIkluaXRpYWwgY2FsbCIsCgkgICAgICAgICJ0YXNrMiI6ICJBYm91dCB0byBzdWJtaXQgLSBjb21wbGV0ZWQiLAogICAgICAgICAgICAidGFzazMiOiAiU3VibWl0dGVkIC0gY29tcGxldGVkIgogICAgICAgIH0KICB9LAogICJmaWVsZDIiOiAiU3RyaW5nIiwKICAiY29tcGxldGVfdGFzayI6IHRydWUKfQ==", "Content-Type": "application/json" }, "jsonBody": {