Search in sources :

Example 1 with ResultMatcher

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

the class ContentResultMatchers method contentTypeCompatibleWith.

/**
	 * Assert the ServletResponse content type is compatible with the given
	 * content type as defined by {@link MediaType#isCompatibleWith(MediaType)}.
	 */
public ResultMatcher contentTypeCompatibleWith(final MediaType contentType) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            String actual = result.getResponse().getContentType();
            assertTrue("Content type not set", actual != null);
            MediaType actualContentType = MediaType.parseMediaType(actual);
            assertTrue("Content type [" + actual + "] is not compatible with [" + contentType + "]", actualContentType.isCompatibleWith(contentType));
        }
    };
}
Also used : MediaType(org.springframework.http.MediaType) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 2 with ResultMatcher

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

the class CookieResultMatchers method secure.

/**
	 * Assert whether the cookie must be sent over a secure protocol or not.
	 */
public ResultMatcher secure(final String name, final boolean secure) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            Cookie cookie = result.getResponse().getCookie(name);
            assertEquals("Response cookie secure", secure, cookie.getSecure());
        }
    };
}
Also used : Cookie(javax.servlet.http.Cookie) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 3 with ResultMatcher

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

the class CookieResultMatchers method exists.

/**
	 * Assert a cookie exists. The existence check is irrespective of whether
	 * max age is 0 (i.e. expired).
	 */
public ResultMatcher exists(final String name) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) {
            Cookie cookie = result.getResponse().getCookie(name);
            assertTrue("No cookie with name: " + name, cookie != null);
        }
    };
}
Also used : Cookie(javax.servlet.http.Cookie) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 4 with ResultMatcher

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

the class CookieResultMatchers method comment.

/**
	 * Assert a cookie's comment value.
	 */
public ResultMatcher comment(final String name, final String comment) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            Cookie cookie = result.getResponse().getCookie(name);
            assertEquals("Response cookie comment", comment, cookie.getComment());
        }
    };
}
Also used : Cookie(javax.servlet.http.Cookie) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 5 with ResultMatcher

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

the class CookieResultMatchers method maxAge.

/**
	 * Assert a cookie's maxAge with a Hamcrest {@link Matcher}.
	 */
public ResultMatcher maxAge(final String name, final Matcher<? super Integer> matcher) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) {
            Cookie cookie = result.getResponse().getCookie(name);
            assertTrue("No cookie with name: " + name, cookie != null);
            assertThat("Response cookie maxAge", cookie.getMaxAge(), matcher);
        }
    };
}
Also used : Cookie(javax.servlet.http.Cookie) ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult)

Aggregations

ResultMatcher (org.springframework.test.web.servlet.ResultMatcher)74 MvcResult (org.springframework.test.web.servlet.MvcResult)69 Test (org.junit.jupiter.api.Test)22 Cookie (javax.servlet.http.Cookie)15 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)13 ModelAndView (org.springframework.web.servlet.ModelAndView)11 Test (org.junit.Test)5 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)5 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 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