use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method customAnnotationController.
@Test
public void customAnnotationController() throws Exception {
initServletWithControllers(CustomAnnotationController.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("Invalid response status code", HttpServletResponse.SC_OK, response.getStatus());
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseAsHttpHeadersNoHeader.
@Test
public void responseAsHttpHeadersNoHeader() throws Exception {
initServletWithControllers(HttpHeadersResponseController.class);
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(new MockHttpServletRequest("POST", "/empty"), response);
assertEquals("Wrong status code", MockHttpServletResponse.SC_CREATED, response.getStatus());
assertEquals("Wrong number of headers", 0, response.getHeaderNames().size());
assertEquals("Expected an empty content", 0, response.getContentLength());
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method parameterizedAnnotatedInterface.
@SuppressWarnings("rawtypes")
@Test
public void parameterizedAnnotatedInterface() throws Exception {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext context) {
context.registerBeanDefinition("viewResolver", new RootBeanDefinition(ModelExposingViewResolver.class));
}
}, MyParameterizedControllerImpl.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPage");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("page1", request.getAttribute("viewName"));
HttpSession session = request.getSession();
assertTrue(session.getAttribute("object1") != null);
assertTrue(session.getAttribute("object2") != null);
assertTrue(((Map) session.getAttribute("model")).containsKey("object1"));
assertTrue(((Map) session.getAttribute("model")).containsKey("object2"));
assertTrue(((Map) session.getAttribute("model")).containsKey("testBeanList"));
request = new MockHttpServletRequest("POST", "/myPage");
request.setSession(session);
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("page2", request.getAttribute("viewName"));
assertTrue(session.getAttribute("object1") != null);
assertTrue(session.getAttribute("object2") != null);
assertTrue(((Map) session.getAttribute("model")).containsKey("object1"));
assertTrue(((Map) session.getAttribute("model")).containsKey("object2"));
assertTrue(((Map) session.getAttribute("model")).containsKey("testBeanList"));
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method multipartFilesAsStringArray.
@Test
public void multipartFilesAsStringArray() throws Exception {
initServletWithControllers(MultipartController.class);
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
request.setRequestURI("/stringArray");
request.addFile(new MockMultipartFile("content", "Juergen".getBytes()));
request.addFile(new MockMultipartFile("content", "Eva".getBytes()));
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("Juergen-Eva", response.getContentAsString());
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method nullCommandController.
@Test
public void nullCommandController() throws Exception {
initServletWithControllers(MyNullCommandController.class);
getServlet().init(new MockServletConfig());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath");
request.setUserPrincipal(new OtherPrincipal());
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("myView", response.getContentAsString());
}
Aggregations