|
23 | 23 | import java.io.PrintWriter;
|
24 | 24 | import java.io.UnsupportedEncodingException;
|
25 | 25 | import java.io.Writer;
|
| 26 | +import java.nio.charset.Charset; |
26 | 27 | import java.text.DateFormat;
|
27 | 28 | import java.text.ParseException;
|
28 | 29 | import java.text.SimpleDateFormat;
|
|
55 | 56 | * @author Rod Johnson
|
56 | 57 | * @author Brian Clozel
|
57 | 58 | * @author Vedran Pavic
|
| 59 | + * @author Sebastien Deleuze |
58 | 60 | * @since 1.0.2
|
59 | 61 | */
|
60 | 62 | public class MockHttpServletResponse implements HttpServletResponse {
|
@@ -204,11 +206,33 @@ public byte[] getContentAsByteArray() {
|
204 | 206 | return this.content.toByteArray();
|
205 | 207 | }
|
206 | 208 |
|
| 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 | + */ |
207 | 216 | public String getContentAsString() throws UnsupportedEncodingException {
|
208 | 217 | return (this.characterEncoding != null ?
|
209 | 218 | this.content.toString(this.characterEncoding) : this.content.toString());
|
210 | 219 | }
|
211 | 220 |
|
| 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 | + |
212 | 236 | @Override
|
213 | 237 | public void setContentLength(int contentLength) {
|
214 | 238 | this.contentLength = contentLength;
|
|
0 commit comments