Search in sources :

Example 21 with ClientRequest

use of org.springframework.web.reactive.function.client.ClientRequest in project spring-security by spring-projects.

the class ServletBearerExchangeFilterFunctionTests method filterWhenAuthenticatedWithOtherTokenThenAuthorizationHeaderNull.

// gh-7353
@Test
public void filterWhenAuthenticatedWithOtherTokenThenAuthorizationHeaderNull() {
    TestingAuthenticationToken token = new TestingAuthenticationToken("user", "pass");
    ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com")).build();
    this.function.filter(request, this.exchange).subscriberContext(context(token)).block();
    assertThat(this.exchange.getRequest().headers().getFirst(HttpHeaders.AUTHORIZATION)).isNull();
}
Also used : TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) Test(org.junit.jupiter.api.Test)

Example 22 with ClientRequest

use of org.springframework.web.reactive.function.client.ClientRequest in project spring-security by spring-projects.

the class ServletBearerExchangeFilterFunctionTests method filterWhenAuthenticatedThenAuthorizationHeader.

@Test
public void filterWhenAuthenticatedThenAuthorizationHeader() {
    ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com")).build();
    this.function.filter(request, this.exchange).subscriberContext(context(this.authentication)).block();
    assertThat(this.exchange.getRequest().headers().getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Bearer " + this.accessToken.getTokenValue());
}
Also used : ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) Test(org.junit.jupiter.api.Test)

Example 23 with ClientRequest

use of org.springframework.web.reactive.function.client.ClientRequest in project spring-boot by spring-projects.

the class MetricsWebClientFilterFunction method instrumentResponse.

private Mono<ClientResponse> instrumentResponse(ClientRequest request, Mono<ClientResponse> responseMono) {
    final AtomicBoolean responseReceived = new AtomicBoolean();
    return Mono.deferContextual((ctx) -> responseMono.doOnEach((signal) -> {
        if (signal.isOnNext() || signal.isOnError()) {
            responseReceived.set(true);
            Iterable<Tag> tags = this.tagProvider.tags(request, signal.get(), signal.getThrowable());
            recordTimer(tags, getStartTime(ctx));
        }
    }).doFinally((signalType) -> {
        if (!responseReceived.get() && SignalType.CANCEL.equals(signalType)) {
            Iterable<Tag> tags = this.tagProvider.tags(request, null, null);
            recordTimer(tags, getStartTime(ctx));
        }
    }));
}
Also used : Tag(io.micrometer.core.instrument.Tag) ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction) AutoTimer(org.springframework.boot.actuate.metrics.AutoTimer) ContextView(reactor.util.context.ContextView) Context(reactor.util.context.Context) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Mono(reactor.core.publisher.Mono) SignalType(reactor.core.publisher.SignalType) TimeUnit(java.util.concurrent.TimeUnit) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) ExchangeFilterFunction(org.springframework.web.reactive.function.client.ExchangeFilterFunction) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tag(io.micrometer.core.instrument.Tag)

Example 24 with ClientRequest

use of org.springframework.web.reactive.function.client.ClientRequest in project spring-boot by spring-projects.

the class MetricsWebClientFilterFunctionTests method filterWhenIoExceptionThrownShouldRecordTimer.

@Test
void filterWhenIoExceptionThrownShouldRecordTimer() {
    ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com/projects/spring-boot")).build();
    ExchangeFunction errorExchange = (r) -> Mono.error(new IOException());
    this.filterFunction.filter(request, errorExchange).onErrorResume(IOException.class, (t) -> Mono.empty()).block(Duration.ofSeconds(5));
    assertThat(this.registry.get("http.client.requests").tags("method", "GET", "uri", "/projects/spring-boot", "status", "IO_ERROR").timer().count()).isEqualTo(1);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) StepVerifier(reactor.test.StepVerifier) AutoTimer(org.springframework.boot.actuate.metrics.AutoTimer) MockClock(io.micrometer.core.instrument.MockClock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WebClient(org.springframework.web.reactive.function.client.WebClient) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) Timer(io.micrometer.core.instrument.Timer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BDDMockito.given(org.mockito.BDDMockito.given) Duration(java.time.Duration) URI(java.net.URI) ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction) MeterNotFoundException(io.micrometer.core.instrument.search.MeterNotFoundException) SimpleConfig(io.micrometer.core.instrument.simple.SimpleConfig) HttpMethod(org.springframework.http.HttpMethod) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Mockito.mock(org.mockito.Mockito.mock) IOException(java.io.IOException) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction) Test(org.junit.jupiter.api.Test)

Example 25 with ClientRequest

use of org.springframework.web.reactive.function.client.ClientRequest in project spring-boot by spring-projects.

the class MetricsWebClientFilterFunctionTests method filterWhenExceptionThrownShouldRecordTimer.

@Test
void filterWhenExceptionThrownShouldRecordTimer() {
    ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com/projects/spring-boot")).build();
    ExchangeFunction exchange = (r) -> Mono.error(new IllegalArgumentException());
    this.filterFunction.filter(request, exchange).onErrorResume(IllegalArgumentException.class, (t) -> Mono.empty()).block(Duration.ofSeconds(5));
    assertThat(this.registry.get("http.client.requests").tags("method", "GET", "uri", "/projects/spring-boot", "status", "CLIENT_ERROR").timer().count()).isEqualTo(1);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) StepVerifier(reactor.test.StepVerifier) AutoTimer(org.springframework.boot.actuate.metrics.AutoTimer) MockClock(io.micrometer.core.instrument.MockClock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WebClient(org.springframework.web.reactive.function.client.WebClient) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) Timer(io.micrometer.core.instrument.Timer) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BDDMockito.given(org.mockito.BDDMockito.given) Duration(java.time.Duration) URI(java.net.URI) ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction) MeterNotFoundException(io.micrometer.core.instrument.search.MeterNotFoundException) SimpleConfig(io.micrometer.core.instrument.simple.SimpleConfig) HttpMethod(org.springframework.http.HttpMethod) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Mockito.mock(org.mockito.Mockito.mock) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction) Test(org.junit.jupiter.api.Test)

Aggregations

ClientRequest (org.springframework.web.reactive.function.client.ClientRequest)63 Test (org.junit.jupiter.api.Test)59 OAuth2AuthorizedClient (org.springframework.security.oauth2.client.OAuth2AuthorizedClient)34 OAuth2RefreshToken (org.springframework.security.oauth2.core.OAuth2RefreshToken)21 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)18 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)18 OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)18 ClientResponse (org.springframework.web.reactive.function.client.ClientResponse)16 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)15 Mono (reactor.core.publisher.Mono)14 Instant (java.time.Instant)13 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)13 OAuth2AccessToken (org.springframework.security.oauth2.core.OAuth2AccessToken)13 ExchangeFunction (org.springframework.web.reactive.function.client.ExchangeFunction)13 URI (java.net.URI)12 HttpHeaders (org.springframework.http.HttpHeaders)12 HttpMethod (org.springframework.http.HttpMethod)12 HttpStatus (org.springframework.http.HttpStatus)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)11 Duration (java.time.Duration)10