1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
26
26
27
27
import org .junit .jupiter .api .Test ;
28
28
29
- import org .springframework .http .HttpHeaders ;
30
29
import org .springframework .web .util .WebUtils ;
31
30
32
31
import static org .assertj .core .api .Assertions .assertThat ;
33
32
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
33
+ import static org .springframework .http .HttpHeaders .CONTENT_LENGTH ;
34
+ import static org .springframework .http .HttpHeaders .CONTENT_TYPE ;
35
+ import static org .springframework .http .HttpHeaders .LAST_MODIFIED ;
36
+ import static org .springframework .http .HttpHeaders .LOCATION ;
37
+ import static org .springframework .http .HttpHeaders .SET_COOKIE ;
34
38
35
39
/**
36
40
* Unit tests for {@link MockHttpServletResponse}.
@@ -55,7 +59,7 @@ void setContentType() {
55
59
String contentType = "test/plain" ;
56
60
response .setContentType (contentType );
57
61
assertThat (response .getContentType ()).isEqualTo (contentType );
58
- assertThat (response .getHeader ("Content-Type" )).isEqualTo (contentType );
62
+ assertThat (response .getHeader (CONTENT_TYPE )).isEqualTo (contentType );
59
63
assertThat (response .getCharacterEncoding ()).isEqualTo (WebUtils .DEFAULT_CHARACTER_ENCODING );
60
64
}
61
65
@@ -65,51 +69,51 @@ void setContentTypeUTF8() {
65
69
response .setContentType (contentType );
66
70
assertThat (response .getCharacterEncoding ()).isEqualTo ("UTF-8" );
67
71
assertThat (response .getContentType ()).isEqualTo (contentType );
68
- assertThat (response .getHeader ("Content-Type" )).isEqualTo (contentType );
72
+ assertThat (response .getHeader (CONTENT_TYPE )).isEqualTo (contentType );
69
73
}
70
74
71
75
@ Test
72
76
void contentTypeHeader () {
73
77
String contentType = "test/plain" ;
74
- response .addHeader ( "Content-Type" , contentType );
78
+ response .setHeader ( CONTENT_TYPE , contentType );
75
79
assertThat (response .getContentType ()).isEqualTo (contentType );
76
- assertThat (response .getHeader ("Content-Type" )).isEqualTo (contentType );
80
+ assertThat (response .getHeader (CONTENT_TYPE )).isEqualTo (contentType );
77
81
assertThat (response .getCharacterEncoding ()).isEqualTo (WebUtils .DEFAULT_CHARACTER_ENCODING );
78
82
79
83
response = new MockHttpServletResponse ();
80
- response .setHeader ( "Content-Type" , contentType );
84
+ response .addHeader ( CONTENT_TYPE , contentType );
81
85
assertThat (response .getContentType ()).isEqualTo (contentType );
82
- assertThat (response .getHeader ("Content-Type" )).isEqualTo (contentType );
86
+ assertThat (response .getHeader (CONTENT_TYPE )).isEqualTo (contentType );
83
87
assertThat (response .getCharacterEncoding ()).isEqualTo (WebUtils .DEFAULT_CHARACTER_ENCODING );
84
88
}
85
89
86
90
@ Test
87
91
void contentTypeHeaderUTF8 () {
88
92
String contentType = "test/plain;charset=UTF-8" ;
89
- response .setHeader ("Content-Type" , contentType );
93
+ response .setHeader (CONTENT_TYPE , contentType );
90
94
assertThat (response .getContentType ()).isEqualTo (contentType );
91
- assertThat (response .getHeader ("Content-Type" )).isEqualTo (contentType );
95
+ assertThat (response .getHeader (CONTENT_TYPE )).isEqualTo (contentType );
92
96
assertThat (response .getCharacterEncoding ()).isEqualTo ("UTF-8" );
93
97
94
98
response = new MockHttpServletResponse ();
95
- response .addHeader ("Content-Type" , contentType );
99
+ response .addHeader (CONTENT_TYPE , contentType );
96
100
assertThat (response .getContentType ()).isEqualTo (contentType );
97
- assertThat (response .getHeader ("Content-Type" )).isEqualTo (contentType );
101
+ assertThat (response .getHeader (CONTENT_TYPE )).isEqualTo (contentType );
98
102
assertThat (response .getCharacterEncoding ()).isEqualTo ("UTF-8" );
99
103
}
100
104
101
105
@ Test // SPR-12677
102
106
void contentTypeHeaderWithMoreComplexCharsetSyntax () {
103
107
String contentType = "test/plain;charset=\" utf-8\" ;foo=\" charset=bar\" ;foocharset=bar;foo=bar" ;
104
- response .setHeader ("Content-Type" , contentType );
108
+ response .setHeader (CONTENT_TYPE , contentType );
105
109
assertThat (response .getContentType ()).isEqualTo (contentType );
106
- assertThat (response .getHeader ("Content-Type" )).isEqualTo (contentType );
110
+ assertThat (response .getHeader (CONTENT_TYPE )).isEqualTo (contentType );
107
111
assertThat (response .getCharacterEncoding ()).isEqualTo ("UTF-8" );
108
112
109
113
response = new MockHttpServletResponse ();
110
- response .addHeader ("Content-Type" , contentType );
114
+ response .addHeader (CONTENT_TYPE , contentType );
111
115
assertThat (response .getContentType ()).isEqualTo (contentType );
112
- assertThat (response .getHeader ("Content-Type" )).isEqualTo (contentType );
116
+ assertThat (response .getHeader (CONTENT_TYPE )).isEqualTo (contentType );
113
117
assertThat (response .getCharacterEncoding ()).isEqualTo ("UTF-8" );
114
118
}
115
119
@@ -118,7 +122,7 @@ void setContentTypeThenCharacterEncoding() {
118
122
response .setContentType ("test/plain" );
119
123
response .setCharacterEncoding ("UTF-8" );
120
124
assertThat (response .getContentType ()).isEqualTo ("test/plain" );
121
- assertThat (response .getHeader ("Content-Type" )).isEqualTo ("test/plain;charset=UTF-8" );
125
+ assertThat (response .getHeader (CONTENT_TYPE )).isEqualTo ("test/plain;charset=UTF-8" );
122
126
assertThat (response .getCharacterEncoding ()).isEqualTo ("UTF-8" );
123
127
}
124
128
@@ -127,29 +131,29 @@ void setCharacterEncodingThenContentType() {
127
131
response .setCharacterEncoding ("UTF-8" );
128
132
response .setContentType ("test/plain" );
129
133
assertThat (response .getContentType ()).isEqualTo ("test/plain" );
130
- assertThat (response .getHeader ("Content-Type" )).isEqualTo ("test/plain;charset=UTF-8" );
134
+ assertThat (response .getHeader (CONTENT_TYPE )).isEqualTo ("test/plain;charset=UTF-8" );
131
135
assertThat (response .getCharacterEncoding ()).isEqualTo ("UTF-8" );
132
136
}
133
137
134
138
@ Test
135
139
void contentLength () {
136
140
response .setContentLength (66 );
137
141
assertThat (response .getContentLength ()).isEqualTo (66 );
138
- assertThat (response .getHeader ("Content-Length" )).isEqualTo ("66" );
142
+ assertThat (response .getHeader (CONTENT_LENGTH )).isEqualTo ("66" );
139
143
}
140
144
141
145
@ Test
142
146
void contentLengthHeader () {
143
- response .addHeader ("Content-Length" , "66" );
147
+ response .addHeader (CONTENT_LENGTH , "66" );
144
148
assertThat (response .getContentLength ()).isEqualTo (66 );
145
- assertThat (response .getHeader ("Content-Length" )).isEqualTo ("66" );
149
+ assertThat (response .getHeader (CONTENT_LENGTH )).isEqualTo ("66" );
146
150
}
147
151
148
152
@ Test
149
153
void contentLengthIntHeader () {
150
- response .addIntHeader ("Content-Length" , 66 );
154
+ response .addIntHeader (CONTENT_LENGTH , 66 );
151
155
assertThat (response .getContentLength ()).isEqualTo (66 );
152
- assertThat (response .getHeader ("Content-Length" )).isEqualTo ("66" );
156
+ assertThat (response .getHeader (CONTENT_LENGTH )).isEqualTo ("66" );
153
157
}
154
158
155
159
@ Test
@@ -173,7 +177,7 @@ void cookies() {
173
177
174
178
response .addCookie (cookie );
175
179
176
- assertThat (response .getHeader (HttpHeaders . SET_COOKIE )).isEqualTo (("foo=bar; Path=/path; Domain=example.com; " +
180
+ assertThat (response .getHeader (SET_COOKIE )).isEqualTo (("foo=bar; Path=/path; Domain=example.com; " +
177
181
"Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; " +
178
182
"Secure; HttpOnly" ));
179
183
}
@@ -272,52 +276,51 @@ void sendRedirect() throws IOException {
272
276
String redirectUrl = "/redirect" ;
273
277
response .sendRedirect (redirectUrl );
274
278
assertThat (response .getStatus ()).isEqualTo (HttpServletResponse .SC_MOVED_TEMPORARILY );
275
- assertThat (response .getHeader ("Location" )).isEqualTo (redirectUrl );
279
+ assertThat (response .getHeader (LOCATION )).isEqualTo (redirectUrl );
276
280
assertThat (response .getRedirectedUrl ()).isEqualTo (redirectUrl );
277
281
assertThat (response .isCommitted ()).isTrue ();
278
282
}
279
283
280
284
@ Test
281
285
void locationHeaderUpdatesGetRedirectedUrl () {
282
286
String redirectUrl = "/redirect" ;
283
- response .setHeader ("Location" , redirectUrl );
287
+ response .setHeader (LOCATION , redirectUrl );
284
288
assertThat (response .getRedirectedUrl ()).isEqualTo (redirectUrl );
285
289
}
286
290
287
291
@ Test
288
292
void setDateHeader () {
289
- response .setDateHeader ("Last-Modified" , 1437472800000L );
290
- assertThat (response .getHeader ("Last-Modified" )).isEqualTo ("Tue, 21 Jul 2015 10:00:00 GMT" );
293
+ response .setDateHeader (LAST_MODIFIED , 1437472800000L );
294
+ assertThat (response .getHeader (LAST_MODIFIED )).isEqualTo ("Tue, 21 Jul 2015 10:00:00 GMT" );
291
295
}
292
296
293
297
@ Test
294
298
void addDateHeader () {
295
- response .addDateHeader ("Last-Modified" , 1437472800000L );
296
- response .addDateHeader ("Last-Modified" , 1437472801000L );
297
- assertThat (response .getHeaders ("Last-Modified" ).get (0 )).isEqualTo ("Tue, 21 Jul 2015 10:00:00 GMT" );
298
- assertThat (response .getHeaders ("Last-Modified" ).get (1 )).isEqualTo ("Tue, 21 Jul 2015 10:00:01 GMT" );
299
+ response .addDateHeader (LAST_MODIFIED , 1437472800000L );
300
+ response .addDateHeader (LAST_MODIFIED , 1437472801000L );
301
+ assertThat (response .getHeaders (LAST_MODIFIED ).get (0 )).isEqualTo ("Tue, 21 Jul 2015 10:00:00 GMT" );
302
+ assertThat (response .getHeaders (LAST_MODIFIED ).get (1 )).isEqualTo ("Tue, 21 Jul 2015 10:00:01 GMT" );
299
303
}
300
304
301
305
@ Test
302
306
void getDateHeader () {
303
307
long time = 1437472800000L ;
304
- response .setDateHeader ("Last-Modified" , time );
305
- assertThat (response .getHeader ("Last-Modified" )).isEqualTo ("Tue, 21 Jul 2015 10:00:00 GMT" );
306
- assertThat (response .getDateHeader ("Last-Modified" )).isEqualTo (time );
308
+ response .setDateHeader (LAST_MODIFIED , time );
309
+ assertThat (response .getHeader (LAST_MODIFIED )).isEqualTo ("Tue, 21 Jul 2015 10:00:00 GMT" );
310
+ assertThat (response .getDateHeader (LAST_MODIFIED )).isEqualTo (time );
307
311
}
308
312
309
313
@ Test
310
314
void getInvalidDateHeader () {
311
- response .setHeader ("Last-Modified" , "invalid" );
312
- assertThat (response .getHeader ("Last-Modified" )).isEqualTo ("invalid" );
313
- assertThatIllegalArgumentException ().isThrownBy (() ->
314
- response .getDateHeader ("Last-Modified" ));
315
+ response .setHeader (LAST_MODIFIED , "invalid" );
316
+ assertThat (response .getHeader (LAST_MODIFIED )).isEqualTo ("invalid" );
317
+ assertThatIllegalArgumentException ().isThrownBy (() -> response .getDateHeader (LAST_MODIFIED ));
315
318
}
316
319
317
320
@ Test // SPR-16160
318
321
void getNonExistentDateHeader () {
319
- assertThat (response .getHeader ("Last-Modified" )).isNull ();
320
- assertThat (response .getDateHeader ("Last-Modified" )).isEqualTo (-1 );
322
+ assertThat (response .getHeader (LAST_MODIFIED )).isNull ();
323
+ assertThat (response .getDateHeader (LAST_MODIFIED )).isEqualTo (-1 );
321
324
}
322
325
323
326
@ Test // SPR-10414
@@ -331,7 +334,7 @@ void modifyStatusAfterSendError() throws IOException {
331
334
@ SuppressWarnings ("deprecation" )
332
335
void modifyStatusMessageAfterSendError () throws IOException {
333
336
response .sendError (HttpServletResponse .SC_NOT_FOUND );
334
- response .setStatus (HttpServletResponse .SC_INTERNAL_SERVER_ERROR ,"Server Error" );
337
+ response .setStatus (HttpServletResponse .SC_INTERNAL_SERVER_ERROR , "Server Error" );
335
338
assertThat (response .getStatus ()).isEqualTo (HttpServletResponse .SC_NOT_FOUND );
336
339
}
337
340
@@ -340,12 +343,12 @@ void modifyStatusMessageAfterSendError() throws IOException {
340
343
*/
341
344
@ Test
342
345
void setCookieHeader () {
343
- response .setHeader (HttpHeaders . SET_COOKIE , "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax" );
346
+ response .setHeader (SET_COOKIE , "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax" );
344
347
assertNumCookies (1 );
345
348
assertPrimarySessionCookie ("123" );
346
349
347
350
// Setting the Set-Cookie header a 2nd time should overwrite the previous value
348
- response .setHeader (HttpHeaders . SET_COOKIE , "SESSION=999; Path=/; Secure; HttpOnly; SameSite=Lax" );
351
+ response .setHeader (SET_COOKIE , "SESSION=999; Path=/; Secure; HttpOnly; SameSite=Lax" );
349
352
assertNumCookies (1 );
350
353
assertPrimarySessionCookie ("999" );
351
354
}
@@ -357,9 +360,9 @@ void setCookieHeader() {
357
360
void setCookieHeaderWithExpiresAttribute () {
358
361
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
359
362
"HttpOnly; SameSite=Lax" ;
360
- response .setHeader (HttpHeaders . SET_COOKIE , cookieValue );
363
+ response .setHeader (SET_COOKIE , cookieValue );
361
364
assertNumCookies (1 );
362
- assertThat (response .getHeader (HttpHeaders . SET_COOKIE )).isEqualTo (cookieValue );
365
+ assertThat (response .getHeader (SET_COOKIE )).isEqualTo (cookieValue );
363
366
}
364
367
365
368
/**
@@ -368,22 +371,22 @@ void setCookieHeaderWithExpiresAttribute() {
368
371
@ Test
369
372
void setCookieHeaderWithZeroExpiresAttribute () {
370
373
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=0" ;
371
- response .setHeader (HttpHeaders . SET_COOKIE , cookieValue );
374
+ response .setHeader (SET_COOKIE , cookieValue );
372
375
assertNumCookies (1 );
373
- String header = response .getHeader (HttpHeaders . SET_COOKIE );
376
+ String header = response .getHeader (SET_COOKIE );
374
377
assertThat (header ).isNotEqualTo (cookieValue );
375
378
// We don't assert the actual Expires value since it is based on the current time.
376
379
assertThat (header ).startsWith ("SESSION=123; Path=/; Max-Age=100; Expires=" );
377
380
}
378
381
379
382
@ Test
380
383
void addCookieHeader () {
381
- response .addHeader (HttpHeaders . SET_COOKIE , "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax" );
384
+ response .addHeader (SET_COOKIE , "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax" );
382
385
assertNumCookies (1 );
383
386
assertPrimarySessionCookie ("123" );
384
387
385
388
// Adding a 2nd cookie header should result in 2 cookies.
386
- response .addHeader (HttpHeaders . SET_COOKIE , "SESSION=999; Path=/; Secure; HttpOnly; SameSite=Lax" );
389
+ response .addHeader (SET_COOKIE , "SESSION=999; Path=/; Secure; HttpOnly; SameSite=Lax" );
387
390
assertNumCookies (2 );
388
391
assertPrimarySessionCookie ("123" );
389
392
assertCookieValues ("123" , "999" );
@@ -396,8 +399,8 @@ void addCookieHeader() {
396
399
void addCookieHeaderWithExpiresAttribute () {
397
400
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
398
401
"HttpOnly; SameSite=Lax" ;
399
- response .addHeader (HttpHeaders . SET_COOKIE , cookieValue );
400
- assertThat (response .getHeader (HttpHeaders . SET_COOKIE )).isEqualTo (cookieValue );
402
+ response .addHeader (SET_COOKIE , cookieValue );
403
+ assertThat (response .getHeader (SET_COOKIE )).isEqualTo (cookieValue );
401
404
}
402
405
403
406
/**
@@ -406,9 +409,9 @@ void addCookieHeaderWithExpiresAttribute() {
406
409
@ Test
407
410
void addCookieHeaderWithZeroExpiresAttribute () {
408
411
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=0" ;
409
- response .addHeader (HttpHeaders . SET_COOKIE , cookieValue );
412
+ response .addHeader (SET_COOKIE , cookieValue );
410
413
assertNumCookies (1 );
411
- String header = response .getHeader (HttpHeaders . SET_COOKIE );
414
+ String header = response .getHeader (SET_COOKIE );
412
415
assertThat (header ).isNotEqualTo (cookieValue );
413
416
// We don't assert the actual Expires value since it is based on the current time.
414
417
assertThat (header ).startsWith ("SESSION=123; Path=/; Max-Age=100; Expires=" );
@@ -427,7 +430,7 @@ void addCookie() {
427
430
response .addCookie (mockCookie );
428
431
429
432
assertNumCookies (1 );
430
- assertThat (response .getHeader (HttpHeaders . SET_COOKIE )).isEqualTo (("SESSION=123; Path=/; Domain=example.com; Max-Age=0; " +
433
+ assertThat (response .getHeader (SET_COOKIE )).isEqualTo (("SESSION=123; Path=/; Domain=example.com; Max-Age=0; " +
431
434
"Expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure; HttpOnly; SameSite=Lax" ));
432
435
433
436
// Adding a 2nd Cookie should result in 2 Cookies.
0 commit comments