Search in sources :

Example 16 with MockServletConfig

use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method parameterDispatchingController.

@PathPatternsParameterizedTest
void parameterDispatchingController(boolean usePathPatterns) throws Exception {
    final MockServletContext servletContext = new MockServletContext();
    final MockServletConfig servletConfig = new MockServletConfig(servletContext);
    WebApplicationContext webAppContext = initDispatcherServlet(MyParameterDispatchingController.class, usePathPatterns, wac -> {
        wac.setServletContext(servletContext);
        AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
        wac.getBeanFactory().registerResolvableDependency(ServletConfig.class, servletConfig);
    });
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpSession session = request.getSession();
    assertThat(session).isNotNull();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myView");
    assertThat(request.getAttribute("servletContext")).isSameAs(servletContext);
    assertThat(request.getAttribute("servletConfig")).isSameAs(servletConfig);
    assertThat(request.getAttribute("sessionId")).isSameAs(session.getId());
    assertThat(request.getAttribute("requestUri")).isSameAs(request.getRequestURI());
    assertThat(request.getAttribute("locale")).isSameAs(request.getLocale());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    response = new MockHttpServletResponse();
    session = request.getSession();
    assertThat(session).isNotNull();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myView");
    assertThat(request.getAttribute("servletContext")).isSameAs(servletContext);
    assertThat(request.getAttribute("servletConfig")).isSameAs(servletConfig);
    assertThat(request.getAttribute("sessionId")).isSameAs(session.getId());
    assertThat(request.getAttribute("requestUri")).isSameAs(request.getRequestURI());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "other");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myOtherView");
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "my");
    request.addParameter("lang", "de");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myLangView");
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("surprise", "!");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("mySurpriseView");
    MyParameterDispatchingController deserialized = (MyParameterDispatchingController) SerializationTestUtils.serializeAndDeserialize(webAppContext.getBean(MyParameterDispatchingController.class.getSimpleName()));
    assertThat(deserialized.request).isNotNull();
    assertThat(deserialized.session).isNotNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 17 with MockServletConfig

use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method routerFunction.

@Test
void routerFunction() throws ServletException, IOException {
    GenericWebApplicationContext wac = new GenericWebApplicationContext();
    wac.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/foo", request -> ServerResponse.ok().body("foo-body")).build());
    wac.refresh();
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.setApplicationContext(wac);
    servlet.init(new MockServletConfig());
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    MockHttpServletResponse response = new MockHttpServletResponse();
    servlet.service(request, response);
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getContentAsString()).isEqualTo("foo-body");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 18 with MockServletConfig

use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.

the class DispatcherServletTests method detectHandlerMappingFromParent.

@Test
public void detectHandlerMappingFromParent() throws ServletException, IOException {
    // create a parent context that includes a mapping
    StaticWebApplicationContext parent = new StaticWebApplicationContext();
    parent.setServletContext(getServletContext());
    parent.registerSingleton("parentHandler", ControllerFromParent.class, new MutablePropertyValues());
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue(new PropertyValue("mappings", URL_KNOWN_ONLY_PARENT + "=parentHandler"));
    parent.registerSingleton("parentMapping", SimpleUrlHandlerMapping.class, pvs);
    parent.refresh();
    DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
    // will have parent
    complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
    complexDispatcherServlet.setNamespace("test");
    ServletConfig config = new MockServletConfig(getServletContext(), "complex");
    config.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, parent);
    complexDispatcherServlet.init(config);
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT);
    MockHttpServletResponse response = new MockHttpServletResponse();
    complexDispatcherServlet.service(request, response);
    assertThat(response.getStatus() == HttpServletResponse.SC_NOT_FOUND).as("Matched through parent controller/handler pair: not response=" + response.getStatus()).isFalse();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) ServletConfig(jakarta.servlet.ServletConfig) PropertyValue(org.springframework.beans.PropertyValue) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 19 with MockServletConfig

use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.

the class DispatcherServletTests method handlerNotMappedWithAutodetect.

@Test
public void handlerNotMappedWithAutodetect() throws ServletException, IOException {
    DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
    // no parent
    complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
    complexDispatcherServlet.setNamespace("test");
    complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT);
    MockHttpServletResponse response = new MockHttpServletResponse();
    complexDispatcherServlet.service(request, response);
    assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_FOUND);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 20 with MockServletConfig

use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.

the class DispatcherServletTests method notDetectAllViewResolvers.

@Test
public void notDetectAllViewResolvers() throws ServletException, IOException {
    DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
    complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
    complexDispatcherServlet.setNamespace("test");
    complexDispatcherServlet.setDetectAllViewResolvers(false);
    complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    assertThatExceptionOfType(ServletException.class).isThrownBy(() -> complexDispatcherServlet.service(request, response)).withMessageContaining("failed0");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

MockServletConfig (org.springframework.web.testfixture.servlet.MockServletConfig)30 Test (org.junit.jupiter.api.Test)23 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)13 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)13 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)13 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)10 ServletConfig (jakarta.servlet.ServletConfig)7 ServletContext (jakarta.servlet.ServletContext)6 WebApplicationContext (org.springframework.web.context.WebApplicationContext)6 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)6 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)5 ServletException (jakarta.servlet.ServletException)3 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)3 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)3 IOException (java.io.IOException)2 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)2 SimpleTraceInterceptor (org.springframework.aop.interceptor.SimpleTraceInterceptor)2 DefaultPointcutAdvisor (org.springframework.aop.support.DefaultPointcutAdvisor)2 ServletConfigAwareBean (org.springframework.web.context.ServletConfigAwareBean)2