Search in sources :

Example 96 with Resource

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

the class BodyInsertersTests method ofResource.

@Test
public void ofResource() throws Exception {
    Resource body = new ClassPathResource("response.txt", getClass());
    BodyInserter<Resource, ReactiveHttpOutputMessage> inserter = BodyInserters.fromResource(body);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    byte[] expectedBytes = Files.readAllBytes(body.getFile().toPath());
    StepVerifier.create(response.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        assertArrayEquals(expectedBytes, resultBytes);
    }).expectComplete().verify();
}
Also used : ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 97 with Resource

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

the class PathResourceLookupFunctionTests method normal.

@Test
public void normal() throws Exception {
    ClassPathResource location = new ClassPathResource("org/springframework/web/reactive/function/server/");
    PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location);
    MockServerRequest request = MockServerRequest.builder().uri(new URI("http://localhost/resources/response.txt")).build();
    Mono<Resource> result = function.apply(request);
    File expected = new ClassPathResource("response.txt", getClass()).getFile();
    StepVerifier.create(result).expectNextMatches(resource -> {
        try {
            return expected.equals(resource.getFile());
        } catch (IOException ex) {
            return false;
        }
    }).expectComplete().verify();
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) URI(java.net.URI) File(java.io.File) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 98 with Resource

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

the class PathResourceLookupFunctionTests method notFound.

@Test
public void notFound() throws Exception {
    ClassPathResource location = new ClassPathResource("org/springframework/web/reactive/function/server/");
    PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location);
    MockServerRequest request = MockServerRequest.builder().uri(new URI("http://localhost/resources/foo")).build();
    Mono<Resource> result = function.apply(request);
    StepVerifier.create(result).expectComplete().verify();
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) URI(java.net.URI) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 99 with Resource

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

the class CssLinkResourceTransformerTests method transformWithNonCssResource.

@Test
public void transformWithNonCssResource() throws Exception {
    MockServerWebExchange exchange = MockServerHttpRequest.get("/static/images/image.png").toExchange();
    Resource expected = new ClassPathResource("test/images/image.png", getClass());
    StepVerifier.create(this.transformerChain.transform(exchange, expected)).expectNext(expected).expectComplete().verify();
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 100 with Resource

use of org.springframework.core.io.Resource 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();
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

Resource (org.springframework.core.io.Resource)541 Test (org.junit.Test)248 ClassPathResource (org.springframework.core.io.ClassPathResource)217 IOException (java.io.IOException)86 FileSystemResource (org.springframework.core.io.FileSystemResource)67 File (java.io.File)60 UrlResource (org.springframework.core.io.UrlResource)60 ArrayList (java.util.ArrayList)49 ByteArrayResource (org.springframework.core.io.ByteArrayResource)46 InputStream (java.io.InputStream)33 InputStreamResource (org.springframework.core.io.InputStreamResource)31 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)24 URL (java.net.URL)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)18 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)16 ServletContextResource (org.springframework.web.context.support.ServletContextResource)16 URI (java.net.URI)15 ResourceLoader (org.springframework.core.io.ResourceLoader)15 HashMap (java.util.HashMap)13 LinkedHashSet (java.util.LinkedHashSet)13