Skip to content

Commit 9b07290

Browse files
committed
Polish MockHttpServletResponseTests
1 parent 7dbf42e commit 9b07290

File tree

1 file changed

+58
-55
lines changed

1 file changed

+58
-55
lines changed

spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,11 +26,15 @@
2626

2727
import org.junit.jupiter.api.Test;
2828

29-
import org.springframework.http.HttpHeaders;
3029
import org.springframework.web.util.WebUtils;
3130

3231
import static org.assertj.core.api.Assertions.assertThat;
3332
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;
3438

3539
/**
3640
* Unit tests for {@link MockHttpServletResponse}.
@@ -55,7 +59,7 @@ void setContentType() {
5559
String contentType = "test/plain";
5660
response.setContentType(contentType);
5761
assertThat(response.getContentType()).isEqualTo(contentType);
58-
assertThat(response.getHeader("Content-Type")).isEqualTo(contentType);
62+
assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType);
5963
assertThat(response.getCharacterEncoding()).isEqualTo(WebUtils.DEFAULT_CHARACTER_ENCODING);
6064
}
6165

@@ -65,51 +69,51 @@ void setContentTypeUTF8() {
6569
response.setContentType(contentType);
6670
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
6771
assertThat(response.getContentType()).isEqualTo(contentType);
68-
assertThat(response.getHeader("Content-Type")).isEqualTo(contentType);
72+
assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType);
6973
}
7074

7175
@Test
7276
void contentTypeHeader() {
7377
String contentType = "test/plain";
74-
response.addHeader("Content-Type", contentType);
78+
response.setHeader(CONTENT_TYPE, contentType);
7579
assertThat(response.getContentType()).isEqualTo(contentType);
76-
assertThat(response.getHeader("Content-Type")).isEqualTo(contentType);
80+
assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType);
7781
assertThat(response.getCharacterEncoding()).isEqualTo(WebUtils.DEFAULT_CHARACTER_ENCODING);
7882

7983
response = new MockHttpServletResponse();
80-
response.setHeader("Content-Type", contentType);
84+
response.addHeader(CONTENT_TYPE, contentType);
8185
assertThat(response.getContentType()).isEqualTo(contentType);
82-
assertThat(response.getHeader("Content-Type")).isEqualTo(contentType);
86+
assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType);
8387
assertThat(response.getCharacterEncoding()).isEqualTo(WebUtils.DEFAULT_CHARACTER_ENCODING);
8488
}
8589

8690
@Test
8791
void contentTypeHeaderUTF8() {
8892
String contentType = "test/plain;charset=UTF-8";
89-
response.setHeader("Content-Type", contentType);
93+
response.setHeader(CONTENT_TYPE, contentType);
9094
assertThat(response.getContentType()).isEqualTo(contentType);
91-
assertThat(response.getHeader("Content-Type")).isEqualTo(contentType);
95+
assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType);
9296
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
9397

9498
response = new MockHttpServletResponse();
95-
response.addHeader("Content-Type", contentType);
99+
response.addHeader(CONTENT_TYPE, contentType);
96100
assertThat(response.getContentType()).isEqualTo(contentType);
97-
assertThat(response.getHeader("Content-Type")).isEqualTo(contentType);
101+
assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType);
98102
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
99103
}
100104

101105
@Test // SPR-12677
102106
void contentTypeHeaderWithMoreComplexCharsetSyntax() {
103107
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);
105109
assertThat(response.getContentType()).isEqualTo(contentType);
106-
assertThat(response.getHeader("Content-Type")).isEqualTo(contentType);
110+
assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType);
107111
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
108112

109113
response = new MockHttpServletResponse();
110-
response.addHeader("Content-Type", contentType);
114+
response.addHeader(CONTENT_TYPE, contentType);
111115
assertThat(response.getContentType()).isEqualTo(contentType);
112-
assertThat(response.getHeader("Content-Type")).isEqualTo(contentType);
116+
assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType);
113117
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
114118
}
115119

@@ -118,7 +122,7 @@ void setContentTypeThenCharacterEncoding() {
118122
response.setContentType("test/plain");
119123
response.setCharacterEncoding("UTF-8");
120124
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");
122126
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
123127
}
124128

@@ -127,29 +131,29 @@ void setCharacterEncodingThenContentType() {
127131
response.setCharacterEncoding("UTF-8");
128132
response.setContentType("test/plain");
129133
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");
131135
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
132136
}
133137

134138
@Test
135139
void contentLength() {
136140
response.setContentLength(66);
137141
assertThat(response.getContentLength()).isEqualTo(66);
138-
assertThat(response.getHeader("Content-Length")).isEqualTo("66");
142+
assertThat(response.getHeader(CONTENT_LENGTH)).isEqualTo("66");
139143
}
140144

141145
@Test
142146
void contentLengthHeader() {
143-
response.addHeader("Content-Length", "66");
147+
response.addHeader(CONTENT_LENGTH, "66");
144148
assertThat(response.getContentLength()).isEqualTo(66);
145-
assertThat(response.getHeader("Content-Length")).isEqualTo("66");
149+
assertThat(response.getHeader(CONTENT_LENGTH)).isEqualTo("66");
146150
}
147151

148152
@Test
149153
void contentLengthIntHeader() {
150-
response.addIntHeader("Content-Length", 66);
154+
response.addIntHeader(CONTENT_LENGTH, 66);
151155
assertThat(response.getContentLength()).isEqualTo(66);
152-
assertThat(response.getHeader("Content-Length")).isEqualTo("66");
156+
assertThat(response.getHeader(CONTENT_LENGTH)).isEqualTo("66");
153157
}
154158

155159
@Test
@@ -173,7 +177,7 @@ void cookies() {
173177

174178
response.addCookie(cookie);
175179

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; " +
177181
"Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; " +
178182
"Secure; HttpOnly"));
179183
}
@@ -272,52 +276,51 @@ void sendRedirect() throws IOException {
272276
String redirectUrl = "/redirect";
273277
response.sendRedirect(redirectUrl);
274278
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_MOVED_TEMPORARILY);
275-
assertThat(response.getHeader("Location")).isEqualTo(redirectUrl);
279+
assertThat(response.getHeader(LOCATION)).isEqualTo(redirectUrl);
276280
assertThat(response.getRedirectedUrl()).isEqualTo(redirectUrl);
277281
assertThat(response.isCommitted()).isTrue();
278282
}
279283

280284
@Test
281285
void locationHeaderUpdatesGetRedirectedUrl() {
282286
String redirectUrl = "/redirect";
283-
response.setHeader("Location", redirectUrl);
287+
response.setHeader(LOCATION, redirectUrl);
284288
assertThat(response.getRedirectedUrl()).isEqualTo(redirectUrl);
285289
}
286290

287291
@Test
288292
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");
291295
}
292296

293297
@Test
294298
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");
299303
}
300304

301305
@Test
302306
void getDateHeader() {
303307
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);
307311
}
308312

309313
@Test
310314
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));
315318
}
316319

317320
@Test // SPR-16160
318321
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);
321324
}
322325

323326
@Test // SPR-10414
@@ -331,7 +334,7 @@ void modifyStatusAfterSendError() throws IOException {
331334
@SuppressWarnings("deprecation")
332335
void modifyStatusMessageAfterSendError() throws IOException {
333336
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");
335338
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_FOUND);
336339
}
337340

@@ -340,12 +343,12 @@ void modifyStatusMessageAfterSendError() throws IOException {
340343
*/
341344
@Test
342345
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");
344347
assertNumCookies(1);
345348
assertPrimarySessionCookie("123");
346349

