Search in sources :

Example 31 with HandlerResult

use of org.springframework.web.reactive.HandlerResult in project spring-framework by spring-projects.

the class ResponseEntityResultHandlerTests method testHandle.

private void testHandle(Object returnValue, MethodParameter returnType) {
    MockServerWebExchange exchange = get("/path").toExchange();
    HandlerResult result = handlerResult(returnValue, returnType);
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertEquals(HttpStatus.OK, exchange.getResponse().getStatusCode());
    assertEquals("text/plain;charset=UTF-8", exchange.getResponse().getHeaders().getFirst("Content-Type"));
    assertResponseBody(exchange, "abc");
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult)

Example 32 with HandlerResult

use of org.springframework.web.reactive.HandlerResult in project spring-framework by spring-projects.

the class ResponseEntityResultHandlerTests method handleReturnValueChangedETagAndLastModified.

@Test
public void handleReturnValueChangedETagAndLastModified() throws Exception {
    String etag = "\"deadb33f8badf00d\"";
    String newEtag = "\"changed-etag-value\"";
    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(newEtag).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.OK, "body", newEtag, 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 33 with HandlerResult

use of org.springframework.web.reactive.HandlerResult in project spring-framework by spring-projects.

the class ResponseEntityResultHandlerTests method handleResponseEntityWithNullBody.

@Test
public void handleResponseEntityWithNullBody() throws Exception {
    Object returnValue = Mono.just(notFound().build());
    MethodParameter type = on(TestController.class).resolveReturnType(Mono.class, entity(String.class));
    HandlerResult result = handlerResult(returnValue, type);
    MockServerWebExchange exchange = get("/path").toExchange();
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertEquals(HttpStatus.NOT_FOUND, exchange.getResponse().getStatusCode());
    assertResponseBodyIsEmpty(exchange);
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 34 with HandlerResult

use of org.springframework.web.reactive.HandlerResult in project spring-framework by spring-projects.

the class ResponseEntityResultHandlerTests method handleReturnValueEtag.

@Test
public void handleReturnValueEtag() throws Exception {
    String etagValue = "\"deadb33f8badf00d\"";
    MockServerWebExchange exchange = get("/path").ifNoneMatch(etagValue).toExchange();
    ResponseEntity<String> entity = ok().eTag(etagValue).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, etagValue, Instant.MIN);
}
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

HandlerResult (org.springframework.web.reactive.HandlerResult)34 Test (org.junit.Test)24 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)22 MethodParameter (org.springframework.core.MethodParameter)16 Method (java.lang.reflect.Method)10 BindingContext (org.springframework.web.reactive.BindingContext)7 Mono (reactor.core.publisher.Mono)7 Arrays (java.util.Arrays)6 Optional (java.util.Optional)6 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)6 Matchers.is (org.hamcrest.Matchers.is)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 Assert.assertThat (org.junit.Assert.assertThat)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 Assert.fail (org.junit.Assert.fail)5 Mockito.any (org.mockito.Mockito.any)5 Mockito.mock (org.mockito.Mockito.mock)5 Mockito.when (org.mockito.Mockito.when)5 Instant (java.time.Instant)4 HttpStatus (org.springframework.http.HttpStatus)4