Search in sources :

Example 1 with MockServletContext

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

the class ResourceHttpRequestHandlerTests method getMediaTypeWithFavorPathExtensionOff.

// SPR-14577
@Test
@SuppressWarnings("deprecation")
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
    ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
    factory.setFavorPathExtension(false);
    factory.afterPropertiesSet();
    ContentNegotiationManager manager = factory.getObject();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
    handler.setServletContext(new MockServletContext());
    handler.setLocations(paths);
    handler.setContentNegotiationManager(manager);
    handler.afterPropertiesSet();
    this.request.addHeader("Accept", "application/json,text/plain,*/*");
    this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html");
    handler.handleRequest(this.request, this.response);
    assertThat(this.response.getContentType()).isEqualTo("text/html");
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) ClassPathResource(org.springframework.core.io.ClassPathResource) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 2 with MockServletContext

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

the class ResourceHttpRequestHandlerTests method getResourceWithMediaTypeResolvedThroughServletContext.

// SPR-14368
@Test
public void getResourceWithMediaTypeResolvedThroughServletContext() throws Exception {
    MockServletContext servletContext = new MockServletContext() {

        @Override
        public String getMimeType(String filePath) {
            return "foo/bar";
        }
    };
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
    handler.setServletContext(servletContext);
    handler.setLocations(paths);
    handler.afterPropertiesSet();
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "");
    request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
    handler.handleRequest(request, this.response);
    assertThat(this.response.getContentType()).isEqualTo("foo/bar");
    assertThat(this.response.getContentAsString()).isEqualTo("h1 { color:red; }");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 3 with MockServletContext

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

the class ResourceUrlProviderTests method initializeOnCurrentContext.

@Test
@SuppressWarnings("resource")
void initializeOnCurrentContext() {
    AnnotationConfigWebApplicationContext parentContext = new AnnotationConfigWebApplicationContext();
    parentContext.setServletContext(new MockServletContext());
    parentContext.register(ParentHandlerMappingConfiguration.class);
    AnnotationConfigWebApplicationContext childContext = new AnnotationConfigWebApplicationContext();
    childContext.setParent(parentContext);
    childContext.setServletContext(new MockServletContext());
    childContext.register(HandlerMappingConfiguration.class);
    parentContext.refresh();
    childContext.refresh();
    ResourceUrlProvider parentUrlProvider = parentContext.getBean(ResourceUrlProvider.class);
    assertThat(parentUrlProvider.getHandlerMap()).isEmpty();
    assertThat(parentUrlProvider.isAutodetect()).isTrue();
    ResourceUrlProvider childUrlProvider = childContext.getBean(ResourceUrlProvider.class);
    assertThat(childUrlProvider.getHandlerMap()).containsOnlyKeys("/resources/**");
    assertThat(childUrlProvider.isAutodetect()).isFalse();
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 4 with MockServletContext

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

the class RequestScopedControllerAdviceIntegrationTests method loadContextWithRequestScopedControllerAdvice.

// gh-23985
@Test
void loadContextWithRequestScopedControllerAdvice() {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(Config.class);
    assertThatCode(context::refresh).doesNotThrowAnyException();
    List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(context);
    assertThat(adviceBeans).hasSize(1);
    // 
    assertThat(adviceBeans.get(0)).returns(RequestScopedControllerAdvice.class, // 
    ControllerAdviceBean::getBeanType).returns(42, ControllerAdviceBean::getOrder);
    context.close();
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ControllerAdviceBean(org.springframework.web.method.ControllerAdviceBean) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 5 with MockServletContext

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

the class ServletContextSupportTests method testServletContextResourcePatternResolverWithUnboundedPatternPath.

@Test
public void testServletContextResourcePatternResolverWithUnboundedPatternPath() throws IOException {
    final Set<String> dirs = new HashSet<>();
    dirs.add("/WEB-INF/mydir1/");
    dirs.add("/WEB-INF/mydir2/");
    final Set<String> paths = new HashSet<>();
    paths.add("/WEB-INF/mydir2/context2.xml");
    paths.add("/WEB-INF/mydir2/mydir3/");
    MockServletContext sc = new MockServletContext("classpath:org/springframework/web/context") {

        @Override
        public Set<String> getResourcePaths(String path) {
            if ("/WEB-INF/".equals(path)) {
                return dirs;
            }
            if ("/WEB-INF/mydir1/".equals(path)) {
                return Collections.singleton("/WEB-INF/mydir1/context1.xml");
            }
            if ("/WEB-INF/mydir2/".equals(path)) {
                return paths;
            }
            if ("/WEB-INF/mydir2/mydir3/".equals(path)) {
                return Collections.singleton("/WEB-INF/mydir2/mydir3/context3.xml");
            }
            return null;
        }
    };
    ServletContextResourcePatternResolver rpr = new ServletContextResourcePatternResolver(sc);
    Resource[] found = rpr.getResources("/WEB-INF/**/*.xml");
    Set<String> foundPaths = new HashSet<>();
    for (Resource resource : found) {
        foundPaths.add(((ServletContextResource) resource).getPath());
    }
    assertThat(foundPaths.size()).isEqualTo(3);
    assertThat(foundPaths.contains("/WEB-INF/mydir1/context1.xml")).isTrue();
    assertThat(foundPaths.contains("/WEB-INF/mydir2/context2.xml")).isTrue();
    assertThat(foundPaths.contains("/WEB-INF/mydir2/mydir3/context3.xml")).isTrue();
}
Also used : Resource(org.springframework.core.io.Resource) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)138 Test (org.junit.jupiter.api.Test)112 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)44 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)38 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)24 ServletContext (jakarta.servlet.ServletContext)22 ServletContextEvent (jakarta.servlet.ServletContextEvent)21 BeforeEach (org.junit.jupiter.api.BeforeEach)17 WebApplicationContext (org.springframework.web.context.WebApplicationContext)17 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)14 MockServletConfig (org.springframework.web.testfixture.servlet.MockServletConfig)13 Resource (org.springframework.core.io.Resource)12 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)12 HashMap (java.util.HashMap)11 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 TestBean (org.springframework.beans.testfixture.beans.TestBean)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)9 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)7