Search in sources :

Example 91 with PathPatternsParameterizedTest

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");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 92 with PathPatternsParameterizedTest

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

Example 93 with PathPatternsParameterizedTest

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);
    }
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 94 with PathPatternsParameterizedTest

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

Example 95 with PathPatternsParameterizedTest

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");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) 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