Search in sources :

Example 96 with MockServerWebExchange

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);
}
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 97 with MockServerWebExchange

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));
}
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 98 with MockServerWebExchange

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; }");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 99 with MockServerWebExchange

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"));
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 100 with MockServerWebExchange

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();
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) DataBuffer(org.springframework.core.io.buffer.DataBuffer) 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