use of cn.taketoday.web.reactive.function.client.ExchangeFilterFunction in project today-infrastructure by TAKETODAY.
the class ExchangeFilterFunctionsTests method basicAuthenticationUsernamePassword.
@Test
public void basicAuthenticationUsernamePassword() {
ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).build();
ClientResponse response = mock(ClientResponse.class);
ExchangeFunction exchange = r -> {
assertThat(r.headers().containsKey(HttpHeaders.AUTHORIZATION)).isTrue();
assertThat(r.headers().getFirst(HttpHeaders.AUTHORIZATION).startsWith("Basic ")).isTrue();
return Mono.just(response);
};
ExchangeFilterFunction auth = ExchangeFilterFunctions.basicAuthentication("foo", "bar");
assertThat(request.headers().containsKey(HttpHeaders.AUTHORIZATION)).isFalse();
ClientResponse result = auth.filter(request, exchange).block();
assertThat(result).isEqualTo(response);
}
use of cn.taketoday.web.reactive.function.client.ExchangeFilterFunction in project today-infrastructure by TAKETODAY.
the class ExchangeFilterFunctionsTests method apply.
@Test
public void apply() {
ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).build();
ClientResponse response = mock(ClientResponse.class);
ExchangeFunction exchange = r -> Mono.just(response);
boolean[] filterInvoked = new boolean[1];
ExchangeFilterFunction filter = (r, n) -> {
assertThat(filterInvoked[0]).isFalse();
filterInvoked[0] = true;
return n.exchange(r);
};
ExchangeFunction filteredExchange = filter.apply(exchange);
ClientResponse result = filteredExchange.exchange(request).block();
assertThat(result).isEqualTo(response);
assertThat(filterInvoked[0]).isTrue();
}
use of cn.taketoday.web.reactive.function.client.ExchangeFilterFunction in project today-infrastructure by TAKETODAY.
the class ExchangeFilterFunctionsTests method basicAuthenticationAttributes.
@Test
@SuppressWarnings("deprecation")
public void basicAuthenticationAttributes() {
ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).attributes(cn.taketoday.web.reactive.function.client.ExchangeFilterFunctions.Credentials.basicAuthenticationCredentials("foo", "bar")).build();
ClientResponse response = mock(ClientResponse.class);
ExchangeFunction exchange = r -> {
assertThat(r.headers().containsKey(HttpHeaders.AUTHORIZATION)).isTrue();
assertThat(r.headers().getFirst(HttpHeaders.AUTHORIZATION).startsWith("Basic ")).isTrue();
return Mono.just(response);
};
ExchangeFilterFunction auth = ExchangeFilterFunctions.basicAuthentication();
assertThat(request.headers().containsKey(HttpHeaders.AUTHORIZATION)).isFalse();
ClientResponse result = auth.filter(request, exchange).block();
assertThat(result).isEqualTo(response);
}
use of cn.taketoday.web.reactive.function.client.ExchangeFilterFunction in project today-infrastructure by TAKETODAY.
the class ExchangeFilterFunctionsTests method statusHandlerMatch.
@Test
public void statusHandlerMatch() {
ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).build();
ClientResponse response = mock(ClientResponse.class);
given(response.statusCode()).willReturn(HttpStatus.NOT_FOUND);
ExchangeFunction exchange = r -> Mono.just(response);
ExchangeFilterFunction errorHandler = ExchangeFilterFunctions.statusError(HttpStatus::is4xxClientError, r -> new MyException());
Mono<ClientResponse> result = errorHandler.filter(request, exchange);
StepVerifier.create(result).expectError(MyException.class).verify();
}
use of cn.taketoday.web.reactive.function.client.ExchangeFilterFunction in project today-infrastructure by TAKETODAY.
the class ExchangeFilterFunctionsTests method basicAuthenticationAbsentAttributes.
@Test
@SuppressWarnings("deprecation")
public void basicAuthenticationAbsentAttributes() {
ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).build();
ClientResponse response = mock(ClientResponse.class);
ExchangeFunction exchange = r -> {
assertThat(r.headers().containsKey(HttpHeaders.AUTHORIZATION)).isFalse();
return Mono.just(response);
};
ExchangeFilterFunction auth = ExchangeFilterFunctions.basicAuthentication();
assertThat(request.headers().containsKey(HttpHeaders.AUTHORIZATION)).isFalse();
ClientResponse result = auth.filter(request, exchange).block();
assertThat(result).isEqualTo(response);
}
Aggregations