use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class CssLinkResourceTransformerTests method setup.
@Before
public void setup() {
ClassPathResource allowedLocation = new ClassPathResource("test/", getClass());
ResourceWebHandler resourceHandler = new ResourceWebHandler();
ResourceUrlProvider resourceUrlProvider = new ResourceUrlProvider();
resourceUrlProvider.setHandlerMap(Collections.singletonMap("/static/**", resourceHandler));
VersionResourceResolver versionResolver = new VersionResourceResolver();
versionResolver.setStrategyMap(Collections.singletonMap("/**", new ContentVersionStrategy()));
PathResourceResolver pathResolver = new PathResourceResolver();
pathResolver.setAllowedLocations(allowedLocation);
List<ResourceResolver> resolvers = Arrays.asList(versionResolver, pathResolver);
CssLinkResourceTransformer cssLinkResourceTransformer = new CssLinkResourceTransformer();
cssLinkResourceTransformer.setResourceUrlProvider(resourceUrlProvider);
List<ResourceTransformer> transformers = Collections.singletonList(cssLinkResourceTransformer);
resourceHandler.setResourceResolvers(resolvers);
resourceHandler.setResourceTransformers(transformers);
resourceHandler.setLocations(Collections.singletonList(allowedLocation));
ResourceResolverChain resolverChain = new DefaultResourceResolverChain(resolvers);
this.transformerChain = new DefaultResourceTransformerChain(resolverChain, transformers);
}
use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class CssLinkResourceTransformerTests method transformWithGzippedResource.
@Test
public void transformWithGzippedResource() throws Exception {
MockServerWebExchange exchange = MockServerHttpRequest.get("/static/main.css").toExchange();
Resource original = new ClassPathResource("test/main.css", getClass());
createTempCopy("main.css", "main.css.gz");
GzipResourceResolver.GzippedResource expected = new GzipResourceResolver.GzippedResource(original);
StepVerifier.create(this.transformerChain.transform(exchange, expected)).expectNext(expected).expectComplete().verify();
}
use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class GzipResourceResolverTests method resolveWithNullRequest.
// SPR-13149
@Test
public void resolveWithNullRequest() throws IOException {
String file = "js/foo.js";
Resource resolved = this.resolver.resolveResource(null, file, this.locations).block(TIMEOUT);
String gzFile = file + ".gz";
Resource gzResource = new ClassPathResource("test/" + gzFile, getClass());
assertEquals(gzResource.getDescription(), resolved.getDescription());
assertEquals(new ClassPathResource("test/" + file).getFilename(), resolved.getFilename());
assertTrue("Expected " + resolved + " to be of type " + HttpResource.class, resolved instanceof HttpResource);
}
use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class GzipResourceResolverTests method createGzFile.
private static void createGzFile(String filePath) throws IOException {
Resource location = new ClassPathResource("test/", GzipResourceResolverTests.class);
Resource fileResource = new FileSystemResource(location.createRelative(filePath).getFile());
Path gzFilePath = Paths.get(fileResource.getFile().getAbsolutePath() + ".gz");
Files.deleteIfExists(gzFilePath);
File gzFile = Files.createFile(gzFilePath).toFile();
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile));
FileCopyUtils.copy(fileResource.getInputStream(), out);
gzFile.deleteOnExit();
}
use of org.springframework.core.io.ClassPathResource in project spring-framework by spring-projects.
the class GzipResourceResolverTests method resolveFromCacheWithEncodingVariants.
@Test
public void resolveFromCacheWithEncodingVariants() throws IOException {
MockServerWebExchange exchange = MockServerHttpRequest.get("").header("Accept-Encoding", "gzip").toExchange();
String file = "js/foo.js";
Resource resolved = this.resolver.resolveResource(exchange, file, this.locations).block(TIMEOUT);
String gzFile = file + ".gz";
Resource gzResource = new ClassPathResource("test/" + gzFile, getClass());
assertEquals(gzResource.getDescription(), resolved.getDescription());
assertEquals(new ClassPathResource("test/" + file).getFilename(), resolved.getFilename());
assertTrue("Expected " + resolved + " to be of type " + HttpResource.class, resolved instanceof HttpResource);
// resolved resource is now cached in CachingResourceResolver
exchange = MockServerHttpRequest.get("/js/foo.js").toExchange();
resolved = this.resolver.resolveResource(exchange, file, this.locations).block(TIMEOUT);
Resource resource = new ClassPathResource("test/" + file, getClass());
assertEquals(resource.getDescription(), resolved.getDescription());
assertEquals(new ClassPathResource("test/" + file).getFilename(), resolved.getFilename());
assertFalse("Expected " + resolved + " to *not* be of type " + HttpResource.class, resolved instanceof HttpResource);
}
Aggregations