Search in sources :

Example 1 with ExchangeFunction

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

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

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

use of cn.taketoday.web.reactive.function.client.ExchangeFunction in project today-infrastructure by TAKETODAY.

the class ExchangeFilterFunctionsTests method basicAuthenticationInvalidCharacters.

@Test
public void basicAuthenticationInvalidCharacters() {
    ClientRequest request = ClientRequest.create(HttpMethod.GET, DEFAULT_URL).build();
    ExchangeFunction exchange = r -> Mono.just(mock(ClientResponse.class));
    assertThatIllegalArgumentException().isThrownBy(() -> ExchangeFilterFunctions.basicAuthentication("foo", "\ud83d\udca9").filter(request, exchange));
}
Also used : 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) ClientResponse(cn.taketoday.web.reactive.function.client.ClientResponse) 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 ExchangeFunction

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

Aggregations

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