use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.
the class CachingResourceResolverTests method resolveResourceInternal.
@Test
public void resolveResourceInternal() {
String file = "bar.css";
Resource expected = new ClassPathResource("test/" + file, getClass());
MockServerWebExchange exchange = MockServerHttpRequest.get("").toExchange();
Resource actual = this.chain.resolveResource(exchange, file, this.locations).block(TIMEOUT);
assertEquals(expected, actual);
}
use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.
the class CssLinkResourceTransformerTests method transformExtLinksNotAllowed.
@Test
public void transformExtLinksNotAllowed() throws Exception {
MockServerWebExchange exchange = MockServerHttpRequest.get("/static/external.css").toExchange();
ResourceResolverChain resolverChain = Mockito.mock(DefaultResourceResolverChain.class);
ResourceTransformerChain transformerChain = new DefaultResourceTransformerChain(resolverChain, Collections.singletonList(new CssLinkResourceTransformer()));
Resource externalCss = new ClassPathResource("test/external.css", getClass());
StepVerifier.create(transformerChain.transform(exchange, externalCss).cast(TransformedResource.class)).consumeNextWith(resource -> {
String expected = "@import url(\"http://example.org/fonts/css\");\n" + "body { background: url(\"file:///home/spring/image.png\") }\n" + "figure { background: url(\"//example.org/style.css\")}";
String result = new String(resource.getByteArray(), StandardCharsets.UTF_8);
result = StringUtils.deleteAny(result, "\r");
assertEquals(expected, result);
}).expectComplete().verify();
Mockito.verify(resolverChain, Mockito.never()).resolveUrlPath("http://example.org/fonts/css", Collections.singletonList(externalCss));
Mockito.verify(resolverChain, Mockito.never()).resolveUrlPath("file:///home/spring/image.png", Collections.singletonList(externalCss));
Mockito.verify(resolverChain, Mockito.never()).resolveUrlPath("//example.org/style.css", Collections.singletonList(externalCss));
}
use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.
the class ResourceWebHandlerTests method getResourceFromAlternatePath.
@Test
public void getResourceFromAlternatePath() throws Exception {
MockServerWebExchange exchange = MockServerHttpRequest.get("").toExchange();
setPathWithinHandlerMapping(exchange, "baz.css");
this.handler.handle(exchange).block(TIMEOUT);
HttpHeaders headers = exchange.getResponse().getHeaders();
assertEquals(MediaType.parseMediaType("text/css"), headers.getContentType());
assertEquals(17, headers.getContentLength());
assertEquals("max-age=3600", headers.getCacheControl());
assertTrue(headers.containsKey("Last-Modified"));
assertEquals(headers.getLastModified() / 1000, resourceLastModifiedDate("testalternatepath/baz.css") / 1000);
assertEquals("bytes", headers.getFirst("Accept-Ranges"));
assertEquals(1, headers.get("Accept-Ranges").size());
assertResponseBody(exchange, "h1 { color:red; }");
}
use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.
the class ResourceWebHandlerTests method getResourceHttpOptions.
@Test
public void getResourceHttpOptions() throws Exception {
MockServerWebExchange exchange = MockServerHttpRequest.options("").toExchange();
setPathWithinHandlerMapping(exchange, "foo.css");
this.handler.handle(exchange).block(TIMEOUT);
assertNull(exchange.getResponse().getStatusCode());
assertEquals("GET,HEAD,OPTIONS", exchange.getResponse().getHeaders().getFirst("Allow"));
}
use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.
the class ResourceWebHandlerTests method partialContentMultipleByteRanges.
@Test
public void partialContentMultipleByteRanges() throws Exception {
MockServerWebExchange exchange = MockServerHttpRequest.get("").header("Range", "bytes=0-1, 4-5, 8-9").toExchange();
setPathWithinHandlerMapping(exchange, "foo.txt");
this.handler.handle(exchange).block(TIMEOUT);
assertEquals(HttpStatus.PARTIAL_CONTENT, exchange.getResponse().getStatusCode());
assertTrue(exchange.getResponse().getHeaders().getContentType().toString().startsWith("multipart/byteranges;boundary="));
String boundary = "--" + exchange.getResponse().getHeaders().getContentType().toString().substring(30);
Mono<DataBuffer> reduced = Flux.from(exchange.getResponse().getBody()).reduce(this.bufferFactory.allocateBuffer(), (previous, current) -> {
previous.write(current);
DataBufferUtils.release(current);
return previous;
});
StepVerifier.create(reduced).consumeNextWith(buf -> {
String content = DataBufferTestUtils.dumpString(buf, StandardCharsets.UTF_8);
String[] ranges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true);
assertEquals(boundary, ranges[0]);
assertEquals("Content-Type: text/plain", ranges[1]);
assertEquals("Content-Range: bytes 0-1/10", ranges[2]);
assertEquals("So", ranges[3]);
assertEquals(boundary, ranges[4]);
assertEquals("Content-Type: text/plain", ranges[5]);
assertEquals("Content-Range: bytes 4-5/10", ranges[6]);
assertEquals(" t", ranges[7]);
assertEquals(boundary, ranges[8]);
assertEquals("Content-Type: text/plain", ranges[9]);
assertEquals("Content-Range: bytes 8-9/10", ranges[10]);
assertEquals("t.", ranges[11]);
}).expectComplete().verify();
}
Aggregations