Search in sources :

Example 41 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-boot by spring-projects.

the class MetricsWebFilterTests method createTimedHandlerMethodExchange.

private MockServerWebExchange createTimedHandlerMethodExchange(String methodName) {
    MockServerWebExchange exchange = createExchange("/projects/spring-boot", "/projects/{project}");
    exchange.getAttributes().put(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE, new HandlerMethod(this, ReflectionUtils.findMethod(Handlers.class, methodName)));
    return exchange;
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 42 with MockServerWebExchange

use of org.springframework.mock.web.server.MockServerWebExchange in project spring-boot by spring-projects.

the class MetricsWebFilterTests method filterAddsExtraTagsAndException.

@Test
void filterAddsExtraTagsAndException() {
    MockServerWebExchange exchange = createTimedHandlerMethodExchange("timedExtraTags");
    this.webFilter.filter(exchange, (serverWebExchange) -> Mono.error(new IllegalStateException("test error"))).onErrorResume((ex) -> {
        exchange.getResponse().setRawStatusCode(500);
        return exchange.getResponse().setComplete();
    }).block(Duration.ofSeconds(30));
    assertMetricsContainsTag("uri", "/projects/{project}");
    assertMetricsContainsTag("status", "500");
    assertMetricsContainsTag("exception", "IllegalStateException");
    assertMetricsContainsTag("tag1", "value1");
    assertMetricsContainsTag("tag2", "value2");
}
Also used : Tag(io.micrometer.core.instrument.Tag) BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) StepVerifier(reactor.test.StepVerifier) AutoTimer(org.springframework.boot.actuate.metrics.AutoTimer) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockClock(io.micrometer.core.instrument.MockClock) Timed(io.micrometer.core.annotation.Timed) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SimpleConfig(io.micrometer.core.instrument.simple.SimpleConfig) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) Mono(reactor.core.publisher.Mono) EOFException(java.io.EOFException) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) HandlerMethod(org.springframework.web.method.HandlerMethod) ReflectionUtils(org.springframework.util.ReflectionUtils) Duration(java.time.Duration) ErrorAttributes(org.springframework.boot.web.reactive.error.ErrorAttributes) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) HandlerMapping(org.springframework.web.reactive.HandlerMapping) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 43 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 44 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 45 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)

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