Search in sources :

Example 51 with MockServerWebExchange

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

the class MetricsWebFilterTests method createExchange.

private MockServerWebExchange createExchange(String path, String pathPattern) {
    PathPatternParser parser = new PathPatternParser();
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(path).build());
    exchange.getAttributes().put(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, parser.parse(pathPattern));
    return exchange;
}
Also used : PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange)

Example 52 with MockServerWebExchange

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

the class MetricsWebFilterTests method filterAddsTagsToRegistry.

@Test
void filterAddsTagsToRegistry() {
    MockServerWebExchange exchange = createExchange("/projects/spring-boot", "/projects/{project}");
    this.webFilter.filter(exchange, (serverWebExchange) -> exchange.getResponse().setComplete()).block(Duration.ofSeconds(30));
    assertMetricsContainsTag("uri", "/projects/{project}");
    assertMetricsContainsTag("status", "200");
}
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 53 with MockServerWebExchange

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

the class MetricsWebFilterTests method filterAddsTagsToRegistryForExceptions.

@Test
void filterAddsTagsToRegistryForExceptions() {
    MockServerWebExchange exchange = createExchange("/projects/spring-boot", "/projects/{project}");
    this.webFilter.filter(exchange, (serverWebExchange) -> Mono.error(new IllegalStateException("test error"))).onErrorResume((t) -> {
        exchange.getResponse().setRawStatusCode(500);
        return exchange.getResponse().setComplete();
    }).block(Duration.ofSeconds(30));
    assertMetricsContainsTag("uri", "/projects/{project}");
    assertMetricsContainsTag("status", "500");
    assertMetricsContainsTag("exception", "IllegalStateException");
}
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 54 with MockServerWebExchange

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

the class MetricsWebFilterTests method filterAddsNonEmptyTagsToRegistryForAnonymousExceptions.

@Test
void filterAddsNonEmptyTagsToRegistryForAnonymousExceptions() {
    final Exception anonymous = new Exception("test error") {
    };
    MockServerWebExchange exchange = createExchange("/projects/spring-boot", "/projects/{project}");
    this.webFilter.filter(exchange, (serverWebExchange) -> Mono.error(anonymous)).onErrorResume((t) -> {
        exchange.getResponse().setRawStatusCode(500);
        return exchange.getResponse().setComplete();
    }).block(Duration.ofSeconds(30));
    assertMetricsContainsTag("uri", "/projects/{project}");
    assertMetricsContainsTag("status", "500");
    assertMetricsContainsTag("exception", anonymous.getClass().getName());
}
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) EOFException(java.io.EOFException) Test(org.junit.jupiter.api.Test)

Example 55 with MockServerWebExchange

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

the class MetricsWebFilterTests method cancelledConnectionsShouldProduceMetrics.

@Test
void cancelledConnectionsShouldProduceMetrics() {
    MockServerWebExchange exchange = createExchange("/projects/spring-boot", "/projects/{project}");
    Mono<Void> processing = this.webFilter.filter(exchange, (serverWebExchange) -> exchange.getResponse().setComplete());
    StepVerifier.create(processing).thenCancel().verify(Duration.ofSeconds(5));
    assertMetricsContainsTag("uri", "/projects/{project}");
    assertMetricsContainsTag("status", "200");
    assertMetricsContainsTag("outcome", "UNKNOWN");
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) 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