Search in sources :

Example 11 with MockHttpServletResponse

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

the class WebRequestTraceFilterTests method filterDoesNotAddAuthorizationHeaderWithoutAuthorizationHeaderInclude.

@Test
@SuppressWarnings({ "unchecked" })
public void filterDoesNotAddAuthorizationHeaderWithoutAuthorizationHeaderInclude() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    request.addHeader("Authorization", "my-auth-header");
    MockHttpServletResponse response = new MockHttpServletResponse();
    this.filter.doFilterInternal(request, response, new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
        }
    });
    Map<String, Object> info = this.repository.findAll().iterator().next().getInfo();
    Map<String, Object> headers = (Map<String, Object>) info.get("headers");
    assertThat(((Map<Object, Object>) headers.get("request"))).hasSize(0);
}
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 12 with MockHttpServletResponse

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

the class WebRequestTraceFilterTests method filterDoesNotAddResponseHeadersWithoutResponseHeadersInclude.

@Test
@SuppressWarnings({ "unchecked" })
public void filterDoesNotAddResponseHeadersWithoutResponseHeadersInclude() throws ServletException, IOException {
    this.properties.setInclude(Collections.singleton(Include.REQUEST_HEADERS));
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.addHeader("Content-Type", "application/json");
    this.filter.doFilterInternal(request, response, new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
        }
    });
    Map<String, Object> info = this.repository.findAll().iterator().next().getInfo();
    Map<String, Object> headers = (Map<String, Object>) info.get("headers");
    assertThat(headers.get("response") == null).isTrue();
}
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 13 with MockHttpServletResponse

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

the class WebRequestTraceFilterTests method filterHasResponseStatus.

@Test
public void filterHasResponseStatus() {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.setStatus(404);
    response.addHeader("Content-Type", "application/json");
    Map<String, Object> trace = this.filter.getTrace(request);
    this.filter.enhanceTrace(trace, response);
    @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) ((Map<String, Object>) trace.get("headers")).get("response");
    assertThat(map.get("status").toString()).isEqualTo("404");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 14 with MockHttpServletResponse

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

the class MockHttpServletRequestBuilder method buildRequest.

/**
	 * Build a {@link MockHttpServletRequest}.
	 */
@Override
public final MockHttpServletRequest buildRequest(ServletContext servletContext) {
    MockHttpServletRequest request = createServletRequest(servletContext);
    request.setAsyncSupported(true);
    request.setMethod(this.method);
    String requestUri = this.url.getRawPath();
    request.setRequestURI(requestUri);
    if (this.url.getScheme() != null) {
        request.setScheme(this.url.getScheme());
    }
    if (this.url.getHost() != null) {
        request.setServerName(this.url.getHost());
    }
    if (this.url.getPort() != -1) {
        request.setServerPort(this.url.getPort());
    }
    updatePathRequestProperties(request, requestUri);
    if (this.secure != null) {
        request.setSecure(this.secure);
    }
    if (this.principal != null) {
        request.setUserPrincipal(this.principal);
    }
    if (this.session != null) {
        request.setSession(this.session);
    }
    request.setCharacterEncoding(this.characterEncoding);
    request.setContent(this.content);
    request.setContentType(this.contentType);
    for (String name : this.headers.keySet()) {
        for (Object value : this.headers.get(name)) {
            request.addHeader(name, value);
        }
    }
    if (this.url.getRawQuery() != null) {
        request.setQueryString(this.url.getRawQuery());
    }
    addRequestParams(request, UriComponentsBuilder.fromUri(this.url).build().getQueryParams());
    for (String name : this.parameters.keySet()) {
        for (String value : this.parameters.get(name)) {
            request.addParameter(name, value);
        }
    }
    if (this.content != null && this.content.length > 0) {
        String requestContentType = request.getContentType();
        if (requestContentType != null) {
            MediaType mediaType = MediaType.parseMediaType(requestContentType);
            if (MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType)) {
                addRequestParams(request, parseFormData(mediaType));
            }
        }
    }
    if (!ObjectUtils.isEmpty(this.cookies)) {
        request.setCookies(this.cookies.toArray(new Cookie[this.cookies.size()]));
    }
    if (!ObjectUtils.isEmpty(this.locales)) {
        request.setPreferredLocales(this.locales);
    }
    for (String name : this.requestAttributes.keySet()) {
        request.setAttribute(name, this.requestAttributes.get(name));
    }
    for (String name : this.sessionAttributes.keySet()) {
        request.getSession().setAttribute(name, this.sessionAttributes.get(name));
    }
    FlashMap flashMap = new FlashMap();
    flashMap.putAll(this.flashAttributes);
    FlashMapManager flashMapManager = getFlashMapManager(request);
    flashMapManager.saveOutputFlashMap(flashMap, request, new MockHttpServletResponse());
    return request;
}
Also used : Cookie(javax.servlet.http.Cookie) FlashMap(org.springframework.web.servlet.FlashMap) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) FlashMapManager(org.springframework.web.servlet.FlashMapManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MediaType(org.springframework.http.MediaType) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 15 with MockHttpServletResponse

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

the class MockMvcClientHttpRequestFactory method createRequest.

@Override
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
    return new MockClientHttpRequest(httpMethod, uri) {

        @Override
        public ClientHttpResponse executeInternal() throws IOException {
            try {
                MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri);
                requestBuilder.content(getBodyAsBytes());
                requestBuilder.headers(getHeaders());
                MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn();
                MockHttpServletResponse servletResponse = mvcResult.getResponse();
                HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
                byte[] body = servletResponse.getContentAsByteArray();
                HttpHeaders headers = getResponseHeaders(servletResponse);
                MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
                clientResponse.getHeaders().putAll(headers);
                return clientResponse;
            } catch (Exception ex) {
                byte[] body = ex.toString().getBytes(StandardCharsets.UTF_8);
                return new MockClientHttpResponse(body, HttpStatus.INTERNAL_SERVER_ERROR);
            }
        }
    };
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) IOException(java.io.IOException) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse)

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