Search in sources :

Example 61 with MockServerWebExchange

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

the class ResponseEntityResultHandlerTests method handleReturnValueETagAndLastModified.

@Test
public void handleReturnValueETagAndLastModified() throws Exception {
    String eTag = "\"deadb33f8badf00d\"";
    Instant currentTime = Instant.now().truncatedTo(ChronoUnit.SECONDS);
    Instant oneMinAgo = currentTime.minusSeconds(60);
    MockServerWebExchange exchange = get("/path").ifNoneMatch(eTag).ifModifiedSince(currentTime.toEpochMilli()).toExchange();
    ResponseEntity<String> entity = ok().eTag(eTag).lastModified(oneMinAgo.toEpochMilli()).body("body");
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class));
    HandlerResult result = handlerResult(entity, returnType);
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertConditionalResponse(exchange, HttpStatus.NOT_MODIFIED, null, eTag, oneMinAgo);
}
Also used : Instant(java.time.Instant) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 62 with MockServerWebExchange

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

the class ResponseEntityResultHandlerTests method handleReturnValueLastModified.

@Test
public void handleReturnValueLastModified() throws Exception {
    Instant currentTime = Instant.now().truncatedTo(ChronoUnit.SECONDS);
    Instant oneMinAgo = currentTime.minusSeconds(60);
    MockServerWebExchange exchange = get("/path").ifModifiedSince(currentTime.toEpochMilli()).toExchange();
    ResponseEntity<String> entity = ok().lastModified(oneMinAgo.toEpochMilli()).body("body");
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class));
    HandlerResult result = handlerResult(entity, returnType);
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertConditionalResponse(exchange, HttpStatus.NOT_MODIFIED, null, null, oneMinAgo);
}
Also used : Instant(java.time.Instant) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 63 with MockServerWebExchange

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

the class ResponseEntityResultHandlerTests method headers.

@Test
public void headers() throws Exception {
    URI location = new URI("/path");
    ResponseEntity<Void> value = ResponseEntity.created(location).build();
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(Void.class));
    HandlerResult result = handlerResult(value, returnType);
    MockServerWebExchange exchange = get("/path").toExchange();
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertEquals(HttpStatus.CREATED, exchange.getResponse().getStatusCode());
    assertEquals(1, exchange.getResponse().getHeaders().size());
    assertEquals(location, exchange.getResponse().getHeaders().getLocation());
    assertResponseBodyIsEmpty(exchange);
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) URI(java.net.URI) Test(org.junit.Test)

Example 64 with MockServerWebExchange

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

the class ResponseEntityResultHandlerTests method handleMonoWithWildcardBodyTypeAndNullBody.

// SPR-14877
@Test
public void handleMonoWithWildcardBodyTypeAndNullBody() throws Exception {
    MockServerWebExchange exchange = get("/path").toExchange();
    exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(APPLICATION_JSON));
    MethodParameter returnType = on(TestController.class).resolveReturnType(Mono.class, ResponseEntity.class);
    HandlerResult result = new HandlerResult(new TestController(), Mono.just(notFound().build()), returnType);
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertEquals(HttpStatus.NOT_FOUND, exchange.getResponse().getStatusCode());
    assertResponseBodyIsEmpty(exchange);
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 65 with MockServerWebExchange

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

the class ResponseEntityResultHandlerTests method handleReturnValueEtagInvalidIfNoneMatch.

// SPR-14559
@Test
public void handleReturnValueEtagInvalidIfNoneMatch() throws Exception {
    MockServerWebExchange exchange = get("/path").ifNoneMatch("unquoted").toExchange();
    ResponseEntity<String> entity = ok().eTag("\"deadb33f8badf00d\"").body("body");
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class));
    HandlerResult result = handlerResult(entity, returnType);
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertEquals(HttpStatus.OK, exchange.getResponse().getStatusCode());
    assertResponseBody(exchange, "body");
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult) MethodParameter(org.springframework.core.MethodParameter) 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