Search in sources :

Example 31 with PathPatternsParameterizedTest

use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest 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>");
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Jaxb2RootElementHttpMessageConverter(org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MarshallingHttpMessageConverter(org.springframework.http.converter.xml.MarshallingHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 32 with PathPatternsParameterizedTest

use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest 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);
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MarshallingHttpMessageConverter(org.springframework.http.converter.xml.MarshallingHttpMessageConverter) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) ServletException(jakarta.servlet.ServletException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 33 with PathPatternsParameterizedTest

use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest 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);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 34 with PathPatternsParameterizedTest

use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest 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");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ArrayList(java.util.ArrayList) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 35 with PathPatternsParameterizedTest

use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest 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");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Aggregations

PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)195 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)171 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)142 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)37 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)19 CorsConfiguration (org.springframework.web.cors.CorsConfiguration)13 ModelAndView (org.springframework.web.servlet.ModelAndView)12 HttpSession (jakarta.servlet.http.HttpSession)7 Map (java.util.Map)7 MultiValueMap (org.springframework.util.MultiValueMap)7 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)6 ModelMap (org.springframework.ui.ModelMap)5 HandlerMethod (org.springframework.web.method.HandlerMethod)5 InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)5 ArrayList (java.util.ArrayList)4 MarshallingHttpMessageConverter (org.springframework.http.converter.xml.MarshallingHttpMessageConverter)4 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)4 RequestMappingInfo (org.springframework.web.servlet.mvc.method.RequestMappingInfo)4 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)4 MockMultipartHttpServletRequest (org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest)4