Search in sources :

Example 91 with MockServerWebExchange

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

the class ResourceWebHandlerTests method directoryInJarFile.

@Test
public void directoryInJarFile() throws Exception {
    MockServerWebExchange exchange = MockServerHttpRequest.get("").toExchange();
    setPathWithinHandlerMapping(exchange, "underscorejs/");
    this.handler.handle(exchange).block(TIMEOUT);
    assertNull(exchange.getResponse().getStatusCode());
    assertEquals(0, exchange.getResponse().getHeaders().getContentLength());
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 92 with MockServerWebExchange

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

the class ResourceWebHandlerTests method getResource.

@Test
public void getResource() throws Exception {
    MockServerWebExchange exchange = MockServerHttpRequest.get("").toExchange();
    setPathWithinHandlerMapping(exchange, "foo.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("test/foo.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 93 with MockServerWebExchange

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

the class ResourceWebHandlerTests method getVersionedResource.

@Test
public void getVersionedResource() throws Exception {
    VersionResourceResolver versionResolver = new VersionResourceResolver();
    versionResolver.addFixedVersionStrategy("versionString", "/**");
    this.handler.setResourceResolvers(Arrays.asList(versionResolver, new PathResourceResolver()));
    this.handler.afterPropertiesSet();
    this.handler.afterSingletonsInstantiated();
    MockServerWebExchange exchange = MockServerHttpRequest.get("").toExchange();
    setPathWithinHandlerMapping(exchange, "versionString/foo.css");
    this.handler.handle(exchange).block(TIMEOUT);
    assertEquals("\"versionString\"", exchange.getResponse().getHeaders().getETag());
    assertEquals("bytes", exchange.getResponse().getHeaders().getFirst("Accept-Ranges"));
    assertEquals(1, exchange.getResponse().getHeaders().get("Accept-Ranges").size());
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 94 with MockServerWebExchange

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

the class ResourceWebHandlerTests method partialContentByteRange.

@Test
public void partialContentByteRange() throws Exception {
    MockServerWebExchange exchange = MockServerHttpRequest.get("").header("Range", "bytes=0-1").toExchange();
    setPathWithinHandlerMapping(exchange, "foo.txt");
    this.handler.handle(exchange).block(TIMEOUT);
    assertEquals(HttpStatus.PARTIAL_CONTENT, exchange.getResponse().getStatusCode());
    assertEquals(MediaType.TEXT_PLAIN, exchange.getResponse().getHeaders().getContentType());
    assertEquals(2, exchange.getResponse().getHeaders().getContentLength());
    assertEquals("bytes 0-1/10", exchange.getResponse().getHeaders().getFirst("Content-Range"));
    assertEquals("bytes", exchange.getResponse().getHeaders().getFirst("Accept-Ranges"));
    assertEquals(1, exchange.getResponse().getHeaders().get("Accept-Ranges").size());
    assertResponseBody(exchange, "So");
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 95 with MockServerWebExchange

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

the class GzipResourceResolverTests method resolveFingerprintedGzippedFile.

@Test
public void resolveFingerprintedGzippedFile() throws IOException {
    MockServerWebExchange exchange = MockServerHttpRequest.get("").header("Accept-Encoding", "gzip").toExchange();
    String file = "foo-e36d2e05253c6c7085a91522ce43a0b4.css";
    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)

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