Search in sources :

Example 41 with MockServerHttpRequest

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

the class DefaultErrorAttributesTests method annotatedResponseStatusCodeWithCustomReasonPhrase.

@Test
void annotatedResponseStatusCodeWithCustomReasonPhrase() {
    Exception error = new Custom2Exception();
    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("status")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.value());
    assertThat(attributes.get("message")).isEqualTo("Nope!");
}
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 42 with MockServerHttpRequest

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

the class DefaultErrorAttributesTests method includeTimeStamp.

@Test
void includeTimeStamp() {
    MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
    Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, NOT_FOUND), ErrorAttributeOptions.defaults());
    assertThat(attributes.get("timestamp")).isInstanceOf(Date.class);
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 43 with MockServerHttpRequest

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

the class DefaultErrorAttributesTests method extractBindingResultErrorsExcludeMessageAndErrors.

@Test
void extractBindingResultErrorsExcludeMessageAndErrors() throws Exception {
    Method method = getClass().getDeclaredMethod("method", String.class);
    MethodParameter stringParam = new MethodParameter(method, 0);
    BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
    bindingResult.addError(new ObjectError("c", "d"));
    Exception ex = new WebExchangeBindException(stringParam, bindingResult);
    MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
    Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, ex), ErrorAttributeOptions.defaults());
    assertThat(attributes).doesNotContainKey("message");
    assertThat(attributes).doesNotContainKey("errors");
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) ObjectError(org.springframework.validation.ObjectError) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MapBindingResult(org.springframework.validation.MapBindingResult) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) 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 44 with MockServerHttpRequest

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

the class DefaultErrorAttributesTests method extractBindingResultErrors.

@Test
void extractBindingResultErrors() throws Exception {
    Method method = getClass().getDeclaredMethod("method", String.class);
    MethodParameter stringParam = new MethodParameter(method, 0);
    BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
    bindingResult.addError(new ObjectError("c", "d"));
    Exception ex = new WebExchangeBindException(stringParam, bindingResult);
    MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
    Map<String, Object> attributes = this.errorAttributes.getErrorAttributes(buildServerRequest(request, ex), ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS));
    assertThat(attributes.get("message")).asString().startsWith("Validation failed for argument at index 0 in method: " + "int org.springframework.boot.web.reactive.error.DefaultErrorAttributesTests" + ".method(java.lang.String), with 1 error(s)");
    assertThat(attributes.get("errors")).isEqualTo(bindingResult.getAllErrors());
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) ObjectError(org.springframework.validation.ObjectError) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MapBindingResult(org.springframework.validation.MapBindingResult) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) 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 45 with MockServerHttpRequest

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

the class DefaultErrorAttributesTests method includeException.

@Test
void includeException() {
    RuntimeException error = new RuntimeException("Test");
    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(this.errorAttributes.getError(serverRequest)).isSameAs(error);
    assertThat(serverRequest.attribute(ErrorAttributes.ERROR_ATTRIBUTE)).containsSame(error);
    assertThat(attributes.get("exception")).isEqualTo(RuntimeException.class.getName());
    assertThat(attributes.get("message")).isEqualTo("Test");
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) ServerRequest(org.springframework.web.reactive.function.server.ServerRequest) Test(org.junit.jupiter.api.Test)

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