use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseAsHttpHeaders.
@PathPatternsParameterizedTest
void responseAsHttpHeaders(boolean usePathPatterns) throws Exception {
initDispatcherServlet(HttpHeadersResponseController.class, usePathPatterns);
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(new MockHttpServletRequest("POST", "/"), response);
assertThat(response.getStatus()).as("Wrong status code").isEqualTo(MockHttpServletResponse.SC_CREATED);
assertThat(response.getHeaderNames().size()).as("Wrong number of headers").isEqualTo(1);
assertThat(response.getHeader("location")).as("Wrong value for 'location' header").isEqualTo("/test/items/123");
assertThat(response.getContentLength()).as("Expected an empty content").isEqualTo(0);
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method typedCommandProvidingFormController.
@PathPatternsParameterizedTest
void typedCommandProvidingFormController(boolean usePathPatterns) throws Exception {
initDispatcherServlet(MyTypedCommandProvidingFormController.class, usePathPatterns, wac -> {
wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(TestViewResolver.class));
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
adapterDef.getPropertyValues().add("webBindingInitializer", new MyWebBindingInitializer());
List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
argumentResolvers.add(new ServletWebArgumentResolverAdapter(new MySpecialArgumentResolver()));
adapterDef.getPropertyValues().add("customArgumentResolvers", argumentResolvers);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
});
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
request.addParameter("defaultName", "10");
request.addParameter("age", "value2");
request.addParameter("date", "2007-10-02");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myView-Integer:10-typeMismatch-tb1-myOriginalValue");
request = new MockHttpServletRequest("GET", "/myOtherPath.do");
request.addParameter("defaultName", "10");
request.addParameter("age", "value2");
request.addParameter("date", "2007-10-02");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myView-myName-typeMismatch-tb1-myOriginalValue");
request = new MockHttpServletRequest("GET", "/myThirdPath.do");
request.addParameter("defaultName", "10");
request.addParameter("age", "100");
request.addParameter("date", "2007-10-02");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myView-special-99-special-99");
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method dataClassBindingWithOptional.
@PathPatternsParameterizedTest
void dataClassBindingWithOptional(boolean usePathPatterns) throws Exception {
initDispatcherServlet(OptionalDataClassController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bind");
request.addParameter("param1", "value1");
request.addParameter("param2", "true");
request.addParameter("param3", "3");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("value1-true-3");
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class UriTemplateServletAnnotationControllerHandlerMethodTests method explicitSubPath.
@PathPatternsParameterizedTest
void explicitSubPath(boolean usePathPatterns) throws Exception {
initDispatcherServlet(ExplicitSubPathController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/42");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("test-42");
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class UriTemplateServletAnnotationControllerHandlerMethodTests method ambiguous.
@PathPatternsParameterizedTest
void ambiguous(boolean usePathPatterns) throws Exception {
initDispatcherServlet(AmbiguousUriTemplateController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/new");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("specific");
}
Aggregations