From 14b1d20822a2b7d4deb330ed2a4a4d54765be7f6 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 7 Aug 2020 14:50:36 +0200 Subject: [PATCH] Reset charset field in MockHttpServletResponse Closes gh-25501 --- .../mock/web/MockHttpServletResponse.java | 20 ++++++++++--------- .../web/test/MockHttpServletResponse.java | 20 ++++++++++--------- 2 files changed, 22 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 fc48384cae10..de097a30da45 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-2018 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. @@ -167,11 +167,11 @@ public void setCharacterEncoding(String characterEncoding) { private void updateContentTypeHeader() { if (this.contentType != null) { - StringBuilder sb = new StringBuilder(this.contentType); - if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.charset) { - sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); + String value = this.contentType; + if (this.charset && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) { + value = value + ';' + CHARSET_PREFIX + this.characterEncoding; } - doAddHeaderValue(HttpHeaders.CONTENT_TYPE, sb.toString(), true); + doAddHeaderValue(HttpHeaders.CONTENT_TYPE, value, true); } } @@ -192,7 +192,8 @@ public PrintWriter getWriter() throws UnsupportedEncodingException { Assert.state(this.writerAccessAllowed, "Writer access not allowed"); if (this.writer == null) { Writer targetWriter = (this.characterEncoding != null ? - new OutputStreamWriter(this.content, this.characterEncoding) : new OutputStreamWriter(this.content)); + new OutputStreamWriter(this.content, this.characterEncoding) : + new OutputStreamWriter(this.content)); this.writer = new ResponsePrintWriter(targetWriter); } return this.writer; @@ -297,6 +298,7 @@ public boolean isCommitted() { public void reset() { resetBuffer(); this.characterEncoding = null; + this.charset = false; this.contentLength = 0; this.contentType = null; this.locale = Locale.getDefault(); @@ -378,7 +380,7 @@ public boolean containsHeader(String name) { /** * 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 @@ -389,7 +391,7 @@ public Collection getHeaderNames() { /** * 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 @@ -404,7 +406,7 @@ public String getHeader(String name) { /** * 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/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java index 4011a2b1b0cd..a882a9ae18c7 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 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. @@ -161,11 +161,11 @@ public void setCharacterEncoding(String characterEncoding) { private void updateContentTypeHeader() { if (this.contentType != null) { - StringBuilder sb = new StringBuilder(this.contentType); - if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.charset) { - sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding); + String value = this.contentType; + if (this.charset && !this.contentType.toLowerCase().contains(CHARSET_PREFIX)) { + value = value + ';' + CHARSET_PREFIX + this.characterEncoding; } - doAddHeaderValue(HttpHeaders.CONTENT_TYPE, sb.toString(), true); + doAddHeaderValue(HttpHeaders.CONTENT_TYPE, value, true); } } @@ -185,7 +185,8 @@ public PrintWriter getWriter() throws UnsupportedEncodingException { Assert.state(this.writerAccessAllowed, "Writer access not allowed"); if (this.writer == null) { Writer targetWriter = (this.characterEncoding != null ? - new OutputStreamWriter(this.content, this.characterEncoding) : new OutputStreamWriter(this.content)); + new OutputStreamWriter(this.content, this.characterEncoding) : + new OutputStreamWriter(this.content)); this.writer = new ResponsePrintWriter(targetWriter); } return this.writer; @@ -289,6 +290,7 @@ public boolean isCommitted() { public void reset() { resetBuffer(); this.characterEncoding = null; + this.charset = false; this.contentLength = 0; this.contentType = null; this.locale = Locale.getDefault(); @@ -369,7 +371,7 @@ public boolean containsHeader(String name) { /** * 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 @@ -380,7 +382,7 @@ public Collection getHeaderNames() { /** * 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 @@ -394,7 +396,7 @@ public String getHeader(String name) { /** * 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