|
35 | 35 | import org.springframework.validation.BindingResult;
|
36 | 36 | import org.springframework.validation.MapBindingResult;
|
37 | 37 | import org.springframework.validation.ObjectError;
|
| 38 | +import org.springframework.web.bind.annotation.ResponseStatus; |
38 | 39 | import org.springframework.web.bind.support.WebExchangeBindException;
|
39 | 40 | import org.springframework.web.reactive.function.server.ServerRequest;
|
40 | 41 | import org.springframework.web.server.ResponseStatusException;
|
@@ -90,6 +91,29 @@ public void defaultStatusCode() {
|
90 | 91 | assertThat(attributes.get("status")).isEqualTo(500);
|
91 | 92 | }
|
92 | 93 |
|
| 94 | + @Test |
| 95 | + public void annotatedResponseStatusCode() { |
| 96 | + Exception error = new CustomException(); |
| 97 | + MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); |
| 98 | + Map<String, Object> attributes = this.errorAttributes |
| 99 | + .getErrorAttributes(buildServerRequest(request, error), false); |
| 100 | + assertThat(attributes.get("error")) |
| 101 | + .isEqualTo(HttpStatus.I_AM_A_TEAPOT.getReasonPhrase()); |
| 102 | + assertThat(attributes.get("status")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.value()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void annotatedResponseStatusCodeWithCustomReasonPhrase() { |
| 107 | + Exception error = new Custom2Exception(); |
| 108 | + MockServerHttpRequest request = MockServerHttpRequest.get("/test").build(); |
| 109 | + Map<String, Object> attributes = this.errorAttributes |
| 110 | + .getErrorAttributes(buildServerRequest(request, error), false); |
| 111 | + assertThat(attributes.get("error")) |
| 112 | + .isEqualTo(HttpStatus.I_AM_A_TEAPOT.getReasonPhrase()); |
| 113 | + assertThat(attributes.get("status")).isEqualTo(HttpStatus.I_AM_A_TEAPOT.value()); |
| 114 | + assertThat(attributes.get("message")).isEqualTo("Nope!"); |
| 115 | + } |
| 116 | + |
93 | 117 | @Test
|
94 | 118 | public void includeStatusCode() {
|
95 | 119 | MockServerHttpRequest request = MockServerHttpRequest.get("/test").build();
|
@@ -214,4 +238,14 @@ public int method(String firstParam) {
|
214 | 238 | return 42;
|
215 | 239 | }
|
216 | 240 |
|
| 241 | + @ResponseStatus(HttpStatus.I_AM_A_TEAPOT) |
| 242 | + private static class CustomException extends RuntimeException { |
| 243 | + |
| 244 | + } |
| 245 | + |
| 246 | + @ResponseStatus(value = HttpStatus.I_AM_A_TEAPOT, reason = "Nope!") |
| 247 | + private static class Custom2Exception extends RuntimeException { |
| 248 | + |
| 249 | + } |
| 250 | + |
217 | 251 | }
|
0 commit comments