From b2b79ae1e634259c7d71cc09127c4e3f97b251f2 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 16 Jul 2019 14:53:51 +0200 Subject: [PATCH] Polish contribution See gh-23219 --- .../mock/web/MockHttpServletResponse.java | 4 +++- .../web/test/MockHttpServletResponse.java | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) 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 77c5f4c11a..195b8a94e3 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 @@ -56,6 +56,7 @@ import org.springframework.web.util.WebUtils; * @author Rod Johnson * @author Brian Clozel * @author Vedran Pavic + * @author Sebastien Deleuze * @since 1.0.2 */ public class MockHttpServletResponse implements HttpServletResponse { @@ -219,10 +220,11 @@ public class MockHttpServletResponse implements HttpServletResponse { /** * Get the content of the response body as a {@code String}, using the provided - * {@code fallbackCharset} if no charset has been explicitly defined, else using + * {@code fallbackCharset} if no charset has been explicitly defined and otherwise * using the configured {@linkplain #getCharacterEncoding character encoding}. * @return the content as a {@code String} * @throws UnsupportedEncodingException if the character encoding is not supported + * @since 5.2 * @see #getContentAsString() */ public String getContentAsString(Charset fallbackCharset) throws UnsupportedEncodingException { diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java index b7e47742bd..bb1240f26e 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java @@ -23,6 +23,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; +import java.nio.charset.Charset; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -55,6 +56,7 @@ import org.springframework.web.util.WebUtils; * @author Rod Johnson * @author Brian Clozel * @author Vedran Pavic + * @author Sebastien Deleuze * @since 1.0.2 */ public class MockHttpServletResponse implements HttpServletResponse { @@ -204,11 +206,33 @@ public class MockHttpServletResponse implements HttpServletResponse { return this.content.toByteArray(); } + /** + * Get the content of the response body as a {@code String}, using the configured + * {@linkplain #getCharacterEncoding character encoding}. + * @return the content as a {@code String} + * @throws UnsupportedEncodingException if the character encoding is not supported + * @see #getContentAsString(Charset) + */ public String getContentAsString() throws UnsupportedEncodingException { return (this.characterEncoding != null ? this.content.toString(this.characterEncoding) : this.content.toString()); } + /** + * Get the content of the response body as a {@code String}, using the provided + * {@code fallbackCharset} if no charset has been explicitly defined and otherwise + * using the configured {@linkplain #getCharacterEncoding character encoding}. + * @return the content as a {@code String} + * @throws UnsupportedEncodingException if the character encoding is not supported + * @since 5.2 + * @see #getContentAsString() + */ + public String getContentAsString(Charset fallbackCharset) throws UnsupportedEncodingException { + return isCharset() ? + this.content.toString(this.characterEncoding) : + this.content.toString(fallbackCharset.name()); + } + @Override public void setContentLength(int contentLength) { this.contentLength = contentLength; -- GitLab