Search in sources :

Example 31 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-boot by spring-projects.

the class DefaultErrorAttributesTests method annotatedResponseStatusCodeWithExceptionMessage.

@Test
void annotatedResponseStatusCodeWithExceptionMessage() {
    Exception error = new CustomException("Test Message");
    MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
    Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, error), ErrorAttributeOptions.of(Include.MESSAGE));
    assertThat(attributes.get("error")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.getReasonPhrase());
    assertThat(attributes.get("message")).isEqualTo("Test Message");
    assertThat(attributes.get("status")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.value());
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) Test(org.junit.jupiter.api.Test)

Example 32 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-boot by spring-projects.

the class DefaultErrorAttributesTests method notIncludeTrace.

@Test
void notIncludeTrace() {
    RuntimeException ex = new RuntimeException("Test");
    MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
    Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, ex), ErrorAttributeOptions.defaults());
    assertThat(attributes.get("trace")).isNull();
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 33 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-boot by spring-projects.

the class DefaultErrorAttributesTests method excludeMessage.

@Test
void excludeMessage() {
    Error error = new OutOfMemoryError("Test error");
    MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
    ServerRequest serverRequest = buildServerRequest(request, error);
    Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(serverRequest, ErrorAttributeOptions.defaults());
    assertThat(this.errorAttributes.getError(serverRequest)).isSameAs(error);
    assertThat(attributes).doesNotContainKey("message");
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) ObjectError(org.springframework.validation.ObjectError) ServerRequest(org.springframework.web.reactive.function.server.ServerRequest) Test(org.junit.jupiter.api.Test)

Example 34 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-boot by spring-projects.

the class DefaultErrorAttributesTests method processResponseStatusException.

@Test
void processResponseStatusException() {
    RuntimeException nested = new RuntimeException("Test");
    ResponseStatusException error = new ResponseStatusException(HttpStatus.BAD_REQUEST, "invalid request", nested);
    this.errorAttributes = new DefaultErrorAttributes();
    MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
    ServerRequest serverRequest = buildServerRequest(request, error);
    Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(serverRequest, ErrorAttributeOptions.of(Include.EXCEPTION, Include.MESSAGE));
    assertThat(attributes.get("status")).isEqualTo(400);
    assertThat(attributes.get("message")).isEqualTo("invalid request");
    assertThat(attributes.get("exception")).isEqualTo(RuntimeException.class.getName());
    assertThat(this.errorAttributes.getError(serverRequest)).isSameAs(error);
    assertThat(serverRequest.attribute(ErrorAttributes.ERROR_ATTRIBUTE)).containsSame(error);
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) ServerRequest(org.springframework.web.reactive.function.server.ServerRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) Test(org.junit.jupiter.api.Test)

Example 35 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-boot by spring-projects.

the class WebFluxAutoConfigurationTests method whenAcceptHeaderLocaleContextResolverIsUsedThenAcceptLanguagesHeaderIsHonoured.

@Test
void whenAcceptHeaderLocaleContextResolverIsUsedThenAcceptLanguagesHeaderIsHonoured() {
    this.contextRunner.withPropertyValues("spring.web.locale:en_UK").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(AcceptHeaderLocaleContextResolver.class);
        LocaleContext localeContext = localeContextResolver.resolveLocaleContext(exchange);
        assertThat(localeContext.getLocale()).isEqualTo(StringUtils.parseLocaleString("nl_NL"));
    });
}
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

MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)75 Test (org.junit.jupiter.api.Test)40 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)35 Test (org.junit.Test)26 ServerWebExchange (org.springframework.web.server.ServerWebExchange)26 URI (java.net.URI)16 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)11 HttpHeaders (org.springframework.http.HttpHeaders)9 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ResponseStatusException (org.springframework.web.server.ResponseStatusException)7 AcceptHeaderLocaleContextResolver (org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver)6 Mono (reactor.core.publisher.Mono)6 InetSocketAddress (java.net.InetSocketAddress)5 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)5 Mockito.mock (org.mockito.Mockito.mock)5 ObjectError (org.springframework.validation.ObjectError)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)4 BDDMockito.given (org.mockito.BDDMockito.given)4