Search in sources :

Example 1 with ExchangeFilterFunction

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);
}
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 2 with ExchangeFilterFunction

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();
}
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 ExchangeFilterFunction

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);
}
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 4 with ExchangeFilterFunction

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();
}
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) HttpStatus(cn.taketoday.http.HttpStatus) 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 ExchangeFilterFunction

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