Search in sources :

Example 46 with MockServerWebExchange

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

the class FreeMarkerAutoConfigurationReactiveIntegrationTests method render.

private MockServerWebExchange render(ApplicationContext context, String viewName) {
    FreeMarkerViewResolver resolver = context.getBean(FreeMarkerViewResolver.class);
    Mono<View> view = resolver.resolveViewName(viewName, Locale.UK);
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path"));
    view.flatMap((v) -> v.render(null, MediaType.TEXT_HTML, exchange)).block(Duration.ofSeconds(30));
    return exchange;
}
Also used : AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) FreeMarkerConfig(org.springframework.web.reactive.result.view.freemarker.FreeMarkerConfig) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) StringWriter(java.io.StringWriter) MediaType(org.springframework.http.MediaType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) ApplicationContext(org.springframework.context.ApplicationContext) Test(org.junit.jupiter.api.Test) ReactiveWebApplicationContextRunner(org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner) FreeMarkerConfigurer(org.springframework.web.reactive.result.view.freemarker.FreeMarkerConfigurer) FreeMarkerViewResolver(org.springframework.web.reactive.result.view.freemarker.FreeMarkerViewResolver) Locale(java.util.Locale) Duration(java.time.Duration) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) View(org.springframework.web.reactive.result.view.View) FreeMarkerViewResolver(org.springframework.web.reactive.result.view.freemarker.FreeMarkerViewResolver) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) View(org.springframework.web.reactive.result.view.View)

Example 47 with MockServerWebExchange

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

the class MetricsWebFilterTests method filterAddsPercentileMeters.

@Test
void filterAddsPercentileMeters() {
    MockServerWebExchange exchange = createTimedHandlerMethodExchange("timedPercentiles");
    this.webFilter.filter(exchange, (serverWebExchange) -> exchange.getResponse().setComplete()).block(Duration.ofSeconds(30));
    assertMetricsContainsTag("uri", "/projects/{project}");
    assertMetricsContainsTag("status", "200");
    assertThat(this.registry.get(REQUEST_METRICS_NAME_PERCENTILE).tag("phi", "0.95").gauge().value()).isNotZero();
    assertThat(this.registry.get(REQUEST_METRICS_NAME_PERCENTILE).tag("phi", "0.5").gauge().value()).isNotZero();
}
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 48 with MockServerWebExchange

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

the class MetricsWebFilterTests method disconnectedExceptionShouldProduceMetrics.

@Test
void disconnectedExceptionShouldProduceMetrics() {
    MockServerWebExchange exchange = createExchange("/projects/spring-boot", "/projects/{project}");
    Mono<Void> processing = this.webFilter.filter(exchange, (serverWebExchange) -> Mono.error(new EOFException("Disconnected"))).onErrorResume((t) -> {
        exchange.getResponse().setRawStatusCode(500);
        return exchange.getResponse().setComplete();
    });
    StepVerifier.create(processing).expectComplete().verify(Duration.ofSeconds(5));
    assertMetricsContainsTag("uri", "/projects/{project}");
    assertMetricsContainsTag("status", "500");
    assertMetricsContainsTag("outcome", "UNKNOWN");
}
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) EOFException(java.io.EOFException) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 49 with MockServerWebExchange

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

the class MetricsWebFilterTests method trailingSlashShouldNotRecordDuplicateMetrics.

@Test
void trailingSlashShouldNotRecordDuplicateMetrics() {
    MockServerWebExchange exchange1 = createExchange("/projects/spring-boot", "/projects/{project}");
    MockServerWebExchange exchange2 = createExchange("/projects/spring-boot", "/projects/{project}/");
    this.webFilter.filter(exchange1, (serverWebExchange) -> exchange1.getResponse().setComplete()).block(Duration.ofSeconds(30));
    this.webFilter.filter(exchange2, (serverWebExchange) -> exchange2.getResponse().setComplete()).block(Duration.ofSeconds(30));
    assertThat(this.registry.get(REQUEST_METRICS_NAME).tag("uri", "/projects/{project}").timer().count()).isEqualTo(2);
    assertThat(this.registry.get(REQUEST_METRICS_NAME).tag("status", "200").timer().count()).isEqualTo(2);
}
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 50 with MockServerWebExchange

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

the class MetricsWebFilterTests method filterAddsExtraTags.

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

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