Search in sources :

Example 21 with MockServerWebExchange

use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.

the class GzipResourceResolverTests method resolveGzippedFile.

@Test
public void resolveGzippedFile() 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 resource = new ClassPathResource("test/" + gzFile, getClass());
    assertEquals(resource.getDescription(), resolved.getDescription());
    assertEquals(new ClassPathResource("test/" + file).getFilename(), resolved.getFilename());
    assertTrue("Expected " + resolved + " to be of type " + HttpResource.class, resolved instanceof HttpResource);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 22 with MockServerWebExchange

use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.

the class CachingResourceResolverTests method resolveResourceNoAcceptEncodingInCacheKey.

@Test
public void resolveResourceNoAcceptEncodingInCacheKey() {
    String file = "bar.css";
    MockServerWebExchange exchange = MockServerHttpRequest.get(file).toExchange();
    Resource expected = this.chain.resolveResource(exchange, file, this.locations).block(TIMEOUT);
    String cacheKey = CachingResourceResolver.RESOLVED_RESOURCE_CACHE_KEY_PREFIX + file;
    assertEquals(expected, this.cache.get(cacheKey).get());
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 23 with MockServerWebExchange

use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange 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);
    String file = "bar.css";
    MockServerWebExchange exchange = MockServerHttpRequest.get(file).toExchange();
    assertSame(resource, this.chain.resolveResource(exchange, file, this.locations).block(TIMEOUT));
    exchange = MockServerHttpRequest.get(file).header("Accept-Encoding", "gzip").toExchange();
    assertSame(gzResource, this.chain.resolveResource(exchange, file, this.locations).block(TIMEOUT));
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 24 with MockServerWebExchange

use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.

the class CachingResourceResolverTests method resolveResourceInternalFromCache.

@Test
public void resolveResourceInternalFromCache() {
    Resource expected = Mockito.mock(Resource.class);
    this.cache.put(CachingResourceResolver.RESOLVED_RESOURCE_CACHE_KEY_PREFIX + "bar.css", expected);
    String file = "bar.css";
    MockServerWebExchange exchange = MockServerHttpRequest.get("").toExchange();
    Resource actual = this.chain.resolveResource(exchange, file, this.locations).block(TIMEOUT);
    assertSame(expected, actual);
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 25 with MockServerWebExchange

use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.

the class CssLinkResourceTransformerTests method transform.

@Test
public void transform() throws Exception {
    MockServerWebExchange exchange = MockServerHttpRequest.get("/static/main.css").toExchange();
    Resource css = new ClassPathResource("test/main.css", getClass());
    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";
    StepVerifier.create(this.transformerChain.transform(exchange, css).cast(TransformedResource.class)).consumeNextWith(resource -> {
        String result = new String(resource.getByteArray(), StandardCharsets.UTF_8);
        result = StringUtils.deleteAny(result, "\r");
        assertEquals(expected, result);
    }).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

MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)135 Test (org.junit.Test)127 ClassPathResource (org.springframework.core.io.ClassPathResource)19 Resource (org.springframework.core.io.Resource)19 HandlerResult (org.springframework.web.reactive.HandlerResult)17 MethodParameter (org.springframework.core.MethodParameter)15 MediaType (org.springframework.http.MediaType)8 Instant (java.time.Instant)7 HttpHeaders (org.springframework.http.HttpHeaders)5 MockServerHttpResponse (org.springframework.mock.http.server.reactive.test.MockServerHttpResponse)5 FileSystemResource (org.springframework.core.io.FileSystemResource)3 UrlResource (org.springframework.core.io.UrlResource)3 DataBuffer (org.springframework.core.io.buffer.DataBuffer)3 CompositeContentTypeResolver (org.springframework.web.reactive.accept.CompositeContentTypeResolver)3 RequestedContentTypeResolverBuilder (org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)3 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Assert.assertSame (org.junit.Assert.assertSame)2 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)2