Search in sources :

Example 56 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class CachingResourceResolverTests method resolveResourceMatchingEncoding.

@Test
public void resolveResourceMatchingEncoding() {
    Resource resource = Mockito.mock(Resource.class);
    Resource gzResource = Mockito.mock(Resource.class);
    this.cache.put(CachingResourceResolver.RESOLVED_RESOURCE_CACHE_KEY_PREFIX + "bar.css", resource);
    this.cache.put(CachingResourceResolver.RESOLVED_RESOURCE_CACHE_KEY_PREFIX + "bar.css+encoding=gzip", gzResource);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "bar.css");
    assertSame(resource, this.chain.resolveResource(request, "bar.css", this.locations));
    request = new MockHttpServletRequest("GET", "bar.css");
    request.addHeader("Accept-Encoding", "gzip");
    assertSame(gzResource, this.chain.resolveResource(request, "bar.css", this.locations));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) Test(org.junit.Test)

Example 57 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class CachingResourceResolverTests method resolveResourceAcceptEncodingInCacheKey.

@Test
public void resolveResourceAcceptEncodingInCacheKey() {
    String file = "bar.css";
    MockHttpServletRequest request = new MockHttpServletRequest("GET", file);
    request.addHeader("Accept-Encoding", "gzip");
    Resource expected = this.chain.resolveResource(request, file, this.locations);
    String cacheKey = CachingResourceResolver.RESOLVED_RESOURCE_CACHE_KEY_PREFIX + file + "+encoding=gzip";
    assertEquals(expected, this.cache.get(cacheKey).get());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) Test(org.junit.Test)

Example 58 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class CachingResourceResolverTests method resolveResourceNoAcceptEncodingInCacheKey.

@Test
public void resolveResourceNoAcceptEncodingInCacheKey() {
    String file = "bar.css";
    MockHttpServletRequest request = new MockHttpServletRequest("GET", file);
    Resource expected = this.chain.resolveResource(request, file, this.locations);
    String cacheKey = CachingResourceResolver.RESOLVED_RESOURCE_CACHE_KEY_PREFIX + file;
    assertEquals(expected, this.cache.get(cacheKey).get());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) Test(org.junit.Test)

Example 59 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class ContentBasedVersionStrategyTests method getResourceVersion.

@Test
public void getResourceVersion() throws Exception {
    Resource expected = new ClassPathResource("test/bar.css", getClass());
    String hash = DigestUtils.md5DigestAsHex(FileCopyUtils.copyToByteArray(expected.getInputStream()));
    assertEquals(hash, this.versionStrategy.getResourceVersion(expected));
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 60 with Resource

use of org.springframework.core.io.Resource in project spring-framework by spring-projects.

the class CssLinkResourceTransformerTests method transform.

@Test
public void transform() throws Exception {
    this.request = new MockHttpServletRequest("GET", "/static/main.css");
    Resource css = new ClassPathResource("test/main.css", getClass());
    TransformedResource actual = (TransformedResource) this.transformerChain.transform(this.request, css);
    String expected = "\n" + "@import url(\"/static/bar-11e16cf79faee7ac698c805cf28248d2.css\");\n" + "@import url('/static/bar-11e16cf79faee7ac698c805cf28248d2.css');\n" + "@import url(/static/bar-11e16cf79faee7ac698c805cf28248d2.css);\n\n" + "@import \"/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css\";\n" + "@import '/static/foo-e36d2e05253c6c7085a91522ce43a0b4.css';\n\n" + "body { background: url(\"/static/images/image-f448cd1d5dba82b774f3202c878230b3.png\") }\n";
    String result = new String(actual.getByteArray(), StandardCharsets.UTF_8);
    result = StringUtils.deleteAny(result, "\r");
    assertEquals(expected, result);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

Resource (org.springframework.core.io.Resource)610 Test (org.junit.Test)257 ClassPathResource (org.springframework.core.io.ClassPathResource)231 IOException (java.io.IOException)103 FileSystemResource (org.springframework.core.io.FileSystemResource)77 UrlResource (org.springframework.core.io.UrlResource)68 File (java.io.File)64 ArrayList (java.util.ArrayList)58 ByteArrayResource (org.springframework.core.io.ByteArrayResource)49 InputStream (java.io.InputStream)46 InputStreamResource (org.springframework.core.io.InputStreamResource)31 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)30 URL (java.net.URL)25 HashMap (java.util.HashMap)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)18 ServletContextResource (org.springframework.web.context.support.ServletContextResource)18 Map (java.util.Map)17 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)17 ResourceLoader (org.springframework.core.io.ResourceLoader)16 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)16