CachingResourceResolver varies by known codings only
Issue: SPR-16381
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.web.servlet.resource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -73,7 +74,7 @@ public class CachingResourceResolverTests {
|
||||
@Test
|
||||
public void resolveResourceInternalFromCache() {
|
||||
Resource expected = Mockito.mock(Resource.class);
|
||||
this.cache.put(getCacheKey("bar.css"), expected);
|
||||
this.cache.put(resourceKey("bar.css"), expected);
|
||||
Resource actual = this.chain.resolveResource(null, "bar.css", this.locations);
|
||||
|
||||
assertSame(expected, actual);
|
||||
@@ -107,16 +108,36 @@ public class CachingResourceResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveResourceAcceptEncodingInCacheKey() {
|
||||
public void resolveResourceAcceptEncodingInCacheKey() throws IOException {
|
||||
|
||||
String file = "bar.css";
|
||||
EncodedResourceResolverTests.createGzippedFile(file);
|
||||
|
||||
// 1. Resolve plain resource
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", file);
|
||||
request.addHeader("Accept-Encoding", "gzip ; a=b , deflate , brotli ; c=d ");
|
||||
Resource expected = this.chain.resolveResource(request, file, this.locations);
|
||||
|
||||
String cacheKey = getCacheKey(file + "+encoding=brotli,deflate,gzip");
|
||||
Object actual = this.cache.get(cacheKey).get();
|
||||
String cacheKey = resourceKey(file);
|
||||
assertSame(expected, this.cache.get(cacheKey).get());
|
||||
|
||||
assertSame(expected, actual);
|
||||
// 2. Resolve with Accept-Encoding
|
||||
|
||||
request = new MockHttpServletRequest("GET", file);
|
||||
request.addHeader("Accept-Encoding", "gzip ; a=b , deflate , br ; c=d ");
|
||||
expected = this.chain.resolveResource(request, file, this.locations);
|
||||
|
||||
cacheKey = resourceKey(file + "+encoding=br,gzip");
|
||||
assertSame(expected, this.cache.get(cacheKey).get());
|
||||
|
||||
// 3. Resolve with Accept-Encoding but no matching codings
|
||||
|
||||
request = new MockHttpServletRequest("GET", file);
|
||||
request.addHeader("Accept-Encoding", "deflate");
|
||||
expected = this.chain.resolveResource(request, file, this.locations);
|
||||
|
||||
cacheKey = resourceKey(file);
|
||||
assertSame(expected, this.cache.get(cacheKey).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -125,7 +146,7 @@ public class CachingResourceResolverTests {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", file);
|
||||
Resource expected = this.chain.resolveResource(request, file, this.locations);
|
||||
|
||||
String cacheKey = getCacheKey(file);
|
||||
String cacheKey = resourceKey(file);
|
||||
Object actual = this.cache.get(cacheKey).get();
|
||||
|
||||
assertEquals(expected, actual);
|
||||
@@ -135,8 +156,8 @@ public class CachingResourceResolverTests {
|
||||
public void resolveResourceMatchingEncoding() {
|
||||
Resource resource = Mockito.mock(Resource.class);
|
||||
Resource gzipped = Mockito.mock(Resource.class);
|
||||
this.cache.put(getCacheKey("bar.css"), resource);
|
||||
this.cache.put(getCacheKey("bar.css+encoding=gzip"), gzipped);
|
||||
this.cache.put(resourceKey("bar.css"), resource);
|
||||
this.cache.put(resourceKey("bar.css+encoding=gzip"), gzipped);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "bar.css");
|
||||
assertSame(resource, this.chain.resolveResource(request,"bar.css", this.locations));
|
||||
@@ -146,7 +167,7 @@ public class CachingResourceResolverTests {
|
||||
assertSame(gzipped, this.chain.resolveResource(request, "bar.css", this.locations));
|
||||
}
|
||||
|
||||
private static String getCacheKey(String key) {
|
||||
private static String resourceKey(String key) {
|
||||
return CachingResourceResolver.RESOLVED_RESOURCE_CACHE_KEY_PREFIX + key;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user