Search in sources :

Example 56 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method defaultParameters.

@Test
public void defaultParameters() throws Exception {
    initServletWithControllers(DefaultValueParamController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("foo--bar", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 57 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method requestBodyResponseBody.

@Test
public void requestBodyResponseBody() throws ServletException, IOException {
    initServletWithControllers(RequestResponseBodyController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
    String requestBody = "Hello World";
    request.setContent(requestBody.getBytes("UTF-8"));
    request.addHeader("Content-Type", "text/plain; charset=utf-8");
    request.addHeader("Accept", "text/*, */*");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(200, response.getStatus());
    assertEquals(requestBody, response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 58 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method emptyValueMapping.

@Test
public void emptyValueMapping() throws Exception {
    initServletWithControllers(ControllerWithEmptyValueMapping.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    request.setContextPath("/foo");
    request.setServletPath("");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("test", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 59 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method relativePathDispatchingController.

@Test
public void relativePathDispatchingController() throws Exception {
    initServletWithControllers(MyRelativePathDispatchingController.class);
    getServlet().init(new MockServletConfig());
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myApp/myHandle");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myView", response.getContentAsString());
    request = new MockHttpServletRequest("GET", "/myApp/myOther");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myOtherView", response.getContentAsString());
    request = new MockHttpServletRequest("GET", "/myApp/myLang");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myLangView", response.getContentAsString());
    request = new MockHttpServletRequest("GET", "/myApp/surprise.do");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("mySurpriseView", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 60 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method restController.

@Test
public void restController() throws Exception {
    initServletWithControllers(ThisWillActuallyRun.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("Hello World!", response.getContentAsString());
}
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