Search in sources :

Example 11 with MvcResult

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

the class CookieResultMatchers method value.

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

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

Example 12 with MvcResult

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

the class CookieResultMatchers method maxAge.

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

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

Example 13 with MvcResult

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

the class HandlerResultMatchers method method.

/**
	 * Assert the controller method used to process the request.
	 */
public ResultMatcher method(final Method method) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            HandlerMethod handlerMethod = getHandlerMethod(result);
            assertEquals("Handler method", method, handlerMethod.getMethod());
        }
    };
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 14 with MvcResult

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

the class HandlerResultMatchers method methodName.

/**
	 * Assert the name of the controller method used to process the request
	 * using the given Hamcrest {@link Matcher}.
	 */
public ResultMatcher methodName(final Matcher<? super String> matcher) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            HandlerMethod handlerMethod = getHandlerMethod(result);
            assertThat("Handler method", handlerMethod.getMethod().getName(), matcher);
        }
    };
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 15 with MvcResult

use of org.springframework.test.web.servlet.MvcResult 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)

Aggregations

MvcResult (org.springframework.test.web.servlet.MvcResult)628 Test (org.junit.Test)365 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)178 Test (org.junit.jupiter.api.Test)163 ResultMatcher (org.springframework.test.web.servlet.ResultMatcher)69 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)39 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)38 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)36 MockHttpSession (org.springframework.mock.web.MockHttpSession)35 ResultActions (org.springframework.test.web.servlet.ResultActions)27 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)26 ExpectedDatabase (com.github.springtestdbunit.annotation.ExpectedDatabase)26 Map (java.util.Map)24 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)24 WithMockUser (org.springframework.security.test.context.support.WithMockUser)23 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)22 AbstractSaturnConsoleTest (com.vip.saturn.job.console.AbstractSaturnConsoleTest)20 Cookie (javax.servlet.http.Cookie)18 MockMvc (org.springframework.test.web.servlet.MockMvc)17 HttpSession (jakarta.servlet.http.HttpSession)16