Search in sources :

Example 66 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-cloud-gateway by spring-cloud.

the class WeightCalculatorWebFilterTests method testChooseRouteWithRandom.

@Test
public void testChooseRouteWithRandom() {
    WeightCalculatorWebFilter filter = new WeightCalculatorWebFilter();
    filter.addWeightConfig(new WeightConfig("groupa", "route1", 1));
    filter.addWeightConfig(new WeightConfig("groupa", "route2", 3));
    filter.addWeightConfig(new WeightConfig("groupa", "route3", 6));
    Random random = mock(Random.class);
    when(random.nextDouble()).thenReturn(0.05).thenReturn(0.2).thenReturn(0.6);
    filter.setRandom(random);
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost").build());
    WebFilterChain filterChain = mock(WebFilterChain.class);
    filter.filter(exchange, filterChain);
    Map<String, String> weights = WeightCalculatorWebFilter.getWeights(exchange);
    assertThat(weights).containsEntry("groupa", "route1");
    filter.filter(exchange, filterChain);
    weights = WeightCalculatorWebFilter.getWeights(exchange);
    assertThat(weights).containsEntry("groupa", "route2");
    filter.filter(exchange, filterChain);
    weights = WeightCalculatorWebFilter.getWeights(exchange);
    assertThat(weights).containsEntry("groupa", "route3");
}
Also used : Random(java.util.Random) WebFilterChain(org.springframework.web.server.WebFilterChain) GroupWeightConfig(org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter.GroupWeightConfig) WeightConfig(org.springframework.cloud.gateway.support.WeightConfig) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.Test)

Example 67 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-cloud-gateway by spring-cloud.

the class CloudFoundryRouteServicePredicateFactoryTest method itReturnsTrueWithAllHeadersPresent.

@Test
public void itReturnsTrueWithAllHeadersPresent() {
    MockServerHttpRequest request = MockServerHttpRequest.get("someurl").header(CloudFoundryRouteServicePredicateFactory.X_CF_FORWARDED_URL, "url").header(CloudFoundryRouteServicePredicateFactory.X_CF_PROXY_METADATA, "metadata").header(CloudFoundryRouteServicePredicateFactory.X_CF_PROXY_SIGNATURE, "signature").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    assertThat(predicate.test(exchange)).isTrue();
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.Test)

Example 68 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-cloud-gateway by spring-cloud.

the class CloudFoundryRouteServicePredicateFactoryTest method itReturnsFalseWithAHeadersMissing.

@Test
public void itReturnsFalseWithAHeadersMissing() {
    MockServerHttpRequest request = MockServerHttpRequest.get("someurl").header(CloudFoundryRouteServicePredicateFactory.X_CF_FORWARDED_URL, "url").header(CloudFoundryRouteServicePredicateFactory.X_CF_PROXY_METADATA, "metadata").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    assertThat(predicate.test(exchange)).isFalse();
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.Test)

Example 69 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-cloud-gateway by spring-cloud.

the class RequestRateLimiterGatewayFilterFactoryTests method assertFilterFactory.

private void assertFilterFactory(KeyResolver keyResolver, String key, boolean allowed, HttpStatus expectedStatus) {
    Tuple args = tuple().build();
    when(rateLimiter.isAllowed("myroute", key)).thenReturn(Mono.just(new Response(allowed, 1)));
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    exchange.getResponse().setStatusCode(HttpStatus.OK);
    exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR, Route.builder().id("myroute").predicate(ex -> true).uri("http://localhost").build());
    when(this.filterChain.filter(exchange)).thenReturn(Mono.empty());
    RequestRateLimiterGatewayFilterFactory factory = this.context.getBean(RequestRateLimiterGatewayFilterFactory.class);
    GatewayFilter filter = factory.apply(config -> config.setKeyResolver(keyResolver));
    Mono<Void> response = filter.filter(exchange, this.filterChain);
    response.subscribe(aVoid -> assertThat(exchange.getResponse().getStatusCode()).isEqualTo(expectedStatus));
}
Also used : Response(org.springframework.cloud.gateway.filter.ratelimit.RateLimiter.Response) DirtiesContext(org.springframework.test.annotation.DirtiesContext) TupleBuilder.tuple(org.springframework.tuple.TupleBuilder.tuple) GatewayFilterChain(org.springframework.cloud.gateway.filter.GatewayFilterChain) ServerWebExchangeUtils(org.springframework.cloud.gateway.support.ServerWebExchangeUtils) Response(org.springframework.cloud.gateway.filter.ratelimit.RateLimiter.Response) RateLimiter(org.springframework.cloud.gateway.filter.ratelimit.RateLimiter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) BaseWebClientTests(org.springframework.cloud.gateway.test.BaseWebClientTests) Autowired(org.springframework.beans.factory.annotation.Autowired) Qualifier(org.springframework.beans.factory.annotation.Qualifier) KeyResolver(org.springframework.cloud.gateway.filter.ratelimit.KeyResolver) SpringRunner(org.springframework.test.context.junit4.SpringRunner) RANDOM_PORT(org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT) MockBean(org.springframework.boot.test.mock.mockito.MockBean) EnableAutoConfiguration(org.springframework.boot.autoconfigure.EnableAutoConfiguration) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Import(org.springframework.context.annotation.Import) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.when(org.mockito.Mockito.when) ApplicationContext(org.springframework.context.ApplicationContext) HttpStatus(org.springframework.http.HttpStatus) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Tuple(org.springframework.tuple.Tuple) SpringBootConfiguration(org.springframework.boot.SpringBootConfiguration) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Bean(org.springframework.context.annotation.Bean) Route(org.springframework.cloud.gateway.route.Route) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Tuple(org.springframework.tuple.Tuple) GatewayFilter(org.springframework.cloud.gateway.filter.GatewayFilter)

Example 70 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange 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)

Aggregations

MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)94 Test (org.junit.jupiter.api.Test)81 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)44 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)27 Mono (reactor.core.publisher.Mono)26 BeforeEach (org.junit.jupiter.api.BeforeEach)22 StepVerifier (reactor.test.StepVerifier)21 ServerWebExchange (org.springframework.web.server.ServerWebExchange)15 Duration (java.time.Duration)14 ErrorAttributes (org.springframework.boot.web.reactive.error.ErrorAttributes)13 HandlerMethod (org.springframework.web.method.HandlerMethod)13 Timed (io.micrometer.core.annotation.Timed)12 MockClock (io.micrometer.core.instrument.MockClock)12 Tag (io.micrometer.core.instrument.Tag)12 SimpleConfig (io.micrometer.core.instrument.simple.SimpleConfig)12 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)12 EOFException (java.io.EOFException)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 AutoTimer (org.springframework.boot.actuate.metrics.AutoTimer)12 ResponseCookie (org.springframework.http.ResponseCookie)12