Search in sources :

Example 1 with ClientRequest

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();
}
Also used : ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) ExchangeFunction(cn.taketoday.web.reactive.function.client.ExchangeFunction) StepVerifier(reactor.test.StepVerifier) ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) StandardCharsets(java.nio.charset.StandardCharsets) BodyExtractors(cn.taketoday.web.reactive.function.BodyExtractors) ExchangeFilterFunction(cn.taketoday.web.reactive.function.client.ExchangeFilterFunction) Test(org.junit.jupiter.api.Test) HttpMethod(cn.taketoday.http.HttpMethod) Flux(reactor.core.publisher.Flux) HttpHeaders(cn.taketoday.http.HttpHeaders) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) ClientRequest(cn.taketoday.web.reactive.function.client.ClientRequest) BDDMockito.given(org.mockito.BDDMockito.given) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) HttpStatus(cn.taketoday.http.HttpStatus) ExchangeFilterFunctions(cn.taketoday.web.reactive.function.client.ExchangeFilterFunctions) URI(java.net.URI) Mockito.mock(org.mockito.Mockito.mock) ClientRequest(cn.taketoday.web.reactive.function.client.ClientRequest) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 2 with ClientRequest

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);
}
Also used : ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) ExchangeFunction(cn.taketoday.web.reactive.function.client.ExchangeFunction) StepVerifier(reactor.test.StepVerifier) ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) StandardCharsets(java.nio.charset.StandardCharsets) BodyExtractors(cn.taketoday.web.reactive.function.BodyExtractors) ExchangeFilterFunction(cn.taketoday.web.reactive.function.client.ExchangeFilterFunction) Test(org.junit.jupiter.api.Test) HttpMethod(cn.taketoday.http.HttpMethod) Flux(reactor.core.publisher.Flux) HttpHeaders(cn.taketoday.http.HttpHeaders) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) ClientRequest(cn.taketoday.web.reactive.function.client.ClientRequest) BDDMockito.given(org.mockito.BDDMockito.given) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) HttpStatus(cn.taketoday.http.HttpStatus) ExchangeFilterFunctions(cn.taketoday.web.reactive.function.client.ExchangeFilterFunctions) URI(java.net.URI) Mockito.mock(org.mockito.Mockito.mock) ExchangeFilterFunction(cn.taketoday.web.reactive.function.client.ExchangeFilterFunction) ClientRequest(cn.taketoday.web.reactive.function.client.ClientRequest) ExchangeFunction(cn.taketoday.web.reactive.function.client.ExchangeFunction) Test(org.junit.jupiter.api.Test)

Example 3 with ClientRequest

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();
}
Also used : ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) ExchangeFunction(cn.taketoday.web.reactive.function.client.ExchangeFunction) StepVerifier(reactor.test.StepVerifier) ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) StandardCharsets(java.nio.charset.StandardCharsets) BodyExtractors(cn.taketoday.web.reactive.function.BodyExtractors) ExchangeFilterFunction(cn.taketoday.web.reactive.function.client.ExchangeFilterFunction) Test(org.junit.jupiter.api.Test) HttpMethod(cn.taketoday.http.HttpMethod) Flux(reactor.core.publisher.Flux) HttpHeaders(cn.taketoday.http.HttpHeaders) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) ClientRequest(cn.taketoday.web.reactive.function.client.ClientRequest) BDDMockito.given(org.mockito.BDDMockito.given) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) HttpStatus(cn.taketoday.http.HttpStatus) ExchangeFilterFunctions(cn.taketoday.web.reactive.function.client.ExchangeFilterFunctions) URI(java.net.URI) Mockito.mock(org.mockito.Mockito.mock) ClientRequest(cn.taketoday.web.reactive.function.client.ClientRequest) Test(org.junit.jupiter.api.Test)

