Search in sources :

Example 16 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse 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 17 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse 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 18 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project spring-framework by spring-projects.

the class XpathResultMatchers method number.

/**
	 * 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 new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            MockHttpServletResponse response = result.getResponse();
            xpathHelper.assertNumber(response.getContentAsByteArray(), getDefinedEncoding(response), matcher);
        }
    };
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 19 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project spring-framework by spring-projects.

the class XpathResultMatchers method doesNotExist.

/**
	 * Evaluate the XPath and assert that content doesn't exist.
	 */
public ResultMatcher doesNotExist() {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            MockHttpServletResponse response = result.getResponse();
            xpathHelper.doesNotExist(response.getContentAsByteArray(), getDefinedEncoding(response));
        }
    };
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 20 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project spring-framework by spring-projects.

the class XpathResultMatchers method booleanValue.

/**
	 * Evaluate the XPath and assert the {@link Boolean} value found.
	 */
public ResultMatcher booleanValue(final Boolean value) {
    return new ResultMatcher() {

        @Override
        public void match(MvcResult result) throws Exception {
            MockHttpServletResponse response = result.getResponse();
            xpathHelper.assertBoolean(response.getContentAsByteArray(), getDefinedEncoding(response), value);
        }
    };
}
Also used : ResultMatcher(org.springframework.test.web.servlet.ResultMatcher) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Aggregations

MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2376 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2002 Test (org.junit.jupiter.api.Test)1416 lombok.val (lombok.val)946 Test (org.junit.Test)582 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)484 MockServletContext (org.springframework.mock.web.MockServletContext)462 MockRequestContext (org.springframework.webflow.test.MockRequestContext)460 MockFilterChain (org.springframework.mock.web.MockFilterChain)240 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)185 JEEContext (org.pac4j.core.context.JEEContext)159 FilterChain (jakarta.servlet.FilterChain)117 Authentication (org.springframework.security.core.Authentication)116 BeforeEach (org.junit.jupiter.api.BeforeEach)107 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)97 HashMap (java.util.HashMap)84 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)83 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)74 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)67 MockHttpSession (org.springframework.mock.web.MockHttpSession)67