Search in sources :

Example 11 with ResultMatcher

use of org.springframework.test.web.servlet.ResultMatcher in project spring-framework by spring-projects.

the class HeaderResultMatchers method dateValue.

/**
	 * Assert the primary value of the named response header as a date String,
	 * using the preferred date format described in RFC 7231.
	 * <p>The {@link ResultMatcher} returned by this method throws an
	 * {@link AssertionError} if the response does not contain the specified
	 * header, or if the supplied {@code value} does not match the primary value.
	 * @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.1.1">Section 7.1.1.1 of RFC 7231</a>
	 * @since 4.2
	 */
public ResultMatcher dateValue(final String name, final long value) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) {
            SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
            format.setTimeZone(TimeZone.getTimeZone("GMT"));
            String formatted = format.format(new Date(value));
            MockHttpServletResponse response = result.getResponse();
            assertTrue("Response does not contain header " + name, response.containsHeader(name));
            assertEquals("Response header " + name, formatted, response.getHeader(name));
        }
    };
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 12 with ResultMatcher

use of org.springframework.test.web.servlet.ResultMatcher in project spring-framework by spring-projects.

the class HeaderResultMatchers method longValue.

/**
	 * Assert the primary value of the named response header as a {@code long}.
	 * <p>The {@link ResultMatcher} returned by this method throws an
	 * {@link AssertionError} if the response does not contain the specified
	 * header, or if the supplied {@code value} does not match the primary value.
	 */
public ResultMatcher longValue(final String name, final long value) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) {
            MockHttpServletResponse response = result.getResponse();
            assertTrue("Response does not contain header " + name, response.containsHeader(name));
            assertEquals("Response header " + name, value, Long.parseLong(response.getHeader(name)));
        }
    };
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 13 with ResultMatcher

use of org.springframework.test.web.servlet.ResultMatcher in project spring-framework by spring-projects.

the class ModelResultMatchers method attributeHasNoErrors.

/**
	 * Assert the given model attribute(s) do not have errors.
	 */
public ResultMatcher attributeHasNoErrors(final String... names) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult mvcResult) throws Exception {
            ModelAndView mav = getModelAndView(mvcResult);
            for (String name : names) {
                BindingResult result = getBindingResult(mav, name);
                assertTrue("No errors for attribute [" + name + "]", !result.hasErrors());
            }
        }
    };
}
Also used : BindingResult(org.springframework.validation.BindingResult) ModelAndView(org.springframework.web.servlet.ModelAndView) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 14 with ResultMatcher

use of org.springframework.test.web.servlet.ResultMatcher in project spring-framework by spring-projects.

the class ModelResultMatchers method attribute.

/**
	 * Assert a model attribute value with the given Hamcrest {@link Matcher}.
	 */
public <T> ResultMatcher attribute(final String name, final Matcher<T> matcher) {
    return new ResultMatcher() {

        @Override
        @SuppressWarnings("unchecked")
        public void match(MvcResult result) throws Exception {
            ModelAndView mav = getModelAndView(result);
            assertThat("Model attribute '" + name + "'", (T) mav.getModel().get(name), matcher);
        }
    };
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 15 with ResultMatcher

use of org.springframework.test.web.servlet.ResultMatcher in project spring-framework by spring-projects.

the class ModelResultMatchers method size.

/**
	 * Assert the number of model attributes.
	 */
public <T> ResultMatcher size(final int size) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            ModelAndView mav = getModelAndView(result);
            int actual = 0;
            for (String key : mav.getModel().keySet()) {
                if (!key.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
                    actual++;
                }
            }
            assertEquals("Model size", size, actual);
        }
    };
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Aggregations

MvcResult (org.springframework.test.web.servlet.MvcResult)48 ResultMatcher (org.springframework.test.web.servlet.ResultMatcher)48 Cookie (javax.servlet.http.Cookie)15 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)13 ModelAndView (org.springframework.web.servlet.ModelAndView)11 BindingResult (org.springframework.validation.BindingResult)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 HandlerMethod (org.springframework.web.method.HandlerMethod)4 Method (java.lang.reflect.Method)2 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Test (org.junit.Test)1 HttpStatus (org.springframework.http.HttpStatus)1 MediaType (org.springframework.http.MediaType)1 AssertionErrors (org.springframework.test.util.AssertionErrors)1 StubMvcResult (org.springframework.test.web.servlet.StubMvcResult)1 Errors (org.springframework.validation.Errors)1 MethodInvocationInfo (org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.MethodInvocationInfo)1