Search in sources :

Example 36 with MockServerWebExchange

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

the class ThymeleafReactiveAutoConfigurationTests method useSecurityDialect.

@Test
void useSecurityDialect() {
    this.contextRunner.run((context) -> {
        ISpringWebFluxTemplateEngine engine = context.getBean(ISpringWebFluxTemplateEngine.class);
        MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/test").build());
        exchange.getAttributes().put(SpringSecurityContextUtils.SECURITY_CONTEXT_MODEL_ATTRIBUTE_NAME, new SecurityContextImpl(new TestingAuthenticationToken("alice", "admin")));
        WebContext attrs = new WebContext(SpringWebFluxWebApplication.buildApplication(null).buildExchange(exchange, Locale.US, MediaType.TEXT_HTML, StandardCharsets.UTF_8));
        String result = engine.process("security-dialect", attrs);
        assertThat(result).isEqualTo("<html><body><div>alice</div></body></html>" + System.lineSeparator());
    });
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) WebContext(org.thymeleaf.context.WebContext) ISpringWebFluxTemplateEngine(org.thymeleaf.spring6.ISpringWebFluxTemplateEngine) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.jupiter.api.Test)

Example 37 with MockServerWebExchange

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

the class FreeMarkerAutoConfigurationReactiveIntegrationTests method customSuffix.

@Test
void customSuffix() {
    this.contextRunner.withPropertyValues("spring.freemarker.suffix:.freemarker").run((context) -> {
        MockServerWebExchange exchange = render(context, "suffixed");
        String result = exchange.getResponse().getBodyAsString().block(Duration.ofSeconds(30));
        assertThat(result).contains("suffixed");
    });
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 38 with MockServerWebExchange

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

the class MetricsWebFilterTests method filterAddsTagsToRegistryForHandledExceptions.

@Test
void filterAddsTagsToRegistryForHandledExceptions() {
    MockServerWebExchange exchange = createExchange("/projects/spring-boot", "/projects/{project}");
    this.webFilter.filter(exchange, (serverWebExchange) -> {
        exchange.getAttributes().put(ErrorAttributes.ERROR_ATTRIBUTE, new IllegalStateException("test error"));
        return exchange.getResponse().setComplete();
    }).block(Duration.ofSeconds(30));
    assertMetricsContainsTag("uri", "/projects/{project}");
    assertMetricsContainsTag("status", "200");
    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 39 with MockServerWebExchange

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

the class MetricsWebFilterTests method filterAddsStandardTags.

@Test
void filterAddsStandardTags() {
    MockServerWebExchange exchange = createTimedHandlerMethodExchange("timed");
    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 40 with MockServerWebExchange

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

the class MetricsWebFilterTests method whenMetricsRecordingFailsThenExchangeFilteringSucceeds.

@Test
void whenMetricsRecordingFailsThenExchangeFilteringSucceeds() {
    MockServerWebExchange exchange = createExchange("/projects/spring-boot", "/projects/{project}");
    this.tagsProvider.failOnce();
    this.webFilter.filter(exchange, (serverWebExchange) -> exchange.getResponse().setComplete()).block(Duration.ofSeconds(30));
}
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