Search in sources :

Example 6 with MockHttpServletRequest

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

Example 7 with MockHttpServletRequest

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");
}
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 8 with MockHttpServletRequest

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

Example 9 with MockHttpServletRequest

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

Example 10 with MockHttpServletRequest

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

Aggregations

MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)752 Test (org.junit.jupiter.api.Test)458 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)359 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)180 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)83 BeforeEach (org.junit.jupiter.api.BeforeEach)73 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)57 ModelAndView (org.springframework.web.servlet.ModelAndView)45 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)45 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)39 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)38 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)38 TestBean (org.springframework.beans.testfixture.beans.TestBean)36 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)35 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)31 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)29 Cookie (jakarta.servlet.http.Cookie)27 HttpRequest (org.springframework.http.HttpRequest)27 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)26 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)25