Search in sources :

Example 56 with MockServerWebExchange

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

the class MetricsWebFilterTests method filterAddsTagsToRegistryForExceptionsAndCommittedResponse.

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

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

the class DefaultErrorAttributesTests method missingExceptionAttribute.

@Test
void missingExceptionAttribute() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/test").build());
    ServerRequest request = ServerRequest.create(exchange, this.readers);
    assertThatIllegalStateException().isThrownBy(() -> this.errorAttributes.getErrorAttributes(request, ErrorAttributeOptions.defaults())).withMessageContaining("Missing exception attribute in ServerWebExchange");
}
Also used : MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) ServerRequest(org.springframework.web.reactive.function.server.ServerRequest) Test(org.junit.jupiter.api.Test)

Example 58 with MockServerWebExchange

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

the class MustacheViewTests method viewResolvesHandlebars.

@Test
void viewResolvesHandlebars() {
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/test").build());
    MustacheView view = new MustacheView();
    view.setCompiler(Mustache.compiler());
    view.setUrl(this.templateUrl);
    view.setCharset(StandardCharsets.UTF_8.displayName());
    view.setApplicationContext(this.context);
    view.render(Collections.singletonMap("World", "Spring"), MediaType.TEXT_HTML, exchange).block(Duration.ofSeconds(30));
    StepVerifier.create(exchange.getResponse().getBodyAsString()).assertNext((body) -> assertThat(body).isEqualToIgnoringWhitespace("Hello Spring")).verifyComplete();
}
Also used : Test(org.junit.jupiter.api.Test) StepVerifier(reactor.test.StepVerifier) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Duration(java.time.Duration) MediaType(org.springframework.http.MediaType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Collections(java.util.Collections) Mustache(com.samskivert.mustache.Mustache) StandardCharsets(java.nio.charset.StandardCharsets) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 59 with MockServerWebExchange

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

the class DefaultErrorWebExceptionHandlerTests method acceptsTextHtmlShouldNotConsiderMediaAllEvenWithQuality.

@Test
void acceptsTextHtmlShouldNotConsiderMediaAllEvenWithQuality() {
    ErrorAttributes errorAttributes = mock(ErrorAttributes.class);
    Resources resourceProperties = new Resources();
    ErrorProperties errorProperties = new ErrorProperties();
    ApplicationContext context = new AnnotationConfigReactiveWebApplicationContext();
    DefaultErrorWebExceptionHandler exceptionHandler = new DefaultErrorWebExceptionHandler(errorAttributes, resourceProperties, errorProperties, context);
    MediaType allWithQuality = new MediaType(MediaType.ALL.getType(), MediaType.ALL.getSubtype(), 0.9);
    MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/test").accept(allWithQuality));
    List<HttpMessageReader<?>> readers = ServerCodecConfigurer.create().getReaders();
    ServerRequest request = ServerRequest.create(exchange, readers);
    assertThat(exceptionHandler.acceptsTextHtml().test(request)).isFalse();
}
Also used : ErrorAttributes(org.springframework.boot.web.reactive.error.ErrorAttributes) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) AnnotationConfigReactiveWebApplicationContext(org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ErrorProperties(org.springframework.boot.autoconfigure.web.ErrorProperties) MediaType(org.springframework.http.MediaType) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Resources(org.springframework.boot.autoconfigure.web.WebProperties.Resources) ServerRequest(org.springframework.web.reactive.function.server.ServerRequest) AnnotationConfigReactiveWebApplicationContext(org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 60 with MockServerWebExchange

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

the class WebFluxAutoConfigurationTests method whenFixedLocalContextResolverIsUsedThenAcceptLanguagesHeaderIsIgnored.

@Test
void whenFixedLocalContextResolverIsUsedThenAcceptLanguagesHeaderIsIgnored() {
    this.contextRunner.withPropertyValues("spring.web.locale:en_UK", "spring.web.locale-resolver=fixed").run((context) -> {
        MockServerHttpRequest request = MockServerHttpRequest.get("/").acceptLanguageAsLocales(StringUtils.parseLocaleString("nl_NL")).build();
        MockServerWebExchange exchange = MockServerWebExchange.from(request);
        LocaleContextResolver localeContextResolver = context.getBean(LocaleContextResolver.class);
        assertThat(localeContextResolver).isInstanceOf(FixedLocaleContextResolver.class);
        LocaleContext localeContext = localeContextResolver.resolveLocaleContext(exchange);
        assertThat(localeContext.getLocale()).isEqualTo(StringUtils.parseLocaleString("en_UK"));
    });
}
Also used : LocaleContext(org.springframework.context.i18n.LocaleContext) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) FixedLocaleContextResolver(org.springframework.web.server.i18n.FixedLocaleContextResolver) LocaleContextResolver(org.springframework.web.server.i18n.LocaleContextResolver) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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