I ran into a similar scenario where I wanted to write a unit test for the various compression scenarios, however, the WebTestClient always returns the decompressed response. Here's my workaround to access the raw response as well as the original headers.
@Honey were you able to figure out any other alternatives?
Disable compression on the WebClient. This will disable both response decompression as well as the "Accept-Encoding=gzip" request header.
@Bean
public WebClient webClient() {
return WebClient
.builder()
.clientConnector(
new ReactorClientHttpConnector(HttpClient.create().compress(false)))
// ^ disabling compression
.build();
}
Manually add the "Accept-Encoding=gzip" request header. This will ensure that the server responds with the compressed payload, but the client doesn't decompress it.
public Mono<Response> reactiveSendRequest(
String body, URI url, String method, HttpHeaders headers) {
return webClient
.method(HttpMethod.valueOf(method.toUpperCase()))
.uri(url)
.bodyValue(body)
.headers(h -> h.addAll(headers))
.header("Accept-Encoding", "gzip")
// ^ manually adding the header back
.retrieve()
.toEntity(byte[].class)
.timeout(this.timeout)
.map(rawResponse -> {
return new Response(rawResponse);
});
}