Search in sources :

Example 6 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project opennms by OpenNMS.

the class AbstractSpringJerseyRestJsonTestCase method sendPost.

@Override
protected MockHttpServletResponse sendPost(String url, String xml, int statusCode, final String expectedUrlSuffix) throws Exception {
    LOG.debug("POST {}, expected status code = {}, expected URL suffix = {}", url, statusCode, expectedUrlSuffix);
    final MockHttpServletResponse response = sendData(POST, MediaType.APPLICATION_JSON, url, xml, statusCode);
    if (expectedUrlSuffix != null) {
        final Object header = response.getHeader("Location");
        assertNotNull("Location header is null", header);
        final String location = URLDecoder.decode(header.toString(), StandardCharsets.UTF_8.name());
        final String decodedExpectedUrlSuffix = URLDecoder.decode(expectedUrlSuffix, StandardCharsets.UTF_8.name());
        assertTrue("location '" + location + "' should end with '" + decodedExpectedUrlSuffix + "'", location.endsWith(decodedExpectedUrlSuffix));
    }
    return response;
}
Also used : JSONObject(org.json.JSONObject) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 7 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project opennms by OpenNMS.

the class AbstractSpringJerseyRestTestCase method putXmlObject.

protected void putXmlObject(final JAXBContext context, final String url, final int expectedStatus, final Object object) throws Exception {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Marshaller marshaller = context.createMarshaller();
    marshaller.marshal(object, out);
    final byte[] content = out.toByteArray();
    final MockHttpServletRequest request = createRequest(servletContext, PUT, url, getUser(), getUserRoles());
    request.setContentType(MediaType.APPLICATION_XML);
    request.setContent(content);
    final MockHttpServletResponse response = createResponse();
    dispatch(request, response);
    assertEquals(expectedStatus, response.getStatus());
}
Also used : Marshaller(javax.xml.bind.Marshaller) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 8 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project ORCID-Source by ORCID.

the class OrcidUrlManagerTest method setUpSavedRequest.

private Pair<HttpServletRequest, HttpServletResponse> setUpSavedRequest(String savedUrl) throws URISyntaxException {
    URI uri = new URI(savedUrl);
    MockHttpServletRequest savedRequest = new MockHttpServletRequest("GET", uri.getPath());
    savedRequest.setScheme(uri.getScheme());
    savedRequest.setServerName(uri.getHost());
    savedRequest.setQueryString(uri.getQuery());
    MockHttpServletResponse savedResponse = new MockHttpServletResponse();
    HttpSessionRequestCache sessionCache = new HttpSessionRequestCache();
    sessionCache.saveRequest(savedRequest, savedResponse);
    MockHttpServletRequest currentRequest = new MockHttpServletRequest();
    currentRequest.setSession(savedRequest.getSession());
    MockHttpServletResponse currentResponse = new MockHttpServletResponse();
    return new ImmutablePair<>(currentRequest, currentResponse);
}
Also used : ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpSessionRequestCache(org.springframework.security.web.savedrequest.HttpSessionRequestCache) URI(java.net.URI) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 9 with MockHttpServletResponse

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

the class WebRequestTraceFilterTests method filterHas500ResponseStatusWhenExceptionIsThrown.

@Test
@SuppressWarnings("unchecked")
public void filterHas500ResponseStatusWhenExceptionIsThrown() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    MockHttpServletResponse response = new MockHttpServletResponse();
    try {
        this.filter.doFilterInternal(request, response, new FilterChain() {

            @Override
            public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
                throw new RuntimeException();
            }
        });
        fail("Exception was swallowed");
    } catch (RuntimeException ex) {
        Map<String, Object> headers = (Map<String, Object>) this.repository.findAll().iterator().next().getInfo().get("headers");
        Map<String, Object> responseHeaders = (Map<String, Object>) headers.get("response");
        assertThat((String) responseHeaders.get("status")).isEqualTo("500");
    }
}
Also used : ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) IOException(java.io.IOException) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 10 with MockHttpServletResponse

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

the class WebRequestTraceFilterTests method filterDoesNotAddResponseCookiesWithCookiesExclude.

@Test
@SuppressWarnings({ "unchecked" })
public void filterDoesNotAddResponseCookiesWithCookiesExclude() throws ServletException, IOException {
    this.properties.setInclude(Collections.singleton(Include.RESPONSE_HEADERS));
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.addHeader("Content-Type", "application/json");
    response.addHeader("Set-Cookie", "testCookie=testValue;");
    Map<String, Object> trace = this.filter.getTrace(request);
    this.filter.enhanceTrace(trace, response);
    Map<String, Object> map = (Map<String, Object>) trace.get("headers");
    assertThat(map.get("response").toString()).isEqualTo("{Content-Type=application/json, status=200}");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

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