79433497

Date: 2025-02-12 14:56:57
Score: 0.5
Natty:
Report link

For the record, this might be helpful for a noob like me in Reactor testing.

I struggled very much to figure it out how to test an ExchangeFilterFunction. Really it took me many hours and tried all the suggestions from StackOverflow, reading the complete documentation from the Project Reactor about testing and so on. Even tried using AI, but if failed absolutely spectacularly.

And just before I wanted to give up I stumbled about this gem. It is a unit test from the Spring Weblux source code and really opened my eyes.

ExchangeFilterFunctionsTests.java

I speak about this particular one:

    @Test
    void basicAuthenticationUsernamePassword() {
        ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).build();
        ClientResponse response = mock();

        ExchangeFunction exchange = r -> {
            assertThat(r.headers().containsHeader(HttpHeaders.AUTHORIZATION)).isTrue();
            assertThat(r.headers().getFirst(HttpHeaders.AUTHORIZATION)).startsWith("Basic ");
            return Mono.just(response);
        };

        ExchangeFilterFunction auth = ExchangeFilterFunctions.basicAuthentication("foo", "bar");
        assertThat(request.headers().containsHeader(HttpHeaders.AUTHORIZATION)).isFalse();
        ClientResponse result = auth.filter(request, exchange).block();
        assertThat(result).isEqualTo(response);
    }

Once I put this in my codebase and started playing around with it (I had to adapt it a little and make it compile, probably I have an older Spring version), I could debug and modify it and very soon I was able to test my own implementation of an ExchangeFilterFunction.

This is such a elegant way of testing in isolation just the mutation done on a request inside an ExchangeFilterFunction. I will give the full example. I need to test an ExchangeFilterFunction function which reads some headers from a supplier and adds them to the outgoing request.

  @Test
  void should_propagate_headers_from_supplier_to_outgoing_request() {
    ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com")).build();
    ClientResponse response = Mockito.mock();

    ExchangeFilterFunction exchangeFilterFunction = getPropagateHeadersExchangeFilter(
        () -> {
          HttpHeaders headers = new HttpHeaders();
          headers.putAll(getTestHeaders());
          return Mono.just(headers);
        });

    ExchangeFunction exchange = clientRequest -> {
      getTestHeaders().forEach((headerName, headerValues) -> {
        assertThat(clientRequest.headers()).containsEntry(headerName, headerValues);
      });
      return Mono.just(response);
    };

    ClientResponse result = exchangeFilterFunction.filter(request, exchange).block();
    assertThat(result).isEqualTo(response);
  }
Reasons:
  • Blacklisted phrase (0.5): I need
  • Blacklisted phrase (1): StackOverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: cristian.andrei.stan