Search in sources :

Example 6 with MockHttpServletResponse

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

the class RequestLoggingFilterTests method noQueryStringAvailable.

@Test
public void noQueryStringAvailable() throws Exception {
    filter.setIncludeQueryString(true);
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = new NoOpFilterChain();
    filter.doFilter(request, response, filterChain);
    assertNotNull(filter.beforeRequestMessage);
    assertTrue(filter.beforeRequestMessage.contains("[uri=/hotels]"));
    assertNotNull(filter.afterRequestMessage);
    assertTrue(filter.afterRequestMessage.contains("[uri=/hotels]"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 7 with MockHttpServletResponse

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

the class RequestLoggingFilterTests method queryStringIncluded.

@Test
public void queryStringIncluded() throws Exception {
    filter.setIncludeQueryString(true);
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setQueryString("booking=42");
    FilterChain filterChain = new NoOpFilterChain();
    filter.doFilter(request, response, filterChain);
    assertNotNull(filter.beforeRequestMessage);
    assertTrue(filter.beforeRequestMessage.contains("[uri=/hotels?booking=42]"));
    assertNotNull(filter.afterRequestMessage);
    assertTrue(filter.afterRequestMessage.contains("[uri=/hotels?booking=42]"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 8 with MockHttpServletResponse

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

the class ForwardedHeaderFilterTests method doWithFiltersAndGetResponse.

@SuppressWarnings("serial")
private MockHttpServletResponse doWithFiltersAndGetResponse(Filter... filters) throws ServletException, IOException {
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = new MockFilterChain(new HttpServlet() {
    }, filters);
    filterChain.doFilter(request, response);
    return response;
}
Also used : HttpServlet(javax.servlet.http.HttpServlet) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.test.MockFilterChain) MockFilterChain(org.springframework.mock.web.test.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse)

Example 9 with MockHttpServletResponse

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

the class HiddenHttpMethodFilterTests method filterWithParameter.

@Test
public void filterWithParameter() throws IOException, ServletException {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
    request.addParameter("_method", "delete");
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse) throws IOException, ServletException {
            assertEquals("Invalid method", "DELETE", ((HttpServletRequest) filterRequest).getMethod());
        }
    };
    filter.doFilter(request, response, filterChain);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 10 with MockHttpServletResponse

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

the class HiddenHttpMethodFilterTests method filterWithNoParameter.

@Test
public void filterWithNoParameter() throws IOException, ServletException {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/hotels");
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse) throws IOException, ServletException {
            assertEquals("Invalid method", "POST", ((HttpServletRequest) filterRequest).getMethod());
        }
    };
    filter.doFilter(request, response, filterChain);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)171 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)162 Test (org.junit.Test)140 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)33 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)28 Before (org.junit.Before)19 MockServletContext (org.springframework.mock.web.test.MockServletContext)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)13 ModelAndView (org.springframework.web.servlet.ModelAndView)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)10 TestBean (org.springframework.tests.sample.beans.TestBean)9 HashMap (java.util.HashMap)8 FilterChain (javax.servlet.FilterChain)8 ServletException (javax.servlet.ServletException)7 HttpSession (javax.servlet.http.HttpSession)7 IOException (java.io.IOException)6 Map (java.util.Map)6 ServletRequest (javax.servlet.ServletRequest)6 ServletResponse (javax.servlet.ServletResponse)6