Search in sources :

Example 11 with MockServletConfig

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

the class ServletContextAwareProcessorTests method servletConfigAwareWithNullServletContextAndNonNullServletConfig.

@Test
public void servletConfigAwareWithNullServletContextAndNonNullServletConfig() {
    ServletContext servletContext = new MockServletContext();
    ServletConfig servletConfig = new MockServletConfig(servletContext);
    ServletContextAwareProcessor processor = new ServletContextAwareProcessor(null, servletConfig);
    ServletConfigAwareBean bean = new ServletConfigAwareBean();
    assertThat(bean.getServletConfig()).isNull();
    processor.postProcessBeforeInitialization(bean, "testBean");
    assertThat(bean.getServletConfig()).as("ServletConfig should have been set").isNotNull();
    assertThat(bean.getServletConfig()).isEqualTo(servletConfig);
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) ServletConfig(jakarta.servlet.ServletConfig) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 12 with MockServletConfig

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

the class HttpRequestHandlerTests method testHttpRequestHandlerServletPassThrough.

@Test
public void testHttpRequestHandlerServletPassThrough() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.getBeanFactory().registerSingleton("myHandler", (HttpRequestHandler) (req, res) -> {
        assertThat(req).isSameAs(request);
        assertThat(res).isSameAs(response);
        String exception = request.getParameter("exception");
        if ("ServletException".equals(exception)) {
            throw new ServletException("test");
        }
        if ("IOException".equals(exception)) {
            throw new IOException("test");
        }
        res.getWriter().write("myResponse");
    });
    wac.setServletContext(servletContext);
    wac.refresh();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    Servlet servlet = new HttpRequestHandlerServlet();
    servlet.init(new MockServletConfig(servletContext, "myHandler"));
    servlet.service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myResponse");
    request.setParameter("exception", "ServletException");
    assertThatExceptionOfType(ServletException.class).isThrownBy(() -> servlet.service(request, response)).withMessage("test");
    request.setParameter("exception", "IOException");
    assertThatIOException().isThrownBy(() -> servlet.service(request, response)).withMessage("test");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WebApplicationContext(org.springframework.web.context.WebApplicationContext) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) Test(org.junit.jupiter.api.Test) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Servlet(jakarta.servlet.Servlet) HttpRequestHandler(org.springframework.web.HttpRequestHandler) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletException(jakarta.servlet.ServletException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Servlet(jakarta.servlet.Servlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) IOException(java.io.IOException) Assertions.assertThatIOException(org.assertj.core.api.Assertions.assertThatIOException) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 13 with MockServletConfig

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

the class AbstractServletHandlerMethodTests method initDispatcherServlet.

@SuppressWarnings("serial")
WebApplicationContext initDispatcherServlet(@Nullable Class<?> controllerClass, boolean usePathPatterns, @Nullable ApplicationContextInitializer<GenericWebApplicationContext> initializer) throws ServletException {
    final GenericWebApplicationContext wac = new GenericWebApplicationContext();
    servlet = new DispatcherServlet() {

        @Override
        protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) {
            if (controllerClass != null) {
                wac.registerBeanDefinition(controllerClass.getSimpleName(), new RootBeanDefinition(controllerClass));
            }
            if (initializer != null) {
                initializer.initialize(wac);
            }
            if (!wac.containsBeanDefinition("handlerMapping")) {
                BeanDefinition def = register("handlerMapping", RequestMappingHandlerMapping.class, wac);
                def.getPropertyValues().add("removeSemicolonContent", "false");
            }
            BeanDefinition mappingDef = wac.getBeanDefinition("handlerMapping");
            if (usePathPatterns && !mappingDef.hasAttribute("patternParser")) {
                BeanDefinition parserDef = register("parser", PathPatternParser.class, wac);
                mappingDef.getPropertyValues().add("patternParser", parserDef);
            }
            register("handlerAdapter", RequestMappingHandlerAdapter.class, wac);
            register("requestMappingResolver", ExceptionHandlerExceptionResolver.class, wac);
            register("responseStatusResolver", ResponseStatusExceptionResolver.class, wac);
            register("defaultResolver", DefaultHandlerExceptionResolver.class, wac);
            wac.refresh();
            return wac;
        }
    };
    servlet.init(new MockServletConfig());
    return wac;
}
Also used : ResponseStatusExceptionResolver(org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) WebApplicationContext(org.springframework.web.context.WebApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Example 14 with MockServletConfig

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

the class ResponseEntityExceptionHandlerTests method controllerAdviceWithinDispatcherServlet.

@Test
public void controllerAdviceWithinDispatcherServlet() throws Exception {
    StaticWebApplicationContext ctx = new StaticWebApplicationContext();
    ctx.registerSingleton("controller", ExceptionThrowingController.class);
    ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
    ctx.refresh();
    DispatcherServlet servlet = new DispatcherServlet(ctx);
    servlet.init(new MockServletConfig());
    servlet.service(this.servletRequest, this.servletResponse);
    assertThat(this.servletResponse.getStatus()).isEqualTo(400);
    assertThat(this.servletResponse.getContentAsString()).isEqualTo("error content");
    assertThat(this.servletResponse.getHeader("someHeader")).isEqualTo("someHeaderValue");
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 15 with MockServletConfig

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

the class ServletAnnotationControllerHandlerMethodTests method nullCommandController.

@PathPatternsParameterizedTest
void nullCommandController(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(MyNullCommandController.class, usePathPatterns);
    getServlet().init(new MockServletConfig());
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath");
    request.setUserPrincipal(new OtherPrincipal());
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myView");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

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