Search in sources :

Example 16 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-cloud-gateway by spring-cloud.

the class RouteToRequestUrlFilterTests method happyPathLb.

@Test
public void happyPathLb() {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost/getb").build();
    ServerWebExchange webExchange = testFilter(request, "lb:http://myhost");
    URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    assertThat(uri).hasScheme("http").hasHost("myhost");
    String schemePrefix = webExchange.getRequiredAttribute(GATEWAY_SCHEME_PREFIX_ATTR);
    assertThat(schemePrefix).isEqualTo("lb");
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Example 17 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-cloud-gateway by spring-cloud.

the class RouteToRequestUrlFilterTests method happyPath.

@Test
public void happyPath() {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost/get?a=b").build();
    ServerWebExchange webExchange = testFilter(request, "http://myhost");
    URI uri = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    assertThat(uri).hasScheme("http").hasHost("myhost").hasParameter("a", "b");
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) URI(java.net.URI) Test(org.junit.Test)

Example 18 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.

the class WebSessionOAuth2ServerAuthorizationRequestRepositoryTests method removeAuthorizationRequestWhenStateMissingThenNoErrors.

// gh-5599
@Test
public void removeAuthorizationRequestWhenStateMissingThenNoErrors() {
    // @formatter:off
    MockServerHttpRequest otherState = MockServerHttpRequest.get("/").queryParam(OAuth2ParameterNames.STATE, "other").build();
    ServerWebExchange otherStateExchange = this.exchange.mutate().request(otherState).build();
    Mono<OAuth2AuthorizationRequest> saveAndRemove = this.repository.saveAuthorizationRequest(this.authorizationRequest, this.exchange).then(this.repository.removeAuthorizationRequest(otherStateExchange));
    StepVerifier.create(saveAndRemove).verifyComplete();
// @formatter:on
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) Test(org.junit.jupiter.api.Test)

Example 19 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.

the class OAuth2AuthorizationCodeGrantWebFilterTests method filterWhenAuthorizationRequestRedirectUriParametersMatchThenProcessed.

// gh-7966
@Test
public void filterWhenAuthorizationRequestRedirectUriParametersMatchThenProcessed() {
    ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration().build();
    given(this.clientRegistrationRepository.findByRegistrationId(any())).willReturn(Mono.just(clientRegistration));
    given(this.authorizedClientRepository.saveAuthorizedClient(any(), any(), any())).willReturn(Mono.empty());
    given(this.authenticationManager.authenticate(any())).willReturn(Mono.just(TestOAuth2AuthorizationCodeAuthenticationTokens.authenticated()));
    // 1) redirect_uri with query parameters
    Map<String, String> parameters = new LinkedHashMap<>();
    parameters.put("param1", "value1");
    parameters.put("param2", "value2");
    MockServerHttpRequest authorizationRequest = createAuthorizationRequest("/authorization/callback", parameters);
    OAuth2AuthorizationRequest oauth2AuthorizationRequest = createOAuth2AuthorizationRequest(authorizationRequest, clientRegistration);
    given(this.authorizationRequestRepository.loadAuthorizationRequest(any())).willReturn(Mono.just(oauth2AuthorizationRequest));
    given(this.authorizationRequestRepository.removeAuthorizationRequest(any())).willReturn(Mono.just(oauth2AuthorizationRequest));
    MockServerHttpRequest authorizationResponse = createAuthorizationResponse(authorizationRequest);
    MockServerWebExchange exchange = MockServerWebExchange.from(authorizationResponse);
    DefaultWebFilterChain chain = new DefaultWebFilterChain((e) -> e.getResponse().setComplete(), Collections.emptyList());
    this.filter.filter(exchange, chain).block();
    verify(this.authenticationManager, times(1)).authenticate(any());
    // 2) redirect_uri with query parameters AND authorization response additional
    // parameters
    Map<String, String> additionalParameters = new LinkedHashMap<>();
    additionalParameters.put("auth-param1", "value1");
    additionalParameters.put("auth-param2", "value2");
    authorizationResponse = createAuthorizationResponse(authorizationRequest, additionalParameters);
    exchange = MockServerWebExchange.from(authorizationResponse);
    this.filter.filter(exchange, chain).block();
    verify(this.authenticationManager, times(2)).authenticate(any());
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) DefaultWebFilterChain(org.springframework.web.server.handler.DefaultWebFilterChain) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 20 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.

the class OAuth2AuthorizationCodeGrantWebFilterTests method filterWhenAuthenticationConverterThrowsOAuth2AuthorizationExceptionThenMappedToOAuth2AuthenticationException.

// gh-8609
@Test
public void filterWhenAuthenticationConverterThrowsOAuth2AuthorizationExceptionThenMappedToOAuth2AuthenticationException() {
    ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration().build();
    given(this.clientRegistrationRepository.findByRegistrationId(any())).willReturn(Mono.empty());
    MockServerHttpRequest authorizationRequest = createAuthorizationRequest("/authorization/callback");
    OAuth2AuthorizationRequest oauth2AuthorizationRequest = createOAuth2AuthorizationRequest(authorizationRequest, clientRegistration);
    given(this.authorizationRequestRepository.loadAuthorizationRequest(any())).willReturn(Mono.just(oauth2AuthorizationRequest));
    given(this.authorizationRequestRepository.removeAuthorizationRequest(any())).willReturn(Mono.just(oauth2AuthorizationRequest));
    MockServerHttpRequest authorizationResponse = createAuthorizationResponse(authorizationRequest);
    MockServerWebExchange exchange = MockServerWebExchange.from(authorizationResponse);
    DefaultWebFilterChain chain = new DefaultWebFilterChain((e) -> e.getResponse().setComplete(), Collections.emptyList());
    assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> this.filter.filter(exchange, chain).block()).satisfies((ex) -> assertThat(ex.getError()).extracting("errorCode").isEqualTo("client_registration_not_found"));
    verifyNoInteractions(this.authenticationManager);
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) DefaultWebFilterChain(org.springframework.web.server.handler.DefaultWebFilterChain) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)75 Test (org.junit.jupiter.api.Test)40 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)35 Test (org.junit.Test)26 ServerWebExchange (org.springframework.web.server.ServerWebExchange)26 URI (java.net.URI)16 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)11 HttpHeaders (org.springframework.http.HttpHeaders)9 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ResponseStatusException (org.springframework.web.server.ResponseStatusException)7 AcceptHeaderLocaleContextResolver (org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver)6 Mono (reactor.core.publisher.Mono)6 InetSocketAddress (java.net.InetSocketAddress)5 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)5 Mockito.mock (org.mockito.Mockito.mock)5 ObjectError (org.springframework.validation.ObjectError)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)4 BDDMockito.given (org.mockito.BDDMockito.given)4