-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
ErrorResponseExceptionTests.java
470 lines (343 loc) · 16.5 KB
/
ErrorResponseExceptionTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import org.junit.jupiter.api.Test;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.context.support.StaticMessageSource;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ProblemDetail;
import org.springframework.lang.Nullable;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingMatrixVariableException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingRequestCookieException;
import org.springframework.web.bind.MissingRequestHeaderException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
import org.springframework.web.bind.support.WebExchangeBindException;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.server.MethodNotAllowedException;
import org.springframework.web.server.MissingRequestValueException;
import org.springframework.web.server.NotAcceptableStatusException;
import org.springframework.web.server.ServerErrorException;
import org.springframework.web.server.UnsatisfiedRequestParameterException;
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
import org.springframework.web.testfixture.method.ResolvableMethod;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests that verify the HTTP response details exposed by exceptions in the
* {@link ErrorResponse} hierarchy.
*
* @author Rossen Stoyanchev
*/
public class ErrorResponseExceptionTests {
private final MethodParameter methodParameter =
new MethodParameter(ResolvableMethod.on(getClass()).resolveMethod("handle"), 0);
@Test
void httpMediaTypeNotSupportedException() {
List<MediaType> mediaTypes =
Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_CBOR);
HttpMediaTypeNotSupportedException ex = new HttpMediaTypeNotSupportedException(
MediaType.APPLICATION_XML, mediaTypes, HttpMethod.PATCH, "Custom message");
assertStatus(ex, HttpStatus.UNSUPPORTED_MEDIA_TYPE);
assertDetail(ex, "Content-Type 'application/xml' is not supported.");
assertDetailMessageCode(ex, null, new Object[] {ex.getContentType(), ex.getSupportedMediaTypes()});
HttpHeaders headers = ex.getHeaders();
assertThat(headers.getAccept()).isEqualTo(mediaTypes);
assertThat(headers.getAcceptPatch()).isEqualTo(mediaTypes);
}
@Test
void httpMediaTypeNotSupportedExceptionWithParseError() {
ErrorResponse ex = new HttpMediaTypeNotSupportedException(
"Could not parse Accept header: Invalid mime type \"foo\": does not contain '/'");
assertStatus(ex, HttpStatus.UNSUPPORTED_MEDIA_TYPE);
assertDetail(ex, "Could not parse Content-Type.");
assertDetailMessageCode(ex, "parseError", null);
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void httpMediaTypeNotAcceptableException() {
List<MediaType> mediaTypes = Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_CBOR);
HttpMediaTypeNotAcceptableException ex = new HttpMediaTypeNotAcceptableException(mediaTypes);
assertStatus(ex, HttpStatus.NOT_ACCEPTABLE);
assertDetail(ex, "Acceptable representations: [application/json, application/cbor].");
assertDetailMessageCode(ex, null, new Object[] {ex.getSupportedMediaTypes()});
assertThat(ex.getHeaders()).hasSize(1);
assertThat(ex.getHeaders().getAccept()).isEqualTo(mediaTypes);
}
@Test
void httpMediaTypeNotAcceptableExceptionWithParseError() {
ErrorResponse ex = new HttpMediaTypeNotAcceptableException(
"Could not parse Accept header: Invalid mime type \"foo\": does not contain '/'");
assertStatus(ex, HttpStatus.NOT_ACCEPTABLE);
assertDetail(ex, "Could not parse Accept header.");
assertDetailMessageCode(ex, "parseError", null);
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void asyncRequestTimeoutException() {
ErrorResponse ex = new AsyncRequestTimeoutException();
assertDetailMessageCode(ex, null, null);
assertStatus(ex, HttpStatus.SERVICE_UNAVAILABLE);
assertDetail(ex, null);
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void httpRequestMethodNotSupportedException() {
HttpRequestMethodNotSupportedException ex =
new HttpRequestMethodNotSupportedException("PUT", Arrays.asList("GET", "POST"));
assertStatus(ex, HttpStatus.METHOD_NOT_ALLOWED);
assertDetail(ex, "Method 'PUT' is not supported.");
assertDetailMessageCode(ex, null, new Object[] {ex.getMethod(), ex.getSupportedHttpMethods()});
assertThat(ex.getHeaders()).hasSize(1);
assertThat(ex.getHeaders().getAllow()).containsExactly(HttpMethod.GET, HttpMethod.POST);
}
@Test
void missingRequestHeaderException() {
MissingRequestHeaderException ex = new MissingRequestHeaderException("Authorization", this.methodParameter);
assertStatus(ex, HttpStatus.BAD_REQUEST);
assertDetail(ex, "Required header 'Authorization' is not present.");
assertDetailMessageCode(ex, null, new Object[] {ex.getHeaderName()});
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void missingServletRequestParameterException() {
MissingServletRequestParameterException ex = new MissingServletRequestParameterException("query", "String");
assertStatus(ex, HttpStatus.BAD_REQUEST);
assertDetail(ex, "Required parameter 'query' is not present.");
assertDetailMessageCode(ex, null, new Object[] {ex.getParameterName()});
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void missingMatrixVariableException() {
MissingMatrixVariableException ex = new MissingMatrixVariableException("region", this.methodParameter);
assertStatus(ex, HttpStatus.BAD_REQUEST);
assertDetail(ex, "Required path parameter 'region' is not present.");
assertDetailMessageCode(ex, null, new Object[] {ex.getVariableName()});
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void missingPathVariableException() {
MissingPathVariableException ex = new MissingPathVariableException("id", this.methodParameter);
assertStatus(ex, HttpStatus.INTERNAL_SERVER_ERROR);
assertDetail(ex, "Required path variable 'id' is not present.");
assertDetailMessageCode(ex, null, new Object[] {ex.getVariableName()});
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void missingRequestCookieException() {
MissingRequestCookieException ex = new MissingRequestCookieException("oreo", this.methodParameter);
assertStatus(ex, HttpStatus.BAD_REQUEST);
assertDetail(ex, "Required cookie 'oreo' is not present.");
assertDetailMessageCode(ex, null, new Object[] {ex.getCookieName()});
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void unsatisfiedServletRequestParameterException() {
UnsatisfiedServletRequestParameterException ex = new UnsatisfiedServletRequestParameterException(
new String[] { "foo=bar", "bar=baz" }, Collections.singletonMap("q", new String[] {"1"}));
assertStatus(ex, HttpStatus.BAD_REQUEST);
assertDetail(ex, "Invalid request parameters.");
assertDetailMessageCode(ex, null, new Object[] {List.of("\"foo=bar, bar=baz\"")});
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void missingServletRequestPartException() {
MissingServletRequestPartException ex = new MissingServletRequestPartException("file");
assertStatus(ex, HttpStatus.BAD_REQUEST);
assertDetail(ex, "Required part 'file' is not present.");
assertDetailMessageCode(ex, null, new Object[] {ex.getRequestPartName()});
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void methodArgumentNotValidException() {
MessageSourceTestHelper messageSourceHelper = new MessageSourceTestHelper(MethodArgumentNotValidException.class);
BindingResult bindingResult = messageSourceHelper.initBindingResult();
ErrorResponse ex = new MethodArgumentNotValidException(this.methodParameter, bindingResult);
assertStatus(ex, HttpStatus.BAD_REQUEST);
assertDetail(ex, "Invalid request content.");
messageSourceHelper.assertDetailMessage(ex);
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void unsupportedMediaTypeStatusException() {
List<MediaType> mediaTypes =
Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_CBOR);
UnsupportedMediaTypeStatusException ex = new UnsupportedMediaTypeStatusException(
MediaType.APPLICATION_XML, mediaTypes, HttpMethod.PATCH);
assertStatus(ex, HttpStatus.UNSUPPORTED_MEDIA_TYPE);
assertDetail(ex, "Content-Type 'application/xml' is not supported.");
assertDetailMessageCode(ex, null, new Object[] {ex.getContentType(), ex.getSupportedMediaTypes()});
HttpHeaders headers = ex.getHeaders();
assertThat(headers.getAccept()).isEqualTo(mediaTypes);
assertThat(headers.getAcceptPatch()).isEqualTo(mediaTypes);
}
@Test
void unsupportedMediaTypeStatusExceptionWithParseError() {
ErrorResponse ex = new UnsupportedMediaTypeStatusException(
"Could not parse Accept header: Invalid mime type \"foo\": does not contain '/'");
assertStatus(ex, HttpStatus.UNSUPPORTED_MEDIA_TYPE);
assertDetail(ex, "Could not parse Content-Type.");
assertDetailMessageCode(ex, "parseError", null);
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void notAcceptableStatusException() {
List<MediaType> mediaTypes = Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_CBOR);
NotAcceptableStatusException ex = new NotAcceptableStatusException(mediaTypes);
assertStatus(ex, HttpStatus.NOT_ACCEPTABLE);
assertDetail(ex, "Acceptable representations: [application/json, application/cbor].");
assertDetailMessageCode(ex, null, new Object[] {ex.getSupportedMediaTypes()});
assertThat(ex.getHeaders()).hasSize(1);
assertThat(ex.getHeaders().getAccept()).isEqualTo(mediaTypes);
}
@Test
void notAcceptableStatusExceptionWithParseError() {
ErrorResponse ex = new NotAcceptableStatusException(
"Could not parse Accept header: Invalid mime type \"foo\": does not contain '/'");
assertStatus(ex, HttpStatus.NOT_ACCEPTABLE);
assertDetail(ex, "Could not parse Accept header.");
assertDetailMessageCode(ex, "parseError", null);
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void serverErrorException() {
ServerErrorException ex = new ServerErrorException("Failure", null);
assertStatus(ex, HttpStatus.INTERNAL_SERVER_ERROR);
assertDetail(ex, "Failure");
assertDetailMessageCode(ex, null, new Object[] {ex.getReason()});
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void missingRequestValueException() {
MissingRequestValueException ex =
new MissingRequestValueException("foo", String.class, "header", this.methodParameter);
assertStatus(ex, HttpStatus.BAD_REQUEST);
assertDetail(ex, "Required header 'foo' is not present.");
assertDetailMessageCode(ex, null, new Object[] {ex.getLabel(), ex.getName()});
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void unsatisfiedRequestParameterException() {
UnsatisfiedRequestParameterException ex =
new UnsatisfiedRequestParameterException(
Arrays.asList("foo=bar", "bar=baz"),
new LinkedMultiValueMap<>(Collections.singletonMap("q", Arrays.asList("1", "2"))));
assertStatus(ex, HttpStatus.BAD_REQUEST);
assertDetail(ex, "Invalid request parameters.");
assertDetailMessageCode(ex, null, new Object[] {ex.getConditions()});
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void webExchangeBindException() {
MessageSourceTestHelper messageSourceHelper = new MessageSourceTestHelper(WebExchangeBindException.class);
BindingResult bindingResult = messageSourceHelper.initBindingResult();
WebExchangeBindException ex = new WebExchangeBindException(this.methodParameter, bindingResult);
assertStatus(ex, HttpStatus.BAD_REQUEST);
assertDetail(ex, "Invalid request content.");
messageSourceHelper.assertDetailMessage(ex);
assertThat(ex.getHeaders()).isEmpty();
}
@Test
void methodNotAllowedException() {
List<HttpMethod> supportedMethods = Arrays.asList(HttpMethod.GET, HttpMethod.POST);
MethodNotAllowedException ex = new MethodNotAllowedException(HttpMethod.PUT, supportedMethods);
assertStatus(ex, HttpStatus.METHOD_NOT_ALLOWED);
assertDetail(ex, "Supported methods: [GET, POST]");
assertDetailMessageCode(ex, null, new Object[] {ex.getHttpMethod(), supportedMethods});
assertThat(ex.getHeaders()).hasSize(1);
assertThat(ex.getHeaders().getAllow()).containsExactly(HttpMethod.GET, HttpMethod.POST);
}
@Test
void methodNotAllowedExceptionWithoutSupportedMethods() {
MethodNotAllowedException ex = new MethodNotAllowedException(HttpMethod.PUT, Collections.emptyList());
assertStatus(ex, HttpStatus.METHOD_NOT_ALLOWED);
assertDetail(ex, "Request method 'PUT' is not supported.");
assertDetailMessageCode(ex, null, new Object[] {ex.getHttpMethod(), Collections.emptyList()});
assertThat(ex.getHeaders()).isEmpty();
}
private void assertStatus(ErrorResponse ex, HttpStatus status) {
ProblemDetail body = ex.getBody();
assertThat(ex.getStatusCode()).isEqualTo(status);
assertThat(body.getStatus()).isEqualTo(status.value());
assertThat(body.getTitle()).isEqualTo(status.getReasonPhrase());
}
private void assertDetail(ErrorResponse ex, @Nullable String detail) {
if (detail != null) {
assertThat(ex.getBody().getDetail()).isEqualTo(detail);
}
else {
assertThat(ex.getBody().getDetail()).isNull();
}
}
private void assertDetailMessageCode(
ErrorResponse ex, @Nullable String suffix, @Nullable Object[] arguments) {
assertThat(ex.getDetailMessageCode())
.isEqualTo(ErrorResponse.getDefaultDetailMessageCode(((Exception) ex).getClass(), suffix));
if (arguments != null) {
assertThat(ex.getDetailMessageArguments()).containsExactlyElementsOf(Arrays.asList(arguments));
}
else {
assertThat(ex.getDetailMessageArguments()).isNull();
}
}
@SuppressWarnings("unused")
private void handle(String arg) {}
private static class MessageSourceTestHelper {
private final String code;
public MessageSourceTestHelper(Class<? extends ErrorResponse> exceptionType) {
this.code = "problemDetail." + exceptionType.getName();
}
public BindingResult initBindingResult() {
BindingResult bindingResult = new BindException(new TestBean(), "myBean");
bindingResult.reject("bean.invalid.A", "Invalid bean message");
bindingResult.reject("bean.invalid.B");
bindingResult.rejectValue("name", "name.required", "Name is required");
bindingResult.rejectValue("age", "age.min");
return bindingResult;
}
private void assertDetailMessage(ErrorResponse ex) {
StaticMessageSource messageSource = new StaticMessageSource();
messageSource.addMessage(this.code, Locale.UK, "Failures {0}. nested failures: {1}");
messageSource.addMessage("bean.invalid.A", Locale.UK, "Bean A message");
messageSource.addMessage("bean.invalid.B", Locale.UK, "Bean B message");
messageSource.addMessage("name.required", Locale.UK, "Required name message");
messageSource.addMessage("age.min", Locale.UK, "Minimum age message");
String message = messageSource.getMessage(
ex.getDetailMessageCode(), ex.getDetailMessageArguments(), Locale.UK);
assertThat(message).isEqualTo("" +
"Failures ['Invalid bean message', 'bean.invalid.B']. " +
"nested failures: [name: 'Name is required', age: 'age.min']");
message = messageSource.getMessage(
ex.getDetailMessageCode(), ex.getDetailMessageArguments(messageSource, Locale.UK), Locale.UK);
assertThat(message).isEqualTo("" +
"Failures ['Bean A message', 'Bean B message']. " +
"nested failures: [name: 'Required name message', age: 'Minimum age message']");
}
}
}