Browse Source

Fix UTF-8 handling in ContentResultMatchers

Closes gh-23622
pull/23567/head
Sebastien Deleuze 6 years ago
parent
commit
7b4b64b8fb
  1. 3
      spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java
  2. 56
      spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java

3
spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.test.web.servlet.result;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
@ -210,7 +211,7 @@ public class ContentResultMatchers { @@ -210,7 +211,7 @@ public class ContentResultMatchers {
*/
public ResultMatcher json(String jsonContent, boolean strict) {
return result -> {
String content = result.getResponse().getContentAsString();
String content = result.getResponse().getContentAsString(StandardCharsets.UTF_8);
this.jsonHelper.assertJsonEqual(jsonContent, content, strict);
};
}

56
spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java

@ -16,9 +16,12 @@ @@ -16,9 +16,12 @@
package org.springframework.test.web.servlet.result;
import java.nio.charset.StandardCharsets;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.StubMvcResult;
@ -26,95 +29,92 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -26,95 +29,92 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
*/
public class ContentResultMatchersTests {
@Test
public void typeMatches() throws Exception {
new ContentResultMatchers().contentType("application/json;charset=UTF-8").match(getStubMvcResult());
new ContentResultMatchers().contentType(MediaType.APPLICATION_JSON_VALUE).match(getStubMvcResult(CONTENT));
}
@Test
public void typeNoMatch() throws Exception {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new ContentResultMatchers().contentType("text/plain").match(getStubMvcResult()));
}
@Test
public void encoding() throws Exception {
new ContentResultMatchers().encoding("UTF-8").match(getStubMvcResult());
}
@Test
public void encodingNoMatch() throws Exception {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new ContentResultMatchers().encoding("ISO-8859-1").match(getStubMvcResult()));
new ContentResultMatchers().contentType("text/plain").match(getStubMvcResult(CONTENT)));
}
@Test
public void string() throws Exception {
new ContentResultMatchers().string(new String(CONTENT.getBytes("UTF-8"))).match(getStubMvcResult());
new ContentResultMatchers().string(new String(CONTENT.getBytes("UTF-8"))).match(getStubMvcResult(CONTENT));
}
@Test
public void stringNoMatch() throws Exception {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new ContentResultMatchers().encoding("bogus").match(getStubMvcResult()));
new ContentResultMatchers().encoding("bogus").match(getStubMvcResult(CONTENT)));
}
@Test
public void stringMatcher() throws Exception {
String content = new String(CONTENT.getBytes("UTF-8"));
new ContentResultMatchers().string(Matchers.equalTo(content)).match(getStubMvcResult());
new ContentResultMatchers().string(Matchers.equalTo(content)).match(getStubMvcResult(CONTENT));
}
@Test
public void stringMatcherNoMatch() throws Exception {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new ContentResultMatchers().string(Matchers.equalTo("bogus")).match(getStubMvcResult()));
new ContentResultMatchers().string(Matchers.equalTo("bogus")).match(getStubMvcResult(CONTENT)));
}
@Test
public void bytes() throws Exception {
new ContentResultMatchers().bytes(CONTENT.getBytes("UTF-8")).match(getStubMvcResult());
new ContentResultMatchers().bytes(CONTENT.getBytes("UTF-8")).match(getStubMvcResult(CONTENT));
}
@Test
public void bytesNoMatch() throws Exception {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new ContentResultMatchers().bytes("bogus".getBytes()).match(getStubMvcResult()));
new ContentResultMatchers().bytes("bogus".getBytes()).match(getStubMvcResult(CONTENT)));
}
@Test
public void jsonLenientMatch() throws Exception {
new ContentResultMatchers().json("{\n \"foo\" : \"bar\" \n}").match(getStubMvcResult());
new ContentResultMatchers().json("{\n \"foo\" : \"bar\" \n}", false).match(getStubMvcResult());
new ContentResultMatchers().json("{\n \"foo\" : \"bar\" \n}").match(getStubMvcResult(CONTENT));
new ContentResultMatchers().json("{\n \"foo\" : \"bar\" \n}", false).match(getStubMvcResult(CONTENT));
}
@Test
public void jsonStrictMatch() throws Exception {
new ContentResultMatchers().json("{\n \"foo\":\"bar\", \"foo array\":[\"foo\",\"bar\"] \n}", true).match(getStubMvcResult());
new ContentResultMatchers().json("{\n \"foo array\":[\"foo\",\"bar\"], \"foo\":\"bar\" \n}", true).match(getStubMvcResult());
new ContentResultMatchers().json("{\n \"foo\":\"bar\", \"foo array\":[\"foo\",\"bar\"] \n}", true).match(getStubMvcResult(CONTENT));
new ContentResultMatchers().json("{\n \"foo array\":[\"foo\",\"bar\"], \"foo\":\"bar\" \n}", true).match(getStubMvcResult(CONTENT));
}
@Test
public void jsonLenientNoMatch() throws Exception {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new ContentResultMatchers().json("{\n\"fooo\":\"bar\"\n}").match(getStubMvcResult()));
new ContentResultMatchers().json("{\n\"fooo\":\"bar\"\n}").match(getStubMvcResult(CONTENT)));
}
@Test
public void jsonStrictNoMatch() throws Exception {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
new ContentResultMatchers().json("{\"foo\":\"bar\", \"foo array\":[\"bar\",\"foo\"]}", true).match(getStubMvcResult()));
new ContentResultMatchers().json("{\"foo\":\"bar\", \"foo array\":[\"bar\",\"foo\"]}", true).match(getStubMvcResult(CONTENT)));
}
@Test // gh-23622
public void jsonUtf8Match() throws Exception {
new ContentResultMatchers().json("{\"name\":\"Jürgen\"}").match(getStubMvcResult(UTF8_CONTENT));
}
private static final String CONTENT = "{\"foo\":\"bar\",\"foo array\":[\"foo\",\"bar\"]}";
private StubMvcResult getStubMvcResult() throws Exception {
private static final String UTF8_CONTENT = "{\"name\":\"Jürgen\"}";
private StubMvcResult getStubMvcResult(String content) throws Exception {
MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader("Content-Type", "application/json; charset=UTF-8");
response.getWriter().print(new String(CONTENT.getBytes("UTF-8")));
response.addHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
response.getOutputStream().write(content.getBytes(StandardCharsets.UTF_8));
return new StubMvcResult(null, null, null, null, null, null, response);
}

Loading…
Cancel
Save