Example 4 with ClientRequest

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();
}
Also used : ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) ExchangeFunction(cn.taketoday.web.reactive.function.client.ExchangeFunction) StepVerifier(reactor.test.StepVerifier) ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) StandardCharsets(java.nio.charset.StandardCharsets) BodyExtractors(cn.taketoday.web.reactive.function.BodyExtractors) ExchangeFilterFunction(cn.taketoday.web.reactive.function.client.ExchangeFilterFunction) Test(org.junit.jupiter.api.Test) HttpMethod(cn.taketoday.http.HttpMethod) Flux(reactor.core.publisher.Flux) HttpHeaders(cn.taketoday.http.HttpHeaders) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) ClientRequest(cn.taketoday.web.reactive.function.client.ClientRequest) BDDMockito.given(org.mockito.BDDMockito.given) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) HttpStatus(cn.taketoday.http.HttpStatus) ExchangeFilterFunctions(cn.taketoday.web.reactive.function.client.ExchangeFilterFunctions) URI(java.net.URI) Mockito.mock(org.mockito.Mockito.mock) ExchangeFilterFunction(cn.taketoday.web.reactive.function.client.ExchangeFilterFunction) ClientRequest(cn.taketoday.web.reactive.function.client.ClientRequest) ExchangeFunction(cn.taketoday.web.reactive.function.client.ExchangeFunction) Test(org.junit.jupiter.api.Test)

Example 5 with ClientRequest

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);
}
Also used : ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) ExchangeFunction(cn.taketoday.web.reactive.function.client.ExchangeFunction) StepVerifier(reactor.test.StepVerifier) ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) StandardCharsets(java.nio.charset.StandardCharsets) BodyExtractors(cn.taketoday.web.reactive.function.BodyExtractors) ExchangeFilterFunction(cn.taketoday.web.reactive.function.client.ExchangeFilterFunction) Test(org.junit.jupiter.api.Test) HttpMethod(cn.taketoday.http.HttpMethod) Flux(reactor.core.publisher.Flux) HttpHeaders(cn.taketoday.http.HttpHeaders) DataBufferUtils(cn.taketoday.core.io.buffer.DataBufferUtils) ClientRequest(cn.taketoday.web.reactive.function.client.ClientRequest) BDDMockito.given(org.mockito.BDDMockito.given) DataBuffer(cn.taketoday.core.io.buffer.DataBuffer) DefaultDataBufferFactory(cn.taketoday.core.io.buffer.DefaultDataBufferFactory) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) HttpStatus(cn.taketoday.http.HttpStatus) ExchangeFilterFunctions(cn.taketoday.web.reactive.function.client.ExchangeFilterFunctions) URI(java.net.URI) Mockito.mock(org.mockito.Mockito.mock) ExchangeFilterFunction(cn.taketoday.web.reactive.function.client.ExchangeFilterFunction) ClientRequest(cn.taketoday.web.reactive.function.client.ClientRequest) ExchangeFunction(cn.taketoday.web.reactive.function.client.ExchangeFunction) Test(org.junit.jupiter.api.Test)

Aggregations

DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)9 DataBufferUtils (cn.taketoday.core.io.buffer.DataBufferUtils)9 DefaultDataBufferFactory (cn.taketoday.core.io.buffer.DefaultDataBufferFactory)9 HttpHeaders (cn.taketoday.http.HttpHeaders)9 HttpMethod (cn.taketoday.http.HttpMethod)9 HttpStatus (cn.taketoday.http.HttpStatus)9 BodyExtractors (cn.taketoday.web.reactive.function.BodyExtractors)9 ClientRequest (cn.taketoday.web.reactive.function.client.ClientRequest)9 ClientResponse (cn.taketoday.web.reactive.function.client.ClientResponse)9 ExchangeFilterFunction (cn.taketoday.web.reactive.function.client.ExchangeFilterFunction)9 ExchangeFilterFunctions (cn.taketoday.web.reactive.function.client.ExchangeFilterFunctions)9 ExchangeFunction (cn.taketoday.web.reactive.function.client.ExchangeFunction)9 URI (java.net.URI)9 StandardCharsets (java.nio.charset.StandardCharsets)9 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)9 Test (org.junit.jupiter.api.Test)9 BDDMockito.given (org.mockito.BDDMockito.given)9 Mockito.mock (org.mockito.Mockito.mock)9