Search in sources :

Example 61 with MockServerHttpRequest

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

the class StripPrefixGatewayFilterFactoryTests method testStripPrefixFilter.

private void testStripPrefixFilter(String actualPath, String expectedPath, int parts) {
    GatewayFilter filter = new StripPrefixGatewayFilterFactory().apply(c -> c.setParts(parts));
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost" + actualPath).build();
    ServerWebExchange exchange = MockServerWebExchange.from(request);
    GatewayFilterChain filterChain = mock(GatewayFilterChain.class);
    ArgumentCaptor<ServerWebExchange> captor = ArgumentCaptor.forClass(ServerWebExchange.class);
    when(filterChain.filter(captor.capture())).thenReturn(Mono.empty());
    filter.filter(exchange, filterChain);
    ServerWebExchange webExchange = captor.getValue();
    assertThat(webExchange.getRequest().getURI()).hasPath(expectedPath);
    URI requestUrl = webExchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
    assertThat(requestUrl).hasScheme("http").hasHost("localhost").hasNoPort().hasPath(expectedPath);
    LinkedHashSet<URI> uris = webExchange.getRequiredAttribute(GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
    assertThat(uris).contains(request.getURI());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) GatewayFilterChain(org.springframework.cloud.gateway.filter.GatewayFilterChain) URI(java.net.URI) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter)

Example 62 with MockServerHttpRequest

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

the class HttpHeadersFilterMixedTypeTests method relevantDownstreamFiltersShouldActOnHeaders.

@Test
public void relevantDownstreamFiltersShouldActOnHeaders() {
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("/get").header("header1", "value1").header("header2", "value2").header("header3", "value3").build();
    HttpHeadersFilter filter1 = filterRemovingHeaders(Type.RESPONSE, "header1");
    HttpHeadersFilter filter2 = filterRemovingHeaders(Type.REQUEST, "header2");
    HttpHeaders result = HttpHeadersFilter.filterRequest(Arrays.asList(filter1, filter2), MockServerWebExchange.from(mockRequest));
    assertThat(result).containsOnlyKeys("header1", "header3");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Test(org.junit.Test)

Example 63 with MockServerHttpRequest

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

the class HttpHeadersFilterTests method httpHeadersFilterTests.

@Test
public void httpHeadersFilterTests() {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost:8080/get").header("X-A", "aValue").header("X-B", "bValue").header("X-C", "cValue").build();
    List<HttpHeadersFilter> filters = Arrays.asList((h, e) -> HttpHeadersFilterTests.this.filter(h, "X-A"), (h, e) -> HttpHeadersFilterTests.this.filter(h, "X-B"));
    HttpHeaders headers = HttpHeadersFilter.filterRequest(filters, MockServerWebExchange.from(request));
    assertThat(headers).containsOnlyKeys("X-C");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Test(org.junit.Test)

Example 64 with MockServerHttpRequest

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

the class OAuth2AuthorizationCodeGrantWebFilterTests method filterWhenAuthorizationRequestRedirectUriParametersNotMatchThenNotProcessed.

// gh-7966
@Test
public void filterWhenAuthorizationRequestRedirectUriParametersNotMatchThenNotProcessed() {
    String requestUri = "/authorization/callback";
    Map<String, String> parameters = new LinkedHashMap<>();
    parameters.put("param1", "value1");
    parameters.put("param2", "value2");
    MockServerHttpRequest authorizationRequest = createAuthorizationRequest(requestUri, parameters);
    ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration().build();
    OAuth2AuthorizationRequest oauth2AuthorizationRequest = createOAuth2AuthorizationRequest(authorizationRequest, clientRegistration);
    given(this.authorizationRequestRepository.loadAuthorizationRequest(any())).willReturn(Mono.just(oauth2AuthorizationRequest));
    // 1) Parameter value
    Map<String, String> parametersNotMatch = new LinkedHashMap<>(parameters);
    parametersNotMatch.put("param2", "value8");
    MockServerHttpRequest authorizationResponse = createAuthorizationResponse(createAuthorizationRequest(requestUri, parametersNotMatch));
    MockServerWebExchange exchange = MockServerWebExchange.from(authorizationResponse);
    DefaultWebFilterChain chain = new DefaultWebFilterChain((e) -> e.getResponse().setComplete(), Collections.emptyList());
    this.filter.filter(exchange, chain).block();
    verifyNoInteractions(this.authenticationManager);
    // 2) Parameter order
    parametersNotMatch = new LinkedHashMap<>();
    parametersNotMatch.put("param2", "value2");
    parametersNotMatch.put("param1", "value1");
    authorizationResponse = createAuthorizationResponse(createAuthorizationRequest(requestUri, parametersNotMatch));
    exchange = MockServerWebExchange.from(authorizationResponse);
    this.filter.filter(exchange, chain).block();
    verifyNoInteractions(this.authenticationManager);
    // 3) Parameter missing
    parametersNotMatch = new LinkedHashMap<>(parameters);
    parametersNotMatch.remove("param2");
    authorizationResponse = createAuthorizationResponse(createAuthorizationRequest(requestUri, parametersNotMatch));
    exchange = MockServerWebExchange.from(authorizationResponse);
    this.filter.filter(exchange, chain).block();
    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) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 65 with MockServerHttpRequest

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

the class OAuth2AuthorizationCodeGrantWebFilterTests method filterWhenAuthorizationSucceedsAndRequestCacheConfiguredThenRequestCacheUsed.

@Test
public void filterWhenAuthorizationSucceedsAndRequestCacheConfiguredThenRequestCacheUsed() {
    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()));
    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());
    ServerRequestCache requestCache = mock(ServerRequestCache.class);
    given(requestCache.getRedirectUri(any(ServerWebExchange.class))).willReturn(Mono.just(URI.create("/saved-request")));
    this.filter.setRequestCache(requestCache);
    this.filter.filter(exchange, chain).block();
    verify(requestCache).getRedirectUri(exchange);
    assertThat(exchange.getResponse().getHeaders().getLocation().toString()).isEqualTo("/saved-request");
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) 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) ServerRequestCache(org.springframework.security.web.server.savedrequest.ServerRequestCache) 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