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");
}
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");
}
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();
}
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();
}
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"));
});
}
Aggregations