Search in sources :

Example 16 with MockServerWebExchange

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

the class ResourceHandlerFunctionTests method head.

@Test
public void head() throws IOException {
    MockServerWebExchange exchange = MockServerHttpRequest.head("http://localhost").toExchange();
    MockServerHttpResponse mockResponse = exchange.getResponse();
    ServerRequest request = new DefaultServerRequest(exchange, HandlerStrategies.withDefaults());
    Mono<ServerResponse> response = this.handlerFunction.handle(request);
    Mono<Void> result = response.then(res -> {
        assertEquals(HttpStatus.OK, res.statusCode());
        return res.writeTo(exchange, HandlerStrategies.withDefaults());
    });
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(mockResponse.getBody()).expectComplete().verify();
    assertEquals(MediaType.TEXT_PLAIN, mockResponse.getHeaders().getContentType());
    assertEquals(this.resource.contentLength(), mockResponse.getHeaders().getContentLength());
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Example 17 with MockServerWebExchange

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

the class ResourceHandlerFunctionTests method options.

@Test
public void options() {
    MockServerWebExchange exchange = MockServerHttpRequest.options("http://localhost").toExchange();
    MockServerHttpResponse mockResponse = exchange.getResponse();
    ServerRequest request = new DefaultServerRequest(exchange, HandlerStrategies.withDefaults());
    Mono<ServerResponse> responseMono = this.handlerFunction.handle(request);
    Mono<Void> result = responseMono.then(response -> {
        assertEquals(HttpStatus.OK, response.statusCode());
        assertEquals(EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS), response.headers().getAllow());
        return response.writeTo(exchange, HandlerStrategies.withDefaults());
    });
    StepVerifier.create(result).expectComplete().verify();
    assertEquals(HttpStatus.OK, mockResponse.getStatusCode());
    assertEquals(EnumSet.of(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS), mockResponse.getHeaders().getAllow());
    StepVerifier.create(mockResponse.getBody()).expectComplete().verify();
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Example 18 with MockServerWebExchange

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

the class CssLinkResourceTransformerTests method transformWithNonCssResource.

@Test
public void transformWithNonCssResource() throws Exception {
    MockServerWebExchange exchange = MockServerHttpRequest.get("/static/images/image.png").toExchange();
    Resource expected = new ClassPathResource("test/images/image.png", getClass());
    StepVerifier.create(this.transformerChain.transform(exchange, expected)).expectNext(expected).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)

Example 19 with MockServerWebExchange

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

the class CssLinkResourceTransformerTests method transformWithGzippedResource.

@Test
public void transformWithGzippedResource() throws Exception {
    MockServerWebExchange exchange = MockServerHttpRequest.get("/static/main.css").toExchange();
    Resource original = new ClassPathResource("test/main.css", getClass());
    createTempCopy("main.css", "main.css.gz");
    GzipResourceResolver.GzippedResource expected = new GzipResourceResolver.GzippedResource(original);
    StepVerifier.create(this.transformerChain.transform(exchange, expected)).expectNext(expected).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)

Example 20 with MockServerWebExchange

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

the class GzipResourceResolverTests method resolveFromCacheWithEncodingVariants.

@Test
public void resolveFromCacheWithEncodingVariants() 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 gzResource = new ClassPathResource("test/" + gzFile, getClass());
    assertEquals(gzResource.getDescription(), resolved.getDescription());
    assertEquals(new ClassPathResource("test/" + file).getFilename(), resolved.getFilename());
    assertTrue("Expected " + resolved + " to be of type " + HttpResource.class, resolved instanceof HttpResource);
    // resolved resource is now cached in CachingResourceResolver
    exchange = MockServerHttpRequest.get("/js/foo.js").toExchange();
    resolved = this.resolver.resolveResource(exchange, file, this.locations).block(TIMEOUT);
    Resource resource = new ClassPathResource("test/" + file, getClass());
    assertEquals(resource.getDescription(), resolved.getDescription());
    assertEquals(new ClassPathResource("test/" + file).getFilename(), resolved.getFilename());
    assertFalse("Expected " + resolved + " to *not* 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