Search in sources :

Example 76 with MockServerWebExchange

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

the class ResourceWebHandlerTests method partialContentByteRangeLargeEnd.

@Test
public void partialContentByteRangeLargeEnd() throws Exception {
    MockServerWebExchange exchange = MockServerHttpRequest.get("").header("range", "bytes=9-10000").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(1, exchange.getResponse().getHeaders().getContentLength());
    assertEquals("bytes 9-9/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, ".");
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 77 with MockServerWebExchange

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

the class ResourceWebHandlerTests method getResourceHttpHeader.

@Test
public void getResourceHttpHeader() throws Exception {
    MockServerWebExchange exchange = MockServerHttpRequest.head("").toExchange();
    setPathWithinHandlerMapping(exchange, "foo.css");
    this.handler.handle(exchange).block(TIMEOUT);
    assertNull(exchange.getResponse().getStatusCode());
    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());
    StepVerifier.create(exchange.getResponse().getBody()).expectErrorMatches(ex -> ex.getMessage().startsWith("The body is not set.")).verify();
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) CacheControl(org.springframework.http.CacheControl) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Assert.assertSame(org.junit.Assert.assertSame) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) DataBufferTestUtils(org.springframework.core.io.buffer.support.DataBufferTestUtils) Duration(java.time.Duration) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) Assert.fail(org.junit.Assert.fail) HandlerMapping(org.springframework.web.reactive.HandlerMapping) Before(org.junit.Before) Resource(org.springframework.core.io.Resource) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) StandardCharsets(java.nio.charset.StandardCharsets) MethodNotAllowedException(org.springframework.web.server.MethodNotAllowedException) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Assert.assertNull(org.junit.Assert.assertNull) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) CompositeContentTypeResolver(org.springframework.web.reactive.accept.CompositeContentTypeResolver) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) StringUtils(org.springframework.util.StringUtils) HttpHeaders(org.springframework.http.HttpHeaders) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 78 with MockServerWebExchange

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

the class HandlerResultHandlerTests method usesContentTypeResolver.

@Test
public void usesContentTypeResolver() throws Exception {
    TestResultHandler resultHandler = new TestResultHandler(new FixedContentTypeResolver(IMAGE_GIF));
    List<MediaType> mediaTypes = Arrays.asList(IMAGE_JPEG, IMAGE_GIF, IMAGE_PNG);
    MockServerWebExchange exchange = MockServerHttpRequest.get("/path").toExchange();
    MediaType actual = resultHandler.selectMediaType(exchange, () -> mediaTypes);
    assertEquals(IMAGE_GIF, actual);
}
Also used : FixedContentTypeResolver(org.springframework.web.reactive.accept.FixedContentTypeResolver) MediaType(org.springframework.http.MediaType) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 79 with MockServerWebExchange

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

the class HandlerResultHandlerTests method sortsByQuality.

// SPR-9160
@Test
public void sortsByQuality() throws Exception {
    MockServerWebExchange exchange = MockServerHttpRequest.get("/path").header("Accept", "text/plain; q=0.5, application/json").toExchange();
    List<MediaType> mediaTypes = Arrays.asList(TEXT_PLAIN, APPLICATION_JSON_UTF8);
    MediaType actual = this.resultHandler.selectMediaType(exchange, () -> mediaTypes);
    assertEquals(APPLICATION_JSON_UTF8, actual);
}
Also used : MediaType(org.springframework.http.MediaType) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 80 with MockServerWebExchange

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

the class HandlerResultHandlerTests method charsetFromAcceptHeader.

@Test
public void charsetFromAcceptHeader() throws Exception {
    MediaType text8859 = MediaType.parseMediaType("text/plain;charset=ISO-8859-1");
    MediaType textUtf8 = MediaType.parseMediaType("text/plain;charset=UTF-8");
    MockServerWebExchange exchange = MockServerHttpRequest.get("/path").accept(text8859).toExchange();
    MediaType actual = this.resultHandler.selectMediaType(exchange, () -> Collections.singletonList(textUtf8));
    assertEquals(text8859, actual);
}
Also used : MediaType(org.springframework.http.MediaType) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) 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