From 7b4b64b8fbc116d09a95180f3729ec6f39fe6b9e Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Fri, 13 Sep 2019 11:43:10 +0200 Subject: [PATCH] Fix UTF-8 handling in ContentResultMatchers Closes gh-23622 --- .../servlet/result/ContentResultMatchers.java | 3 +- .../result/ContentResultMatchersTests.java | 56 +++++++++---------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java index fc77ef33bb1..87232ae45bc 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/ContentResultMatchers.java @@ -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 { */ 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); }; } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java index 89cd56743da..6884bdf15f1 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/result/ContentResultMatchersTests.java @@ -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; /** * @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); }