use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method unsupportedRequestBody.
@PathPatternsParameterizedTest
void unsupportedRequestBody(boolean usePathPatterns) throws Exception {
initDispatcherServlet(RequestResponseBodyController.class, usePathPatterns, wac -> {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
StringHttpMessageConverter converter = new StringHttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_PLAIN));
adapterDef.getPropertyValues().add("messageConverters", converter);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
});
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
String requestBody = "Hello World";
request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
request.addHeader("Content-Type", "application/pdf");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(415);
assertThat(response.getHeader("Accept")).isEqualTo("text/plain");
}
use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method consumes.
@PathPatternsParameterizedTest
void consumes(boolean usePathPatterns) throws Exception {
initDispatcherServlet(ConsumesController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/something");
request.setContentType("application/pdf");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("pdf");
request = new MockHttpServletRequest("POST", "/something");
request.setContentType("text/html");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("text");
request = new MockHttpServletRequest("POST", "/something");
request.setContentType("application/xml");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(415);
}
use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseBodyAsHtmlWithProducesCondition.
@PathPatternsParameterizedTest
void responseBodyAsHtmlWithProducesCondition(boolean usePathPatterns) throws Exception {
initDispatcherServlet(TextRestController.class, usePathPatterns, wac -> {
if (!usePathPatterns) {
RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
mappingDef.getPropertyValues().add("useSuffixPatternMatch", true);
wac.registerBeanDefinition("handlerMapping", mappingDef);
}
ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
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", "/a3.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")).isNull();
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.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method multipartFilesAsStringArray.
@PathPatternsParameterizedTest
void multipartFilesAsStringArray(boolean usePathPatterns) throws Exception {
initDispatcherServlet(MultipartController.class, usePathPatterns);
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);
assertThat(response.getContentAsString()).isEqualTo("Juergen-Eva");
}
use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method binderInitializingCommandProvidingFormController.
@PathPatternsParameterizedTest
void binderInitializingCommandProvidingFormController(boolean usePathPatterns) throws Exception {
initDispatcherServlet(MyBinderInitializingCommandProvidingFormController.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");
}
Aggregations