Search in sources :

Example 1 with ClientResponse

use of cn.taketoday.web.reactive.function.client.ClientResponse 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 ClientResponse

use of cn.taketoday.web.reactive.function.client.ClientResponse 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 ClientResponse

use of cn.taketoday.web.reactive.function.client.ClientResponse 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 ClientResponse

use of cn.taketoday.web.reactive.function.client.ClientResponse 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 ClientResponse

use of cn.taketoday.web.reactive.function.client.ClientResponse 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

HttpStatus (cn.taketoday.http.HttpStatus)10 ClientResponse (cn.taketoday.web.reactive.function.client.ClientResponse)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 Mono (reactor.core.publisher.Mono)10 StepVerifier (reactor.test.StepVerifier)10 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)8 DataBufferUtils (cn.taketoday.core.io.buffer.DataBufferUtils)8 DefaultDataBufferFactory (cn.taketoday.core.io.buffer.DefaultDataBufferFactory)8 HttpHeaders (cn.taketoday.http.HttpHeaders)8 HttpMethod (cn.taketoday.http.HttpMethod)8 BodyExtractors (cn.taketoday.web.reactive.function.BodyExtractors)8 ClientRequest (cn.taketoday.web.reactive.function.client.ClientRequest)8 ExchangeFilterFunction (cn.taketoday.web.reactive.function.client.ExchangeFilterFunction)8 ExchangeFilterFunctions (cn.taketoday.web.reactive.function.client.ExchangeFilterFunctions)8 ExchangeFunction (cn.taketoday.web.reactive.function.client.ExchangeFunction)8 URI (java.net.URI)8 StandardCharsets (java.nio.charset.StandardCharsets)8 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)8 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)8 Test (org.junit.jupiter.api.Test)8