use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method optionalParamMissing.
@PathPatternsParameterizedTest
void optionalParamMissing(boolean usePathPatterns) throws Exception {
initDispatcherServlet(OptionalParamController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("null-false-null");
}
use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method sessionAttributeExposureWithInterface.
@SuppressWarnings("rawtypes")
@PathPatternsParameterizedTest
void sessionAttributeExposureWithInterface(boolean usePathPatterns) throws Exception {
initDispatcherServlet(MySessionAttributesControllerImpl.class, usePathPatterns, wac -> {
wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(ModelExposingViewResolver.class));
DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
autoProxyCreator.setBeanFactory(wac.getBeanFactory());
wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor()));
});
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPage");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(request.getAttribute("viewName")).isEqualTo("page1");
HttpSession session = request.getSession();
assertThat(session).isNotNull();
assertThat(session.getAttribute("object1") != null).isTrue();
assertThat(session.getAttribute("object2") != null).isTrue();
assertThat(((Map) session.getAttribute("model")).containsKey("object1")).isTrue();
assertThat(((Map) session.getAttribute("model")).containsKey("object2")).isTrue();
request = new MockHttpServletRequest("POST", "/myPage");
request.setSession(session);
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(request.getAttribute("viewName")).isEqualTo("page2");
assertThat(session.getAttribute("object1") != null).isTrue();
assertThat(session.getAttribute("object2") != null).isTrue();
assertThat(((Map) session.getAttribute("model")).containsKey("object1")).isTrue();
assertThat(((Map) session.getAttribute("model")).containsKey("object2")).isTrue();
}
use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method httpOptions.
@PathPatternsParameterizedTest
void httpOptions(boolean usePathPatterns) throws Exception {
initDispatcherServlet(ResponseEntityController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/baz");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getHeader("Allow")).isEqualTo("GET,HEAD,OPTIONS");
assertThat(response.getContentAsByteArray().length == 0).isTrue();
}
use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method badRequestRequestBody.
@PathPatternsParameterizedTest
void badRequestRequestBody(boolean usePathPatterns) throws Exception {
initDispatcherServlet(RequestResponseBodyController.class, usePathPatterns, wac -> {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
adapterDef.getPropertyValues().add("messageConverters", new NotReadableMessageConverter());
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()).as("Invalid response status code").isEqualTo(HttpServletResponse.SC_BAD_REQUEST);
}
use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method dataClassBindingWithFieldMarker.
@PathPatternsParameterizedTest
void dataClassBindingWithFieldMarker(boolean usePathPatterns) throws Exception {
initDispatcherServlet(DataClassController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bind");
request.addParameter("param1", "value1");
request.addParameter("param2", "true");
request.addParameter("_param2", "on");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("value1-true-0");
}
Aggregations