Search in sources :

Example 31 with ClientRequest

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

the class MetricsWebClientFilterFunctionTests method filterWhenCancelAfterResponseThrownShouldNotRecordTimer.

@Test
void filterWhenCancelAfterResponseThrownShouldNotRecordTimer() {
    ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com/projects/spring-boot")).build();
    given(this.response.rawStatusCode()).willReturn(HttpStatus.OK.value());
    Mono<ClientResponse> filter = this.filterFunction.filter(request, this.exchange);
    StepVerifier.create(filter).expectNextCount(1).thenCancel().verify(Duration.ofSeconds(5));
    assertThat(this.registry.get("http.client.requests").tags("method", "GET", "uri", "/projects/spring-boot", "status", "200").timer().count()).isEqualTo(1);
    assertThatThrownBy(() -> this.registry.get("http.client.requests").tags("method", "GET", "uri", "/projects/spring-boot", "status", "CLIENT_ERROR").timer()).isInstanceOf(MeterNotFoundException.class);
}
Also used : ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) Test(org.junit.jupiter.api.Test)

Example 32 with ClientRequest

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

the class MetricsWebClientFilterFunctionTests method filterShouldRecordTimer.

@Test
void filterShouldRecordTimer() {
    ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com/projects/spring-boot")).build();
    given(this.response.rawStatusCode()).willReturn(HttpStatus.OK.value());
    this.filterFunction.filter(request, this.exchange).block(Duration.ofSeconds(5));
    assertThat(this.registry.get("http.client.requests").tags("method", "GET", "uri", "/projects/spring-boot", "status", "200").timer().count()).isEqualTo(1);
}
Also used : ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) Test(org.junit.jupiter.api.Test)

Example 33 with ClientRequest

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

the class SecurityReactorContextConfigurationTests method createPublisherWhenLastOperatorAddedThenSecurityContextAttributesAvailable.

@Test
public void createPublisherWhenLastOperatorAddedThenSecurityContextAttributesAvailable() {
    // Trigger the importing of SecurityReactorContextConfiguration via
    // OAuth2ImportSelector
    this.spring.register(SecurityConfig.class).autowire();
    // Setup for SecurityReactorContextSubscriberRegistrar
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.servletRequest, this.servletResponse));
    SecurityContextHolder.getContext().setAuthentication(this.authentication);
    ClientResponse clientResponseOk = ClientResponse.create(HttpStatus.OK).build();
    // @formatter:off
    ExchangeFilterFunction filter = (req, next) -> Mono.subscriberContext().filter((ctx) -> ctx.hasKey(SecurityReactorContextSubscriber.SECURITY_CONTEXT_ATTRIBUTES)).map((ctx) -> ctx.get(SecurityReactorContextSubscriber.SECURITY_CONTEXT_ATTRIBUTES)).cast(Map.class).map((attributes) -> {
        if (attributes.containsKey(HttpServletRequest.class) && attributes.containsKey(HttpServletResponse.class) && attributes.containsKey(Authentication.class)) {
            return clientResponseOk;
        } else {
            return ClientResponse.create(HttpStatus.NOT_FOUND).build();
        }
    });
    // @formatter:on
    ClientRequest clientRequest = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com")).build();
    MockExchangeFunction exchange = new MockExchangeFunction();
    Map<Object, Object> expectedContextAttributes = new HashMap<>();
    expectedContextAttributes.put(HttpServletRequest.class, this.servletRequest);
    expectedContextAttributes.put(HttpServletResponse.class, this.servletResponse);
    expectedContextAttributes.put(Authentication.class, this.authentication);
    Mono<ClientResponse> clientResponseMono = filter.filter(clientRequest, exchange).flatMap((response) -> filter.filter(clientRequest, exchange));
    // @formatter:off
    StepVerifier.create(clientResponseMono).expectAccessibleContext().contains(SecurityReactorContextSubscriber.SECURITY_CONTEXT_ATTRIBUTES, expectedContextAttributes).then().expectNext(clientResponseOk).verifyComplete();
