/** * Assert the Servlet response error message with the given Hamcrest {@link Matcher}. */ public ResultMatcher reason(final Matcher<? super String> matcher) { return result -> assertThat("Response status reason", result.getResponse().getErrorMessage(), matcher); }
/** * Assert the primary value of the response header with the given Hamcrest * String {@code Matcher}. */ public ResultMatcher string(final String name, final Matcher<? super String> matcher) { return result -> assertThat("Response header '" + name + "'", result.getResponse().getHeader(name), matcher); }
/** * Assert the values of the response header as String values. * @since 4.3 */ public ResultMatcher stringValues(final String name, final String... values) { return result -> { List<Object> actual = result.getResponse().getHeaderValues(name); assertEquals("Response header '" + name + "'", Arrays.asList(values), actual); }; }
/** * Assert the Servlet response error message. */ public ResultMatcher reason(final String reason) { return result -> assertEquals("Response status reason", reason, result.getResponse().getErrorMessage()); }
/** * Asserts the request was redirected to the given URL. * <p>This method accepts only exact matches. * @param expectedUrl the exact URL expected */ public static ResultMatcher redirectedUrl(String expectedUrl) { return result -> assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl()); }
/** * Assert that the named response header does not exist. * @since 4.0 */ public ResultMatcher doesNotExist(final String name) { return result -> assertTrue("Response should not contain header '" + name + "'", !result.getResponse().containsHeader(name)); }
/** * Assert a cookie does not exist. Note that the existence check is * irrespective of whether max age is 0, i.e. expired. */ public ResultMatcher doesNotExist(final String name) { return result -> { Cookie cookie = result.getResponse().getCookie(name); assertTrue("Unexpected cookie with name '" + name + "'", cookie == null); }; }
/** * Assert the response body content as a byte array. */ public ResultMatcher bytes(final byte[] expectedContent) { return result -> assertEquals("Response content", expectedContent, result.getResponse().getContentAsByteArray()); }
/** * Assert that the named response header exists. * @since 5.0.3 */ public ResultMatcher exists(final String name) { return result -> assertTrue("Response should contain header '" + name + "'", result.getResponse().containsHeader(name)); }
private static Cookie getCookie(MvcResult result, String name) { Cookie cookie = result.getResponse().getCookie(name); if (cookie == null) { AssertionErrors.fail("No cookie with name '" + name + "'"); } return cookie; }
/** * Assert the response status code is in the 2xx range. */ public ResultMatcher is2xxSuccessful() { return result -> assertEquals("Range for response status value " + result.getResponse().getStatus(), HttpStatus.Series.SUCCESSFUL, getHttpStatusSeries(result)); }
/** * Apply the XPath and assert the {@link String} value found. */ public ResultMatcher string(final String expectedValue) { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.assertString(response.getContentAsByteArray(), getDefinedEncoding(response), expectedValue); }; }
/** * Assert the response status code is in the 4xx range. */ public ResultMatcher is4xxClientError() { return result -> assertEquals("Range for response status value " + result.getResponse().getStatus(), HttpStatus.Series.CLIENT_ERROR, getHttpStatusSeries(result)); }
private HttpStatus.Series getHttpStatusSeries(MvcResult result) { int statusValue = result.getResponse().getStatus(); HttpStatus status = HttpStatus.valueOf(statusValue); return status.series(); }
/** * Evaluate the XPath and assert the {@link Boolean} value found. */ public ResultMatcher booleanValue(final Boolean value) { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.assertBoolean(response.getContentAsByteArray(), getDefinedEncoding(response), value); }; }
/** * Assert the response status code is in the 1xx range. */ public ResultMatcher is1xxInformational() { return result -> assertEquals("Range for response status value " + result.getResponse().getStatus(), HttpStatus.Series.INFORMATIONAL, getHttpStatusSeries(result)); }
/** * Assert the response status code is in the 3xx range. */ public ResultMatcher is3xxRedirection() { return result -> assertEquals("Range for response status value " + result.getResponse().getStatus(), HttpStatus.Series.REDIRECTION, getHttpStatusSeries(result)); }
/** * Evaluate the XPath and assert that content doesn't exist. */ public ResultMatcher doesNotExist() { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.doesNotExist(response.getContentAsByteArray(), getDefinedEncoding(response)); }; }
/** * Evaluate the XPath and assert the number of nodes found. */ public ResultMatcher nodeCount(final int expectedCount) { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.assertNodeCount(response.getContentAsByteArray(), getDefinedEncoding(response), expectedCount); }; }
/** * Evaluate the XPath and assert the {@link Double} value found with the * given Hamcrest {@link Matcher}. */ public ResultMatcher number(final Matcher<? super Double> matcher) { return result -> { MockHttpServletResponse response = result.getResponse(); this.xpathHelper.assertNumber(response.getContentAsByteArray(), getDefinedEncoding(response), matcher); }; }