Skip to content

Commit b2b79ae

Browse files
committed
Polish contribution
See spring-projectsgh-23219
1 parent adadffe commit b2b79ae

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
* @author Rod Johnson
5757
* @author Brian Clozel
5858
* @author Vedran Pavic
59+
* @author Sebastien Deleuze
5960
* @since 1.0.2
6061
*/
6162
public class MockHttpServletResponse implements HttpServletResponse {
@@ -219,10 +220,11 @@ public String getContentAsString() throws UnsupportedEncodingException {
219220

220221
/**
221222
* Get the content of the response body as a {@code String}, using the provided
222-
* {@code fallbackCharset} if no charset has been explicitly defined, else using
223+
* {@code fallbackCharset} if no charset has been explicitly defined and otherwise
223224
* using the configured {@linkplain #getCharacterEncoding character encoding}.
224225
* @return the content as a {@code String}
225226
* @throws UnsupportedEncodingException if the character encoding is not supported
227+
* @since 5.2
226228
* @see #getContentAsString()
227229
*/
228230
public String getContentAsString(Charset fallbackCharset) throws UnsupportedEncodingException {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.PrintWriter;
2424
import java.io.UnsupportedEncodingException;
2525
import java.io.Writer;
26+
import java.nio.charset.Charset;
2627
import java.text.DateFormat;
2728
import java.text.ParseException;
2829
import java.text.SimpleDateFormat;
@@ -55,6 +56,7 @@
5556
* @author Rod Johnson
5657
* @author Brian Clozel
5758
* @author Vedran Pavic
59+
* @author Sebastien Deleuze
5860
* @since 1.0.2
5961
*/
6062
public class MockHttpServletResponse implements HttpServletResponse {
@@ -204,11 +206,33 @@ public byte[] getContentAsByteArray() {
204206
return this.content.toByteArray();
205207
}
206208

209+
/**
210+
* Get the content of the response body as a {@code String}, using the configured
211+
* {@linkplain #getCharacterEncoding character encoding}.
212+
* @return the content as a {@code String}
213+
* @throws UnsupportedEncodingException if the character encoding is not supported
214+
* @see #getContentAsString(Charset)
215+
*/
207216
public String getContentAsString() throws UnsupportedEncodingException {
208217
return (this.characterEncoding != null ?
209218
this.content.toString(this.characterEncoding) : this.content.toString());
210219
}
211220

221+
/**
222+
* Get the content of the response body as a {@code String}, using the provided
223+
* {@code fallbackCharset} if no charset has been explicitly defined and otherwise
224+
* using the configured {@linkplain #getCharacterEncoding character encoding}.
225+
* @return the content as a {@code String}
226+
* @throws UnsupportedEncodingException if the character encoding is not supported
227+
* @since 5.2
228+
* @see #getContentAsString()
229+
*/
230+
public String getContentAsString(Charset fallbackCharset) throws UnsupportedEncodingException {
231+
return isCharset() ?
232+
this.content.toString(this.characterEncoding) :
233+
this.content.toString(fallbackCharset.name());
234+
}
235+
212236
@Override
213237
public void setContentLength(int contentLength) {
214238
this.contentLength = contentLength;

0 commit comments

Comments
 (0)