From 403e04c0b4a79c68b4caf69aabcc9fd0c7bbbd3c Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 28 Jul 2021 10:47:36 +0200 Subject: [PATCH] Polish MockHttpServletResponse --- .../mock/web/MockHttpServletResponse.java | 31 +++++++++++++------ .../servlet/MockHttpServletResponse.java | 31 +++++++++++++------ 2 files changed, 44 insertions(+), 18 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 b7b6b9626a..4182597f33 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 @@ -83,7 +83,12 @@ public class MockHttpServletResponse implements HttpServletResponse { @Nullable private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING; - private boolean charset = false; + /** + * {@code true} if the character encoding has been explicitly set through + * {@link HttpServletResponse} methods or through a {@code charset} parameter + * on the {@code Content-Type}. + */ + private boolean characterEncodingSet = false; private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024); @@ -117,6 +122,11 @@ public class MockHttpServletResponse implements HttpServletResponse { @Nullable private String errorMessage; + + //--------------------------------------------------------------------- + // Properties for MockRequestDispatcher + //--------------------------------------------------------------------- + @Nullable private String forwardedUrl; @@ -158,24 +168,27 @@ public class MockHttpServletResponse implements HttpServletResponse { } /** - * Return whether the character encoding has been set. - *

If {@code false}, {@link #getCharacterEncoding()} will return a default encoding value. + * Determine whether the character encoding has been explicitly set through + * {@link HttpServletResponse} methods or through a {@code charset} parameter + * on the {@code Content-Type}. + *

If {@code false}, {@link #getCharacterEncoding()} will return a default + * encoding value. */ public boolean isCharset() { - return this.charset; + return this.characterEncodingSet; } @Override public void setCharacterEncoding(String characterEncoding) { this.characterEncoding = characterEncoding; - this.charset = true; + this.characterEncodingSet = true; updateContentTypePropertyAndHeader(); } private void updateContentTypePropertyAndHeader() { if (this.contentType != null) { String value = this.contentType; - if (this.charset && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) { + if (this.characterEncodingSet && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) { value = value + ';' + CHARSET_PREFIX + this.characterEncoding; this.contentType = value; } @@ -270,7 +283,7 @@ public class MockHttpServletResponse implements HttpServletResponse { MediaType mediaType = MediaType.parseMediaType(contentType); if (mediaType.getCharset() != null) { this.characterEncoding = mediaType.getCharset().name(); - this.charset = true; + this.characterEncodingSet = true; } } catch (Exception ex) { @@ -278,7 +291,7 @@ public class MockHttpServletResponse implements HttpServletResponse { int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); if (charsetIndex != -1) { this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); - this.charset = true; + this.characterEncodingSet = true; } } updateContentTypePropertyAndHeader(); @@ -332,7 +345,7 @@ public class MockHttpServletResponse implements HttpServletResponse { public void reset() { resetBuffer(); this.characterEncoding = null; - this.charset = false; + this.characterEncodingSet = false; this.contentLength = 0; this.contentType = null; this.locale = Locale.getDefault(); diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java index f3215a88b2..e357bc3ce1 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java @@ -83,7 +83,12 @@ public class MockHttpServletResponse implements HttpServletResponse { @Nullable private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING; - private boolean charset = false; + /** + * {@code true} if the character encoding has been explicitly set through + * {@link HttpServletResponse} methods or through a {@code charset} parameter + * on the {@code Content-Type}. + */ + private boolean characterEncodingSet = false; private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024); @@ -117,6 +122,11 @@ public class MockHttpServletResponse implements HttpServletResponse { @Nullable private String errorMessage; + + //--------------------------------------------------------------------- + // Properties for MockRequestDispatcher + //--------------------------------------------------------------------- + @Nullable private String forwardedUrl; @@ -158,24 +168,27 @@ public class MockHttpServletResponse implements HttpServletResponse { } /** - * Return whether the character encoding has been set. - *

If {@code false}, {@link #getCharacterEncoding()} will return a default encoding value. + * Determine whether the character encoding has been explicitly set through + * {@link HttpServletResponse} methods or through a {@code charset} parameter + * on the {@code Content-Type}. + *

If {@code false}, {@link #getCharacterEncoding()} will return a default + * encoding value. */ public boolean isCharset() { - return this.charset; + return this.characterEncodingSet; } @Override public void setCharacterEncoding(String characterEncoding) { this.characterEncoding = characterEncoding; - this.charset = true; + this.characterEncodingSet = true; updateContentTypePropertyAndHeader(); } private void updateContentTypePropertyAndHeader() { if (this.contentType != null) { String value = this.contentType; - if (this.charset && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) { + if (this.characterEncodingSet && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) { value = value + ';' + CHARSET_PREFIX + this.characterEncoding; this.contentType = value; } @@ -270,7 +283,7 @@ public class MockHttpServletResponse implements HttpServletResponse { MediaType mediaType = MediaType.parseMediaType(contentType); if (mediaType.getCharset() != null) { this.characterEncoding = mediaType.getCharset().name(); - this.charset = true; + this.characterEncodingSet = true; } } catch (Exception ex) { @@ -278,7 +291,7 @@ public class MockHttpServletResponse implements HttpServletResponse { int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); if (charsetIndex != -1) { this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); - this.charset = true; + this.characterEncodingSet = true; } } updateContentTypePropertyAndHeader(); @@ -332,7 +345,7 @@ public class MockHttpServletResponse implements HttpServletResponse { public void reset() { resetBuffer(); this.characterEncoding = null; - this.charset = false; + this.characterEncodingSet = false; this.contentLength = 0; this.contentType = null; this.locale = Locale.getDefault();