Search in sources :

Example 46 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method parameterDispatchingController.

@Test
public void parameterDispatchingController() throws Exception {
    final MockServletContext servletContext = new MockServletContext();
    final MockServletConfig servletConfig = new MockServletConfig(servletContext);
    WebApplicationContext webAppContext = initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            wac.setServletContext(servletContext);
            AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
            wac.getBeanFactory().registerResolvableDependency(ServletConfig.class, servletConfig);
        }
    }, MyParameterDispatchingController.class);
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpSession session = request.getSession();
    getServlet().service(request, response);
    assertEquals("myView", response.getContentAsString());
    assertSame(servletContext, request.getAttribute("servletContext"));
    assertSame(servletConfig, request.getAttribute("servletConfig"));
    assertSame(session.getId(), request.getAttribute("sessionId"));
    assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
    assertSame(request.getLocale(), request.getAttribute("locale"));
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    response = new MockHttpServletResponse();
    session = request.getSession();
    getServlet().service(request, response);
    assertEquals("myView", response.getContentAsString());
    assertSame(servletContext, request.getAttribute("servletContext"));
    assertSame(servletConfig, request.getAttribute("servletConfig"));
    assertSame(session.getId(), request.getAttribute("sessionId"));
    assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "other");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myOtherView", response.getContentAsString());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "my");
    request.addParameter("lang", "de");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myLangView", response.getContentAsString());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("surprise", "!");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("mySurpriseView", response.getContentAsString());
    MyParameterDispatchingController deserialized = (MyParameterDispatchingController) SerializationTestUtils.serializeAndDeserialize(webAppContext.getBean(MyParameterDispatchingController.class.getSimpleName()));
    assertNotNull(deserialized.request);
    assertNotNull(deserialized.session);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 47 with MockHttpServletResponse

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method methodNotSupported.

@Test
public void methodNotSupported() throws Exception {
    initServletWithControllers(MethodNotAllowedController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/1");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(200, response.getStatus());
    request = new MockHttpServletRequest("POST", "/hotels/1");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(405, response.getStatus());
    request = new MockHttpServletRequest("GET", "/hotels");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(200, response.getStatus());
    request = new MockHttpServletRequest("POST", "/hotels");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(405, response.getStatus());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 48 with MockHttpServletResponse

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method customRegex.

@Test
public void customRegex() throws Exception {
    initServletWithControllers(CustomRegexController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/42;q=1;q=2");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(200, response.getStatus());
    assertEquals("test-42-;q=1;q=2-[1, 2]", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 49 with MockHttpServletResponse

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method simple.

@Test
public void simple() throws Exception {
    initServletWithControllers(SimpleUriTemplateController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/42");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("test-42-7", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 50 with MockHttpServletResponse

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method binding.

@Test
public void binding() throws Exception {
    initServletWithControllers(BindingUriTemplateController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/42/dates/2008-11-18");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(200, response.getStatus());
    request = new MockHttpServletRequest("GET", "/hotels/42/dates/2008-foo-bar");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(400, response.getStatus());
    initServletWithControllers(NonBindingUriTemplateController.class);
    request = new MockHttpServletRequest("GET", "/hotels/42/dates/2008-foo-bar");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(500, response.getStatus());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) 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