use of cn.taketoday.web.reactive.function.client.ClientRequest in project today-infrastructure by TAKETODAY.
the class ExchangeFilterFunctionsTests method limitResponseSize.
@Test
public void limitResponseSize() {
DataBuffer b1 = dataBuffer("foo");
DataBuffer b2 = dataBuffer("bar");
DataBuffer b3 = dataBuffer("baz");
ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).build();
ClientResponse response = ClientResponse.create(HttpStatus.OK).body(Flux.just(b1, b2, b3)).build();
Mono<ClientResponse> result = ExchangeFilterFunctions.limitResponseSize(5).filter(request, req -> Mono.just(response));
StepVerifier.create(result.flatMapMany(res -> res.body(BodyExtractors.toDataBuffers()))).consumeNextWith(buffer -> assertThat(string(buffer)).isEqualTo("foo")).consumeNextWith(buffer -> assertThat(string(buffer)).isEqualTo("ba")).expectComplete().verify();
}
use of cn.taketoday.web.reactive.function.client.ClientRequest 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.ClientRequest in project today-infrastructure by TAKETODAY.
the class ExchangeFilterFunctionsTests method statusHandlerNoMatch.
@Test
public void statusHandlerNoMatch() {
ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).build();
ClientResponse response = mock(ClientResponse.class);
given(response.statusCode()).willReturn(HttpStatus.NOT_FOUND);
Mono<ClientResponse> result = ExchangeFilterFunctions.statusError(HttpStatus::is5xxServerError, req -> new MyException()).filter(request, req -> Mono.just(response));
StepVerifier.create(result).expectNext(response).expectComplete().verify();
}
use of cn.taketoday.web.reactive.function.client.ClientRequest 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.ClientRequest 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);
}
Aggregations