Search in sources :

Example 86 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 87 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 88 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)

Example 89 with MockServerWebExchange

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

the class ResourceWebHandlerTests method getMediaTypeWithFavorPathExtensionOff.

// SPR-14577
@Test
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
    CompositeContentTypeResolver contentTypeResolver = new RequestedContentTypeResolverBuilder().favorPathExtension(false).build();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceWebHandler handler = new ResourceWebHandler();
    handler.setLocations(paths);
    handler.setContentTypeResolver(contentTypeResolver);
    handler.afterPropertiesSet();
    handler.afterSingletonsInstantiated();
    MockServerWebExchange exchange = MockServerHttpRequest.get("").header("Accept", "application/json,text/plain,*/*").toExchange();
    setPathWithinHandlerMapping(exchange, "foo.html");
    handler.handle(exchange).block(TIMEOUT);
    assertEquals(MediaType.TEXT_HTML, exchange.getResponse().getHeaders().getContentType());
}
Also used : UrlResource(org.springframework.core.io.UrlResource) 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) CompositeContentTypeResolver(org.springframework.web.reactive.accept.CompositeContentTypeResolver) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) Test(org.junit.Test)

Example 90 with MockServerWebExchange

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

the class ResourceWebHandlerTests method getResourceWithRegisteredMediaType.

// SPR-13658
@Test
public void getResourceWithRegisteredMediaType() throws Exception {
    CompositeContentTypeResolver contentTypeResolver = new RequestedContentTypeResolverBuilder().mediaType("css", new MediaType("foo", "bar")).build();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceWebHandler handler = new ResourceWebHandler();
    handler.setLocations(paths);
    handler.setContentTypeResolver(contentTypeResolver);
    handler.afterPropertiesSet();
    handler.afterSingletonsInstantiated();
    MockServerWebExchange exchange = MockServerHttpRequest.get("").toExchange();
    setPathWithinHandlerMapping(exchange, "foo.css");
    handler.handle(exchange).block(TIMEOUT);
    assertEquals(MediaType.parseMediaType("foo/bar"), exchange.getResponse().getHeaders().getContentType());
    assertResponseBody(exchange, "h1 { color:red; }");
}
Also used : UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MediaType(org.springframework.http.MediaType) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ClassPathResource(org.springframework.core.io.ClassPathResource) CompositeContentTypeResolver(org.springframework.web.reactive.accept.CompositeContentTypeResolver) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) 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