79140366

Date: 2024-10-30 09:09:31
Score: 7 🚩
Natty:
Report link

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?

  1. 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();
    }
    
  2. 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);
        });
    }
    
Reasons:
  • RegEx Blacklisted phrase (3): were you able to figure out
  • RegEx Blacklisted phrase (3): were you able
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • User mentioned (1): @Honey
  • Low reputation (1):
Posted by: aj1001