Improve charset handling in MockHttpServletResponse

This commit adds a getContentAsString(Charset fallbackCharset) method
to MockHttpServletResponse in order to make it easier to get the content
in a specific charset like UTF-8 when the response charset has not been
explicitly set (by default ISO-8859-1 is used).

JsonPathResultMatchers leverages this new feature to support UTF-8
content out of the box.

Closes gh-23219
This commit is contained in:
Sebastien Deleuze
2019-07-16 11:42:39 +02:00
parent 60a7092977
commit adadffe0e1
4 changed files with 47 additions and 6 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.mock.web;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import javax.servlet.http.Cookie;
@@ -39,6 +40,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* @author Rob Winch
* @author Sam Brannen
* @author Brian Clozel
* @author Sebastien Deleuze
* @since 19.02.2006
*/
public class MockHttpServletResponseTests {
@@ -238,10 +240,11 @@ public class MockHttpServletResponseTests {
assertThat(response.getContentAsByteArray().length).isEqualTo(1);
}
@Test
public void servletWriterAutoFlushedForString() throws IOException {
response.getWriter().write("X");
assertThat(response.getContentAsString()).isEqualTo("X");
@Test // gh-23219
public void contentAsUtf8() throws IOException {
String content = "Příliš žluťoučký kůň úpěl ďábelské ódy";
response.getOutputStream().write(content.getBytes(StandardCharsets.UTF_8));
assertThat(response.getContentAsString(StandardCharsets.UTF_8)).isEqualTo(content);
}
@Test
@@ -256,6 +259,12 @@ public class MockHttpServletResponseTests {
assertThat(response.getContentAsString()).isEqualTo("XY");
}
@Test
public void servletWriterAutoFlushedForString() throws IOException {
response.getWriter().write("X");
assertThat(response.getContentAsString()).isEqualTo("X");
}
@Test
public void sendRedirect() throws IOException {
String redirectUrl = "/redirect";

View File

@@ -16,6 +16,8 @@
package org.springframework.test.web.servlet.result;
import java.nio.charset.StandardCharsets;
import org.hamcrest.Matchers;
import org.junit.Test;
@@ -31,11 +33,13 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* @author Craig Andrews
* @author Sam Brannen
* @author Brian Clozel
* @author Sebastien Deleuze
*/
public class JsonPathResultMatchersTests {
private static final String RESPONSE_CONTENT = "{" + //
"'str': 'foo', " + //
"'utf8Str': 'Příliš', " + //
"'num': 5, " + //
"'bool': true, " + //
"'arr': [42], " + //
@@ -51,7 +55,7 @@ public class JsonPathResultMatchersTests {
try {
MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader("Content-Type", "application/json");
response.getWriter().print(new String(RESPONSE_CONTENT.getBytes("ISO-8859-1")));
response.getOutputStream().write(RESPONSE_CONTENT.getBytes(StandardCharsets.UTF_8));
stubMvcResult = new StubMvcResult(null, null, null, null, null, null, response);
}
catch (Exception e) {
@@ -70,6 +74,11 @@ public class JsonPathResultMatchersTests {
new JsonPathResultMatchers("$.str").value("foo").match(stubMvcResult);
}
@Test // gh-23219
public void utf8ValueWithDirectMatch() throws Exception {
new JsonPathResultMatchers("$.utf8Str").value("Příliš").match(stubMvcResult);
}
@Test // SPR-16587
public void valueWithNumberConversion() throws Exception {
new JsonPathResultMatchers("$.num").value(5.0f).match(stubMvcResult);