use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseBodyAsTextWithCssExtension.
@Test
public void responseBodyAsTextWithCssExtension() throws Exception {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
factoryBean.afterPropertiesSet();
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
wac.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, TextRestController.class);
byte[] content = "body".getBytes(StandardCharsets.ISO_8859_1);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a4.css");
request.setContent(content);
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals(200, response.getStatus());
assertEquals("text/css;charset=ISO-8859-1", response.getContentType());
assertNull(response.getHeader("Content-Disposition"));
assertArrayEquals(content, response.getContentAsByteArray());
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method errorThrownFromHandlerMethod.
@Test
public void errorThrownFromHandlerMethod() throws Exception {
initServletWithControllers(ControllerWithErrorThrown.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
request.setContextPath("/foo");
request.setServletPath("");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("test", response.getContentAsString());
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method overlappingMessageConvertersRequestBody.
/*
* See SPR-6877
*/
@Test
public void overlappingMessageConvertersRequestBody() throws ServletException, IOException {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new SimpleMessageConverter(new MediaType("application", "json"), MediaType.ALL));
adapterDef.getPropertyValues().add("messageConverters", messageConverters);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, RequestResponseBodyController.class);
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
request.setContent("Hello World".getBytes("UTF-8"));
request.addHeader("Content-Type", "text/plain; charset=utf-8");
request.addHeader("Accept", "application/json, text/javascript, */*");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("Invalid content-type", "application/json;charset=ISO-8859-1", response.getHeader("Content-Type"));
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method ambiguousParams.
@Test
public void ambiguousParams() throws ServletException, IOException {
initServletWithControllers(AmbiguousParamsController.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("noParams", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/test");
request.addParameter("myParam", "42");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("myParam-42", response.getContentAsString());
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method regularParameterAsStringArray.
@Test
public void regularParameterAsStringArray() throws Exception {
initServletWithControllers(MultipartController.class);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/stringArray");
request.setMethod("POST");
request.addParameter("content", "Juergen");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("Juergen", response.getContentAsString());
}
Aggregations