use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method specificBinderInitializingCommandProvidingFormController.
@PathPatternsParameterizedTest
void specificBinderInitializingCommandProvidingFormController(boolean usePathPatterns) throws Exception {
initDispatcherServlet(MySpecificBinderInitializingCommandProvidingFormController.class, usePathPatterns, wac -> wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(TestViewResolver.class)));
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
request.addParameter("defaultName", "myDefaultName");
request.addParameter("age", "value2");
request.addParameter("date", "2007-10-02");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myView-String:myDefaultName-typeMismatch-tb1-myOriginalValue");
}
use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseBodyAsHtml.
@PathPatternsParameterizedTest
@SuppressWarnings("deprecation")
void responseBodyAsHtml(boolean usePathPatterns) throws Exception {
initDispatcherServlet(TextRestController.class, usePathPatterns, wac -> {
if (!usePathPatterns) {
// `useSuffixPatternMatch` is not allowed with PathPattern's
RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
mappingDef.getPropertyValues().add("useSuffixPatternMatch", true);
wac.registerBeanDefinition("handlerMapping", mappingDef);
}
ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
factoryBean.setFavorPathExtension(true);
factoryBean.afterPropertiesSet();
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
wac.registerBeanDefinition("handlerAdapter", adapterDef);
});
byte[] content = "alert('boo')".getBytes(StandardCharsets.ISO_8859_1);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a1.html");
request.setContent(content);
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
if (!usePathPatterns) {
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getContentType()).isEqualTo("text/html;charset=ISO-8859-1");
assertThat(response.getHeader("Content-Disposition")).isEqualTo("inline;filename=f.txt");
assertThat(response.getContentAsByteArray()).isEqualTo(content);
} else {
assertThat(response.getStatus()).as("Suffixes pattern matching should not work with PathPattern's").isEqualTo(404);
}
}
use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseBodyWildCardMediaType.
@PathPatternsParameterizedTest
void responseBodyWildCardMediaType(boolean usePathPatterns) throws Exception {
initDispatcherServlet(RequestResponseBodyController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
String requestBody = "Hello World";
request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
request.addHeader("Content-Type", "text/plain; charset=utf-8");
request.addHeader("Accept", "*/*");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo(requestBody);
}
use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method dataClassBindingWithMultipartFile.
@PathPatternsParameterizedTest
void dataClassBindingWithMultipartFile(boolean usePathPatterns) throws Exception {
initDispatcherServlet(MultipartFileDataClassController.class, usePathPatterns);
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
request.setRequestURI("/bind");
request.addFile(new MockMultipartFile("param1", "value1".getBytes(StandardCharsets.UTF_8)));
request.addParameter("param2", "true");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("value1-true-0");
}
use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method produces.
@PathPatternsParameterizedTest
void produces(boolean usePathPatterns) throws Exception {
initDispatcherServlet(ProducesController.class, usePathPatterns, wac -> {
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
converters.add(new Jaxb2RootElementHttpMessageConverter());
RootBeanDefinition beanDef;
beanDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
beanDef.getPropertyValues().add("messageConverters", converters);
wac.registerBeanDefinition("handlerAdapter", beanDef);
beanDef = new RootBeanDefinition(ExceptionHandlerExceptionResolver.class);
beanDef.getPropertyValues().add("messageConverters", converters);
wac.registerBeanDefinition("requestMappingResolver", beanDef);
});
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "text/html");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("html");
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "application/xml");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("xml");
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "application/xml, text/html");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("xml");
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "text/html;q=0.9, application/xml");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("xml");
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "application/msword");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(406);
// SPR-16318
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "text/csv,application/problem+json");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(500);
assertThat(response.getContentType()).isEqualTo("application/problem+json");
assertThat(response.getContentAsString()).isEqualTo("{\"reason\":\"error\"}");
}
Aggregations