I have changed the version from 3.3.1 to 3.3.5 for spring boot in my project.
This caused the http headers to read only when using a filters. I found a workaround for this and its working perfectly fine.
But the trouble is that i am unable to write test for this inspite of many tries.
The workaround is
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
WebFilter writeableHeaders() {
return (exchange, chain) -> {
HttpHeaders writeableHeaders =
HttpHeaders.writableHttpHeaders(exchange.getRequest().getHeaders());
ServerHttpRequestDecorator writeableRequest =
new ServerHttpRequestDecorator(exchange.getRequest()) {
@Override
public HttpHeaders getHeaders() {
return writeableHeaders;
}
};
ServerWebExchange writeableExchange = exchange.mutate().request(writeableRequest).build();
return chain.filter(writeableExchange);
};
}
In my test case, i can use wiremock or MockWebServer. But Code is failing with 404 error
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class WebFilterWorkaroundTest {
@Autowired
private WebTestClient webTestClient;
private static WireMockServer wireMockServer;
@BeforeAll
static void startWireMock() {
wireMockServer = new WireMockServer(7653);
wireMockServer.start();
wireMockServer.stubFor(get(urlEqualTo("/instance-details/instance-type/abc"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withHeader("Custom-Header", "Value")
.withHeader("Nokia-ProjectHub-Project-ID","xyz")
.withBody("{\"message\":\"success\"}")
.withStatus(200))
);
//configureFor("localhost", wireMockServer.port());
System.setProperty("wiremock.server.port", String.valueOf(wireMockServer.port()));
System.out.println("WireMock is running on port: " + wireMockServer.port());
}
@AfterAll
static void stopWireMock() {
if (wireMockServer != null && wireMockServer.isRunning()) {
wireMockServer.stop();
}
}
@Test
void testWriteableHeadersFilter() {
webTestClient.get()
.uri("http://localhost:7653/instance-details/instance-type/abc")
.header("Nokia-ProjectHub-Project-ID","xyz")
.exchange()
.expectStatus().isOk()
.expectHeader().valueEquals("Custom-Header", "Value")
.expectBody()
.jsonPath("$.message").isEqualTo("success");
}
}
Can someone tell me if my approach is wrong, or if i am doing something wrong
I have tried to use wiremock to mock some api randomly that will git this workaround such that the "exhange" chain.filter is getting hit and the headers cam be modified.
The expectation is that the api should return a response with modified headers