Search in sources :

Example 86 with PathPatternsParameterizedTest

use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method ambiguousParams.

@PathPatternsParameterizedTest
void ambiguousParams(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(AmbiguousParamsController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("noParams");
    request = new MockHttpServletRequest("GET", "/test");
    request.addParameter("myParam", "42");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myParam-42");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 87 with PathPatternsParameterizedTest

use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method relativePathDispatchingController.

@PathPatternsParameterizedTest
void relativePathDispatchingController(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(MyRelativePathDispatchingController.class, usePathPatterns, wac -> {
        if (!usePathPatterns) {
            RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
            mappingDef.getPropertyValues().add("useSuffixPatternMatch", true);
            wac.registerBeanDefinition("handlerMapping", mappingDef);
        }
    });
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myApp/myHandle");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myView");
    request = new MockHttpServletRequest("GET", "/myApp/myOther");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myOtherView");
    request = new MockHttpServletRequest("GET", "/myApp/myLang");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myLangView");
    request = new MockHttpServletRequest("GET", "/myApp/surprise.do");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo(!usePathPatterns ? "mySurpriseView" : "myView");
}
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)

Example 88 with PathPatternsParameterizedTest

use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method methodNotAllowed.

@PathPatternsParameterizedTest
void methodNotAllowed(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(MethodNotAllowedController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).as("Invalid response status").isEqualTo(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
    String allowHeader = response.getHeader("Allow");
    assertThat(allowHeader).as("No Allow header").isNotNull();
    Set<String> allowedMethods = new HashSet<>(Arrays.asList(StringUtils.delimitedListToStringArray(allowHeader, ", ")));
    assertThat(allowedMethods.size()).as("Invalid amount of supported methods").isEqualTo(6);
    assertThat(allowedMethods.contains("PUT")).as("PUT not allowed").isTrue();
    assertThat(allowedMethods.contains("DELETE")).as("DELETE not allowed").isTrue();
    assertThat(allowedMethods.contains("HEAD")).as("HEAD not allowed").isTrue();
    assertThat(allowedMethods.contains("TRACE")).as("TRACE not allowed").isTrue();
    assertThat(allowedMethods.contains("OPTIONS")).as("OPTIONS not allowed").isTrue();
    assertThat(allowedMethods.contains("POST")).as("POST not allowed").isTrue();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) HashSet(java.util.HashSet) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 89 with PathPatternsParameterizedTest

use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method acceptHeaders.

@PathPatternsParameterizedTest
void acceptHeaders(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(AcceptHeadersController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/something");
    request.addHeader("Accept", "text/html");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("html");
    request = new MockHttpServletRequest("GET", "/something");
    request.addHeader("Accept", "application/xml");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("xml");
    request = new MockHttpServletRequest("GET", "/something");
    request.addHeader("Accept", "application/xml, text/html");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("xml");
    request = new MockHttpServletRequest("GET", "/something");
    request.addHeader("Accept", "text/html;q=0.9, application/xml");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("xml");
    request = new MockHttpServletRequest("GET", "/something");
    request.addHeader("Accept", "application/msword");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(406);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 90 with PathPatternsParameterizedTest

use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method emptyParameterListHandleMethod.

@PathPatternsParameterizedTest
void emptyParameterListHandleMethod(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(EmptyParameterListHandlerMethodController.class, usePathPatterns, wac -> {
        RootBeanDefinition vrDef = new RootBeanDefinition(InternalResourceViewResolver.class);
        vrDef.getPropertyValues().add("suffix", ".jsp");
        wac.registerBeanDefinition("viewResolver", vrDef);
    });
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/emptyParameterListHandler");
    MockHttpServletResponse response = new MockHttpServletResponse();
    EmptyParameterListHandlerMethodController.called = false;
    getServlet().service(request, response);
    assertThat(EmptyParameterListHandlerMethodController.called).isTrue();
    assertThat(response.getContentAsString()).isEqualTo("");
}
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