347350
// 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");
349352
assertNumCookies(1);
350353
assertPrimarySessionCookie("999");
351354
}
@@ -357,9 +360,9 @@ void setCookieHeader() {
357360
void setCookieHeaderWithExpiresAttribute() {
358361
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
359362
"HttpOnly; SameSite=Lax";
360-
response.setHeader(HttpHeaders.SET_COOKIE, cookieValue);
363+
response.setHeader(SET_COOKIE, cookieValue);
361364
assertNumCookies(1);
362-
assertThat(response.getHeader(HttpHeaders.SET_COOKIE)).isEqualTo(cookieValue);
365+
assertThat(response.getHeader(SET_COOKIE)).isEqualTo(cookieValue);
363366
}
364367

365368
/**
@@ -368,22 +371,22 @@ void setCookieHeaderWithExpiresAttribute() {
368371
@Test
369372
void setCookieHeaderWithZeroExpiresAttribute() {
370373
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=0";
371-
response.setHeader(HttpHeaders.SET_COOKIE, cookieValue);
374+
response.setHeader(SET_COOKIE, cookieValue);
372375
assertNumCookies(1);
373-
String header = response.getHeader(HttpHeaders.SET_COOKIE);
376+
String header = response.getHeader(SET_COOKIE);
374377
assertThat(header).isNotEqualTo(cookieValue);
375378
// We don't assert the actual Expires value since it is based on the current time.
376379
assertThat(header).startsWith("SESSION=123; Path=/; Max-Age=100; Expires=");
377380
}
378381

379382
@Test
380383
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");
382385
assertNumCookies(1);
383386
assertPrimarySessionCookie("123");
384387

385388
// 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");
387390
assertNumCookies(2);
388391
assertPrimarySessionCookie("123");
389392
assertCookieValues("123", "999");
@@ -396,8 +399,8 @@ void addCookieHeader() {
396399
void addCookieHeaderWithExpiresAttribute() {
397400
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " +
398401
"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);
401404
}
402405

403406
/**
@@ -406,9 +409,9 @@ void addCookieHeaderWithExpiresAttribute() {
406409
@Test
407410
void addCookieHeaderWithZeroExpiresAttribute() {
408411
String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=0";
409-
response.addHeader(HttpHeaders.SET_COOKIE, cookieValue);
412+
response.addHeader(SET_COOKIE, cookieValue);
410413
assertNumCookies(1);
411-
String header = response.getHeader(HttpHeaders.SET_COOKIE);
414+
String header = response.getHeader(SET_COOKIE);
412415
assertThat(header).isNotEqualTo(cookieValue);
413416
// We don't assert the actual Expires value since it is based on the current time.
414417
assertThat(header).startsWith("SESSION=123; Path=/; Max-Age=100; Expires=");
@@ -427,7 +430,7 @@ void addCookie() {
427430
response.addCookie(mockCookie);
428431

429432
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; " +
431434
"Expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure; HttpOnly; SameSite=Lax"));
432435

433436
// Adding a 2nd Cookie should result in 2 Cookies.

0 commit comments

Comments
 (0)