/** * Static method for matching with an array of result matchers. * @param matchers the matchers * @since 5.1 */ static ResultMatcher matchAll(ResultMatcher... matchers) { return result -> { for (ResultMatcher matcher : matchers) { matcher.match(result); } }; }
@Override public ResultActions andExpect(ResultMatcher matcher) throws Exception { matcher.match(mvcResult); return this; } @Override
private void applyDefaultResultActions(MvcResult mvcResult) throws Exception { for (ResultMatcher matcher : this.defaultResultMatchers) { matcher.match(mvcResult); } for (ResultHandler handler : this.defaultResultHandlers) { handler.handle(mvcResult); } }
@Test // SPR-17330 public void matchDateFormattedWithHttpHeaders() throws Exception { long epochMilli = ZonedDateTime.of(2018, 10, 5, 0, 0, 0, 0, ZoneId.of("GMT")).toInstant().toEpochMilli(); HttpHeaders headers = new HttpHeaders(); headers.setDate("myDate", epochMilli); this.response.setHeader("d", headers.getFirst("myDate")); this.matchers.dateValue("d", epochMilli).match(this.mvcResult); }
@Test public void stringMatcher() throws Exception { String content = new String(CONTENT.getBytes("UTF-8")); new ContentResultMatchers().string(Matchers.equalTo(content)).match(getStubMvcResult()); }
@Test // SPR-16587 public void valueWithMatcherAndNumberConversion() throws Exception { new JsonPathResultMatchers("$.num").value(Matchers.equalTo(5.0f), Float.class).match(stubMvcResult); }
@Test(expected = AssertionError.class) public void stringMatcherNoMatch() throws Exception { new ContentResultMatchers().string(Matchers.equalTo("bogus")).match(getStubMvcResult()); }
@Test public void isArrayForAnEmptyArray() throws Exception { new JsonPathResultMatchers("$.emptyArray").isArray().match(stubMvcResult); }
@Test public void isMap() throws Exception { new JsonPathResultMatchers("$.colorMap").isMap().match(stubMvcResult); }
@Test public void isBoolean() throws Exception { new JsonPathResultMatchers("$.bool").isBoolean().match(stubMvcResult); }
@Test(expected = AssertionError.class) public void isBooleanNoMatch() throws Exception { new JsonPathResultMatchers("$.str").isBoolean().match(stubMvcResult); }
@Test(expected = AssertionError.class) public void isStringNoMatch() throws Exception { new JsonPathResultMatchers("$.arr").isString().match(stubMvcResult); }
@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()); }
@Test(expected = AssertionError.class) public void prefixWithPayloadNotLongEnough() throws Exception { MockHttpServletResponse response = new MockHttpServletResponse(); response.addHeader("Content-Type", "application/json"); response.getWriter().print(new String("test".getBytes("ISO-8859-1"))); StubMvcResult result = new StubMvcResult(null, null, null, null, null, null, response); new JsonPathResultMatchers("$.str").prefix("prefix").value("foo").match(result); }
@Test(expected = AssertionError.class) public void valueWithJsonPrefixNotConfigured() throws Exception { String jsonPrefix = "prefix"; StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix); new JsonPathResultMatchers("$.str").value("foo").match(result); }
@Test public void attributeExists() throws Exception { new FlashAttributeResultMatchers().attributeExists("good").match(getStubMvcResult()); }
@Test public void valueWithJsonPrefix() throws Exception { String jsonPrefix = "prefix"; StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix); new JsonPathResultMatchers("$.str").prefix(jsonPrefix).value("foo").match(result); }
@Test(expected = AssertionError.class) public void valueWithJsonWrongPrefix() throws Exception { String jsonPrefix = "prefix"; StubMvcResult result = createPrefixedStubMvcResult(jsonPrefix); new JsonPathResultMatchers("$.str").prefix("wrong").value("foo").match(result); }
@Test public void stringEncodingDetection() throws Exception { String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + "<person><name>Jürgen</name></person>"; byte[] bytes = content.getBytes(StandardCharsets.UTF_8); MockHttpServletResponse response = new MockHttpServletResponse(); response.addHeader("Content-Type", "application/xml"); StreamUtils.copy(bytes, response.getOutputStream()); StubMvcResult result = new StubMvcResult(null, null, null, null, null, null, response); new XpathResultMatchers("/person/name", null).string("Jürgen").match(result); }