use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method requiredParamMissing.
@PathPatternsParameterizedTest
void requiredParamMissing(boolean usePathPatterns) throws Exception {
WebApplicationContext webAppContext = initDispatcherServlet(RequiredParamController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).as("Invalid response status code").isEqualTo(HttpServletResponse.SC_BAD_REQUEST);
assertThat(webAppContext.isSingleton(RequiredParamController.class.getSimpleName())).isTrue();
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method mavResolver.
@PathPatternsParameterizedTest
void mavResolver(boolean usePathPatterns) throws Exception {
initDispatcherServlet(ModelAndViewResolverController.class, usePathPatterns, wac -> {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
ModelAndViewResolver[] mavResolvers = new ModelAndViewResolver[] { new MyModelAndViewResolver() };
adapterDef.getPropertyValues().add("modelAndViewResolvers", mavResolvers);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
});
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myValue");
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method unsupportedPatchBody.
@PathPatternsParameterizedTest
void unsupportedPatchBody(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("PATCH", "/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-Patch")).isEqualTo("text/plain");
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method httpEntityWithContentType.
// SPR-16172
@PathPatternsParameterizedTest
void httpEntityWithContentType(boolean usePathPatterns) throws Exception {
initDispatcherServlet(ResponseEntityController.class, usePathPatterns, wac -> {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new MappingJackson2HttpMessageConverter());
messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
adapterDef.getPropertyValues().add("messageConverters", messageConverters);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
});
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test-entity");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getHeader("Content-Type")).isEqualTo("application/xml");
assertThat(response.getContentAsString()).isEqualTo("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<testEntity><name>Foo Bar</name></testEntity>");
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseBodyArgMismatch.
@PathPatternsParameterizedTest
void responseBodyArgMismatch(boolean usePathPatterns) throws Exception {
initDispatcherServlet(RequestBodyArgMismatchController.class, usePathPatterns, 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);
});
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
String requestBody = "<b/>";
request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
request.addHeader("Content-Type", "application/xml; charset=utf-8");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(400);
}
Aggregations