use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method parameterCsvAsStringArray.
@Test
public void parameterCsvAsStringArray() throws Exception {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
wbiDef.getPropertyValues().add("conversionService", csDef);
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, CsvController.class);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/integerArray");
request.setMethod("POST");
request.addParameter("content", "1,2");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("1-2", response.getContentAsString());
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method httpPatch.
@Test
public void httpPatch() throws ServletException, IOException {
initServletWithControllers(RequestResponseBodyController.class);
MockHttpServletRequest request = new MockHttpServletRequest("PATCH", "/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());
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method doTestAdaptedHandleMethods.
private void doTestAdaptedHandleMethods(final Class<?> controllerClass) throws Exception {
initServletWithControllers(controllerClass);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath1.do");
MockHttpServletResponse response = new MockHttpServletResponse();
request.addParameter("param1", "value1");
request.addParameter("param2", "2");
getServlet().service(request, response);
assertEquals("test", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/myPath2.do");
request.addParameter("param1", "value1");
request.addParameter("param2", "2");
request.addHeader("header1", "10");
request.setCookies(new Cookie("cookie1", "3"));
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("test-value1-2-10-3", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/myPath3.do");
request.addParameter("param1", "value1");
request.addParameter("param2", "2");
request.addParameter("name", "name1");
request.addParameter("age", "2");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("test-name1-2", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/myPath4.do");
request.addParameter("param1", "value1");
request.addParameter("param2", "2");
request.addParameter("name", "name1");
request.addParameter("age", "value2");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("test-name1-typeMismatch", response.getContentAsString());
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseBodyArgMismatch.
@Test
public void responseBodyArgMismatch() throws ServletException, IOException {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(A.class, B.class);
try {
marshaller.afterPropertiesSet();
} catch (Exception ex) {
throw new BeanCreationException(ex.getMessage(), ex);
}
MarshallingHttpMessageConverter messageConverter = new MarshallingHttpMessageConverter(marshaller);
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
adapterDef.getPropertyValues().add("messageConverters", messageConverter);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, RequestBodyArgMismatchController.class);
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
String requestBody = "<b/>";
request.setContent(requestBody.getBytes("UTF-8"));
request.addHeader("Content-Type", "application/xml; charset=utf-8");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals(400, response.getStatus());
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method produces.
@Test
public void produces() throws ServletException, IOException {
initServletWithControllers(ProducesController.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "text/html");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("html", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "application/xml");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("xml", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "application/xml, text/html");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("xml", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "text/html;q=0.9, application/xml");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("xml", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "application/msword");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals(406, response.getStatus());
}
Aggregations