From 9b0729007fc01d0d5d3858aaeb75022fea9cce83 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 26 Jul 2020 09:57:25 +0200 Subject: [PATCH 1/2] Polish MockHttpServletResponseTests --- .../web/MockHttpServletResponseTests.java | 113 +++++++++--------- 1 file changed, 58 insertions(+), 55 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java index 075d45c2d90..1dd6f117aca 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java +++ b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -26,11 +26,15 @@ import javax.servlet.http.HttpServletResponse; import org.junit.jupiter.api.Test; -import org.springframework.http.HttpHeaders; import org.springframework.web.util.WebUtils; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.springframework.http.HttpHeaders.CONTENT_LENGTH; +import static org.springframework.http.HttpHeaders.CONTENT_TYPE; +import static org.springframework.http.HttpHeaders.LAST_MODIFIED; +import static org.springframework.http.HttpHeaders.LOCATION; +import static org.springframework.http.HttpHeaders.SET_COOKIE; /** * Unit tests for {@link MockHttpServletResponse}. @@ -55,7 +59,7 @@ class MockHttpServletResponseTests { String contentType = "test/plain"; response.setContentType(contentType); assertThat(response.getContentType()).isEqualTo(contentType); - assertThat(response.getHeader("Content-Type")).isEqualTo(contentType); + assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType); assertThat(response.getCharacterEncoding()).isEqualTo(WebUtils.DEFAULT_CHARACTER_ENCODING); } @@ -65,51 +69,51 @@ class MockHttpServletResponseTests { response.setContentType(contentType); assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8"); assertThat(response.getContentType()).isEqualTo(contentType); - assertThat(response.getHeader("Content-Type")).isEqualTo(contentType); + assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType); } @Test void contentTypeHeader() { String contentType = "test/plain"; - response.addHeader("Content-Type", contentType); + response.setHeader(CONTENT_TYPE, contentType); assertThat(response.getContentType()).isEqualTo(contentType); - assertThat(response.getHeader("Content-Type")).isEqualTo(contentType); + assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType); assertThat(response.getCharacterEncoding()).isEqualTo(WebUtils.DEFAULT_CHARACTER_ENCODING); response = new MockHttpServletResponse(); - response.setHeader("Content-Type", contentType); + response.addHeader(CONTENT_TYPE, contentType); assertThat(response.getContentType()).isEqualTo(contentType); - assertThat(response.getHeader("Content-Type")).isEqualTo(contentType); + assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType); assertThat(response.getCharacterEncoding()).isEqualTo(WebUtils.DEFAULT_CHARACTER_ENCODING); } @Test void contentTypeHeaderUTF8() { String contentType = "test/plain;charset=UTF-8"; - response.setHeader("Content-Type", contentType); + response.setHeader(CONTENT_TYPE, contentType); assertThat(response.getContentType()).isEqualTo(contentType); - assertThat(response.getHeader("Content-Type")).isEqualTo(contentType); + assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType); assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8"); response = new MockHttpServletResponse(); - response.addHeader("Content-Type", contentType); + response.addHeader(CONTENT_TYPE, contentType); assertThat(response.getContentType()).isEqualTo(contentType); - assertThat(response.getHeader("Content-Type")).isEqualTo(contentType); + assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType); assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8"); } @Test // SPR-12677 void contentTypeHeaderWithMoreComplexCharsetSyntax() { String contentType = "test/plain;charset=\"utf-8\";foo=\"charset=bar\";foocharset=bar;foo=bar"; - response.setHeader("Content-Type", contentType); + response.setHeader(CONTENT_TYPE, contentType); assertThat(response.getContentType()).isEqualTo(contentType); - assertThat(response.getHeader("Content-Type")).isEqualTo(contentType); + assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType); assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8"); response = new MockHttpServletResponse(); - response.addHeader("Content-Type", contentType); + response.addHeader(CONTENT_TYPE, contentType); assertThat(response.getContentType()).isEqualTo(contentType); - assertThat(response.getHeader("Content-Type")).isEqualTo(contentType); + assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo(contentType); assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8"); } @@ -118,7 +122,7 @@ class MockHttpServletResponseTests { response.setContentType("test/plain"); response.setCharacterEncoding("UTF-8"); assertThat(response.getContentType()).isEqualTo("test/plain"); - assertThat(response.getHeader("Content-Type")).isEqualTo("test/plain;charset=UTF-8"); + assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo("test/plain;charset=UTF-8"); assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8"); } @@ -127,7 +131,7 @@ class MockHttpServletResponseTests { response.setCharacterEncoding("UTF-8"); response.setContentType("test/plain"); assertThat(response.getContentType()).isEqualTo("test/plain"); - assertThat(response.getHeader("Content-Type")).isEqualTo("test/plain;charset=UTF-8"); + assertThat(response.getHeader(CONTENT_TYPE)).isEqualTo("test/plain;charset=UTF-8"); assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8"); } @@ -135,21 +139,21 @@ class MockHttpServletResponseTests { void contentLength() { response.setContentLength(66); assertThat(response.getContentLength()).isEqualTo(66); - assertThat(response.getHeader("Content-Length")).isEqualTo("66"); + assertThat(response.getHeader(CONTENT_LENGTH)).isEqualTo("66"); } @Test void contentLengthHeader() { - response.addHeader("Content-Length", "66"); + response.addHeader(CONTENT_LENGTH, "66"); assertThat(response.getContentLength()).isEqualTo(66); - assertThat(response.getHeader("Content-Length")).isEqualTo("66"); + assertThat(response.getHeader(CONTENT_LENGTH)).isEqualTo("66"); } @Test void contentLengthIntHeader() { - response.addIntHeader("Content-Length", 66); + response.addIntHeader(CONTENT_LENGTH, 66); assertThat(response.getContentLength()).isEqualTo(66); - assertThat(response.getHeader("Content-Length")).isEqualTo("66"); + assertThat(response.getHeader(CONTENT_LENGTH)).isEqualTo("66"); } @Test @@ -173,7 +177,7 @@ class MockHttpServletResponseTests { response.addCookie(cookie); - assertThat(response.getHeader(HttpHeaders.SET_COOKIE)).isEqualTo(("foo=bar; Path=/path; Domain=example.com; " + + assertThat(response.getHeader(SET_COOKIE)).isEqualTo(("foo=bar; Path=/path; Domain=example.com; " + "Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; " + "Secure; HttpOnly")); } @@ -272,7 +276,7 @@ class MockHttpServletResponseTests { String redirectUrl = "/redirect"; response.sendRedirect(redirectUrl); assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_MOVED_TEMPORARILY); - assertThat(response.getHeader("Location")).isEqualTo(redirectUrl); + assertThat(response.getHeader(LOCATION)).isEqualTo(redirectUrl); assertThat(response.getRedirectedUrl()).isEqualTo(redirectUrl); assertThat(response.isCommitted()).isTrue(); } @@ -280,44 +284,43 @@ class MockHttpServletResponseTests { @Test void locationHeaderUpdatesGetRedirectedUrl() { String redirectUrl = "/redirect"; - response.setHeader("Location", redirectUrl); + response.setHeader(LOCATION, redirectUrl); assertThat(response.getRedirectedUrl()).isEqualTo(redirectUrl); } @Test void setDateHeader() { - response.setDateHeader("Last-Modified", 1437472800000L); - assertThat(response.getHeader("Last-Modified")).isEqualTo("Tue, 21 Jul 2015 10:00:00 GMT"); + response.setDateHeader(LAST_MODIFIED, 1437472800000L); + assertThat(response.getHeader(LAST_MODIFIED)).isEqualTo("Tue, 21 Jul 2015 10:00:00 GMT"); } @Test void addDateHeader() { - response.addDateHeader("Last-Modified", 1437472800000L); - response.addDateHeader("Last-Modified", 1437472801000L); - assertThat(response.getHeaders("Last-Modified").get(0)).isEqualTo("Tue, 21 Jul 2015 10:00:00 GMT"); - assertThat(response.getHeaders("Last-Modified").get(1)).isEqualTo("Tue, 21 Jul 2015 10:00:01 GMT"); + response.addDateHeader(LAST_MODIFIED, 1437472800000L); + response.addDateHeader(LAST_MODIFIED, 1437472801000L); + assertThat(response.getHeaders(LAST_MODIFIED).get(0)).isEqualTo("Tue, 21 Jul 2015 10:00:00 GMT"); + assertThat(response.getHeaders(LAST_MODIFIED).get(1)).isEqualTo("Tue, 21 Jul 2015 10:00:01 GMT"); } @Test void getDateHeader() { long time = 1437472800000L; - response.setDateHeader("Last-Modified", time); - assertThat(response.getHeader("Last-Modified")).isEqualTo("Tue, 21 Jul 2015 10:00:00 GMT"); - assertThat(response.getDateHeader("Last-Modified")).isEqualTo(time); + response.setDateHeader(LAST_MODIFIED, time); + assertThat(response.getHeader(LAST_MODIFIED)).isEqualTo("Tue, 21 Jul 2015 10:00:00 GMT"); + assertThat(response.getDateHeader(LAST_MODIFIED)).isEqualTo(time); } @Test void getInvalidDateHeader() { - response.setHeader("Last-Modified", "invalid"); - assertThat(response.getHeader("Last-Modified")).isEqualTo("invalid"); - assertThatIllegalArgumentException().isThrownBy(() -> - response.getDateHeader("Last-Modified")); + response.setHeader(LAST_MODIFIED, "invalid"); + assertThat(response.getHeader(LAST_MODIFIED)).isEqualTo("invalid"); + assertThatIllegalArgumentException().isThrownBy(() -> response.getDateHeader(LAST_MODIFIED)); } @Test // SPR-16160 void getNonExistentDateHeader() { - assertThat(response.getHeader("Last-Modified")).isNull(); - assertThat(response.getDateHeader("Last-Modified")).isEqualTo(-1); + assertThat(response.getHeader(LAST_MODIFIED)).isNull(); + assertThat(response.getDateHeader(LAST_MODIFIED)).isEqualTo(-1); } @Test // SPR-10414 @@ -331,7 +334,7 @@ class MockHttpServletResponseTests { @SuppressWarnings("deprecation") void modifyStatusMessageAfterSendError() throws IOException { response.sendError(HttpServletResponse.SC_NOT_FOUND); - response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Server Error"); + response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Server Error"); assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_FOUND); } @@ -340,12 +343,12 @@ class MockHttpServletResponseTests { */ @Test void setCookieHeader() { - response.setHeader(HttpHeaders.SET_COOKIE, "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax"); + response.setHeader(SET_COOKIE, "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax"); assertNumCookies(1); assertPrimarySessionCookie("123"); // Setting the Set-Cookie header a 2nd time should overwrite the previous value - response.setHeader(HttpHeaders.SET_COOKIE, "SESSION=999; Path=/; Secure; HttpOnly; SameSite=Lax"); + response.setHeader(SET_COOKIE, "SESSION=999; Path=/; Secure; HttpOnly; SameSite=Lax"); assertNumCookies(1); assertPrimarySessionCookie("999"); } @@ -357,9 +360,9 @@ class MockHttpServletResponseTests { void setCookieHeaderWithExpiresAttribute() { String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " + "HttpOnly; SameSite=Lax"; - response.setHeader(HttpHeaders.SET_COOKIE, cookieValue); + response.setHeader(SET_COOKIE, cookieValue); assertNumCookies(1); - assertThat(response.getHeader(HttpHeaders.SET_COOKIE)).isEqualTo(cookieValue); + assertThat(response.getHeader(SET_COOKIE)).isEqualTo(cookieValue); } /** @@ -368,9 +371,9 @@ class MockHttpServletResponseTests { @Test void setCookieHeaderWithZeroExpiresAttribute() { String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=0"; - response.setHeader(HttpHeaders.SET_COOKIE, cookieValue); + response.setHeader(SET_COOKIE, cookieValue); assertNumCookies(1); - String header = response.getHeader(HttpHeaders.SET_COOKIE); + String header = response.getHeader(SET_COOKIE); assertThat(header).isNotEqualTo(cookieValue); // We don't assert the actual Expires value since it is based on the current time. assertThat(header).startsWith("SESSION=123; Path=/; Max-Age=100; Expires="); @@ -378,12 +381,12 @@ class MockHttpServletResponseTests { @Test void addCookieHeader() { - response.addHeader(HttpHeaders.SET_COOKIE, "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax"); + response.addHeader(SET_COOKIE, "SESSION=123; Path=/; Secure; HttpOnly; SameSite=Lax"); assertNumCookies(1); assertPrimarySessionCookie("123"); // Adding a 2nd cookie header should result in 2 cookies. - response.addHeader(HttpHeaders.SET_COOKIE, "SESSION=999; Path=/; Secure; HttpOnly; SameSite=Lax"); + response.addHeader(SET_COOKIE, "SESSION=999; Path=/; Secure; HttpOnly; SameSite=Lax"); assertNumCookies(2); assertPrimarySessionCookie("123"); assertCookieValues("123", "999"); @@ -396,8 +399,8 @@ class MockHttpServletResponseTests { void addCookieHeaderWithExpiresAttribute() { String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=Tue, 8 Oct 2019 19:50:00 GMT; Secure; " + "HttpOnly; SameSite=Lax"; - response.addHeader(HttpHeaders.SET_COOKIE, cookieValue); - assertThat(response.getHeader(HttpHeaders.SET_COOKIE)).isEqualTo(cookieValue); + response.addHeader(SET_COOKIE, cookieValue); + assertThat(response.getHeader(SET_COOKIE)).isEqualTo(cookieValue); } /** @@ -406,9 +409,9 @@ class MockHttpServletResponseTests { @Test void addCookieHeaderWithZeroExpiresAttribute() { String cookieValue = "SESSION=123; Path=/; Max-Age=100; Expires=0"; - response.addHeader(HttpHeaders.SET_COOKIE, cookieValue); + response.addHeader(SET_COOKIE, cookieValue); assertNumCookies(1); - String header = response.getHeader(HttpHeaders.SET_COOKIE); + String header = response.getHeader(SET_COOKIE); assertThat(header).isNotEqualTo(cookieValue); // We don't assert the actual Expires value since it is based on the current time. assertThat(header).startsWith("SESSION=123; Path=/; Max-Age=100; Expires="); @@ -427,7 +430,7 @@ class MockHttpServletResponseTests { response.addCookie(mockCookie); assertNumCookies(1); - assertThat(response.getHeader(HttpHeaders.SET_COOKIE)).isEqualTo(("SESSION=123; Path=/; Domain=example.com; Max-Age=0; " + + assertThat(response.getHeader(SET_COOKIE)).isEqualTo(("SESSION=123; Path=/; Domain=example.com; Max-Age=0; " + "Expires=Thu, 01 Jan 1970 00:00:00 GMT; Secure; HttpOnly; SameSite=Lax")); // Adding a 2nd Cookie should result in 2 Cookies. From 89bb9cb74915f1c79dc5f87d74b31a59bd65d242 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 27 Jul 2020 14:38:24 +0200 Subject: [PATCH 2/2] Polish Javadoc --- .../mock/web/MockHttpServletResponse.java | 8 ++++---- .../java/org/springframework/http/HttpHeaders.java | 11 ++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java index a1d5beefcd6..95f6092ebc5 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -424,7 +424,7 @@ public class MockHttpServletResponse implements HttpServletResponse { /** * Return the names of all specified headers as a Set of Strings. - *

As of Servlet 3.0, this method is also defined HttpServletResponse. + *

As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}. * @return the {@code Set} of header name {@code Strings}, or an empty {@code Set} if none */ @Override @@ -435,7 +435,7 @@ public class MockHttpServletResponse implements HttpServletResponse { /** * Return the primary value for the given header as a String, if any. * Will return the first value in case of multiple values. - *

As of Servlet 3.0, this method is also defined in HttpServletResponse. + *

As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}. * As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility. * Consider using {@link #getHeaderValue(String)} for raw Object access. * @param name the name of the header @@ -450,7 +450,7 @@ public class MockHttpServletResponse implements HttpServletResponse { /** * Return all values for the given header as a List of Strings. - *

As of Servlet 3.0, this method is also defined in HttpServletResponse. + *

As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}. * As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility. * Consider using {@link #getHeaderValues(String)} for raw Object access. * @param name the name of the header diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index 6a6f9db9db3..aaa65b5e449 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -906,11 +906,12 @@ public class HttpHeaders implements MultiValueMap, Serializable } /** - * Return the first {@link Locale} of the content languages, - * as specified by the {@literal Content-Language} header. - *

Returns {@code null} when the content language is unknown. - *

Use {@code getValuesAsList(CONTENT_LANGUAGE)} if you need - * to get multiple content languages.

+ * Get the first {@link Locale} of the content languages, as specified by the + * {@code Content-Language} header. + *

Use {@link #getValuesAsList(String)} if you need to get multiple content + * languages. + * @return the first {@code Locale} of the content languages, or {@code null} + * if unknown * @since 5.0 */ @Nullable