// @formatter:on
}
Also used : ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) HttpSecurity(org.springframework.security.config.annotation.web.builders.HttpSecurity) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) RequestContextHolder(org.springframework.web.context.request.RequestContextHolder) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) CoreSubscriber(reactor.core.CoreSubscriber) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) RequestAttributes(org.springframework.web.context.request.RequestAttributes) SecurityReactorContextSubscriber(org.springframework.security.config.annotation.web.configuration.SecurityReactorContextConfiguration.SecurityReactorContextSubscriber) URI(java.net.URI) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) Operators(reactor.core.publisher.Operators) Context(reactor.util.context.Context) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockExchangeFunction(org.springframework.security.oauth2.client.web.reactive.function.client.MockExchangeFunction) Assertions.entry(org.assertj.core.api.Assertions.entry) BaseSubscriber(reactor.core.publisher.BaseSubscriber) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) AfterEach(org.junit.jupiter.api.AfterEach) ExchangeFilterFunction(org.springframework.web.reactive.function.client.ExchangeFilterFunction) SpringTestContext(org.springframework.security.config.test.SpringTestContext) SpringTestContextExtension(org.springframework.security.config.test.SpringTestContextExtension) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Authentication(org.springframework.security.core.Authentication) HashMap(java.util.HashMap) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockExchangeFunction(org.springframework.security.oauth2.client.web.reactive.function.client.MockExchangeFunction) ExchangeFilterFunction(org.springframework.web.reactive.function.client.ExchangeFilterFunction) Authentication(org.springframework.security.core.Authentication) HashMap(java.util.HashMap) Map(java.util.Map) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) Test(org.junit.jupiter.api.Test)

Example 34 with ClientRequest

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

the class WiretapConnectorTests method captureAndClaim.

@Test
public void captureAndClaim() {
    ClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, "/test");
    ClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
    ClientHttpConnector connector = (method, uri, fn) -> fn.apply(request).then(Mono.just(response));
    ClientRequest clientRequest = ClientRequest.create(HttpMethod.GET, URI.create("/test")).header(WebTestClient.WEBTESTCLIENT_REQUEST_ID, "1").build();
    WiretapConnector wiretapConnector = new WiretapConnector(connector);
    ExchangeFunction function = ExchangeFunctions.create(wiretapConnector);
    function.exchange(clientRequest).block(ofMillis(0));
    ExchangeResult result = wiretapConnector.getExchangeResult("1", null, Duration.ZERO);
    assertThat(result.getMethod()).isEqualTo(HttpMethod.GET);
    assertThat(result.getUrl().toString()).isEqualTo("/test");
}
Also used : MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpMethod(org.springframework.http.HttpMethod) Mono(reactor.core.publisher.Mono) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) Duration(java.time.Duration) ExchangeFunctions(org.springframework.web.reactive.function.client.ExchangeFunctions) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) URI(java.net.URI) Duration.ofMillis(java.time.Duration.ofMillis) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) ExchangeFunction(org.springframework.web.reactive.function.client.ExchangeFunction) Test(org.junit.jupiter.api.Test)

Example 35 with ClientRequest

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

the class ServerOAuth2AuthorizedClientExchangeFilterFunctionTests method filterWhenDefaultOAuth2AuthorizedClientFalseThenEmpty.

@Test
public void filterWhenDefaultOAuth2AuthorizedClientFalseThenEmpty() {
    ClientRequest request = ClientRequest.create(HttpMethod.GET, URI.create("https://example.com")).build();
    OAuth2User user = new DefaultOAuth2User(AuthorityUtils.createAuthorityList("ROLE_USER"), Collections.singletonMap("user", "rob"), "user");
    OAuth2AuthenticationToken authentication = new OAuth2AuthenticationToken(user, user.getAuthorities(), "client-id");
    // @formatter:off
    this.function.filter(request, this.exchange).subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication)).block();
    // @formatter:on
    List<ClientRequest> requests = this.exchange.getRequests();
    assertThat(requests).hasSize(1);
    verifyZeroInteractions(this.clientRegistrationRepository, this.authorizedClientRepository);
}
Also used : DefaultOAuth2User(org.springframework.security.oauth2.core.user.DefaultOAuth2User) OAuth2User(org.springframework.security.oauth2.core.user.OAuth2User) OAuth2AuthenticationToken(org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken) DefaultOAuth2User(org.springframework.security.oauth2.core.user.DefaultOAuth2User) ClientRequest(org.springframework.web.reactive.function.client.ClientRequest) 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