diff --git a/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java index d4eef94bac..ee76368ab9 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java @@ -24,6 +24,7 @@ import org.junit.jupiter.api.Test; import org.springframework.http.MediaType; import org.springframework.mock.http.client.MockClientHttpRequest; +import org.springframework.test.json.JsonCompareMode; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -234,6 +235,16 @@ public class ContentRequestMatchersTests { MockRestRequestMatchers.content().json("{\n \"foo array\":[\"second\",\"first\"] \n}") .match(this.request); + MockRestRequestMatchers.content().json("{\n \"foo array\":[\"second\",\"first\"] \n}", JsonCompareMode.LENIENT) + .match(this.request); + } + + @Test + @Deprecated + public void testJsonLenientMatchWithDeprecatedBooleanFlag() throws Exception { + String content = "{\n \"foo array\":[\"first\",\"second\"] , \"someExtraProperty\": \"which is allowed\" \n}"; + this.request.getBody().write(content.getBytes()); + MockRestRequestMatchers.content().json("{\n \"foo array\":[\"second\",\"first\"] \n}", false) .match(this.request); } @@ -243,6 +254,18 @@ public class ContentRequestMatchersTests { String content = "{\n \"foo\": \"bar\", \"foo array\":[\"first\",\"second\"] \n}"; this.request.getBody().write(content.getBytes()); + MockRestRequestMatchers + .content() + .json("{\n \"foo array\":[\"first\",\"second\"] , \"foo\": \"bar\" \n}", JsonCompareMode.STRICT) + .match(this.request); + } + + @Test + @Deprecated + public void testJsonStrictMatchWithDeprecatedBooleanFlag() throws Exception { + String content = "{\n \"foo\": \"bar\", \"foo array\":[\"first\",\"second\"] \n}"; + this.request.getBody().write(content.getBytes()); + MockRestRequestMatchers .content() .json("{\n \"foo array\":[\"first\",\"second\"] , \"foo\": \"bar\" \n}", true) @@ -259,6 +282,19 @@ public class ContentRequestMatchersTests { .content() .json("{\n \"foo\" : \"bar\" \n}") .match(this.request)); + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> + MockRestRequestMatchers + .content() + .json("{\n \"foo\" : \"bar\" \n}", JsonCompareMode.LENIENT) + .match(this.request)); + } + + @Test + @Deprecated + public void testJsonLenientNoMatchWithDeprecatedBooleanFlag() throws Exception { + String content = "{\n \"bar\" : \"foo\" \n}"; + this.request.getBody().write(content.getBytes()); + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers .content() @@ -271,6 +307,19 @@ public class ContentRequestMatchersTests { String content = "{\n \"foo array\":[\"first\",\"second\"] , \"someExtraProperty\": \"which is NOT allowed\" \n}"; this.request.getBody().write(content.getBytes()); + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> + MockRestRequestMatchers + .content() + .json("{\n \"foo array\":[\"second\",\"first\"] \n}", JsonCompareMode.STRICT) + .match(this.request)); + } + + @Test + @Deprecated + public void testJsonStrictNoMatchWithDeprecatedBooleanFlag() throws Exception { + String content = "{\n \"foo array\":[\"first\",\"second\"] , \"someExtraProperty\": \"which is NOT allowed\" \n}"; + this.request.getBody().write(content.getBytes()); + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> MockRestRequestMatchers .content() 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 fa98d3d1ea..4c28ae7fb9 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 @@ -20,6 +20,7 @@ import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.test.json.JsonCompareMode; import org.springframework.test.web.servlet.StubMvcResult; import static java.nio.charset.StandardCharsets.UTF_8; @@ -80,13 +81,31 @@ class ContentResultMatchersTests { @Test void jsonLenientMatch() throws Exception { new ContentResultMatchers().json("{\n \"foo\" : \"bar\" \n}").match(getStubMvcResult(CONTENT)); + new ContentResultMatchers().json("{\n \"foo\" : \"bar\" \n}", + JsonCompareMode.LENIENT).match(getStubMvcResult(CONTENT)); + } + + @Test + @Deprecated + void jsonLenientMatchWithDeprecatedBooleanFlag() throws Exception { new ContentResultMatchers().json("{\n \"foo\" : \"bar\" \n}", false).match(getStubMvcResult(CONTENT)); } @Test void jsonStrictMatch() throws Exception { - 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)); + new ContentResultMatchers().json("{\n \"foo\":\"bar\", \"foo array\":[\"foo\",\"bar\"] \n}", + JsonCompareMode.STRICT).match(getStubMvcResult(CONTENT)); + new ContentResultMatchers().json("{\n \"foo array\":[\"foo\",\"bar\"], \"foo\":\"bar\" \n}", + JsonCompareMode.STRICT).match(getStubMvcResult(CONTENT)); + } + + @Test + @Deprecated + void jsonStrictMatchWithDeprecatedBooleanFlag() throws Exception { + 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 @@ -98,7 +117,16 @@ class ContentResultMatchersTests { @Test void jsonStrictNoMatch() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> - new ContentResultMatchers().json("{\"foo\":\"bar\", \"foo array\":[\"bar\",\"foo\"]}", true).match(getStubMvcResult(CONTENT))); + new ContentResultMatchers().json("{\"foo\":\"bar\", \"foo array\":[\"bar\",\"foo\"]}", + JsonCompareMode.STRICT).match(getStubMvcResult(CONTENT))); + } + + @Test + @Deprecated + void jsonStrictNoMatchWithDeprecatedBooleanFlag() { + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> + new ContentResultMatchers().json("{\"foo\":\"bar\", \"foo array\":[\"bar\",\"foo\"]}", true) + .match(getStubMvcResult(CONTENT))); } @Test // gh-23622