Search in sources :

Example 26 with HandlerResult

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

the class ViewResolutionResultHandlerTests method unresolvedViewName.

@Test
public void unresolvedViewName() throws Exception {
    String returnValue = "account";
    MethodParameter returnType = on(TestController.class).resolveReturnType(String.class);
    HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext);
    MockServerWebExchange exchange = get("/path").toExchange();
    Mono<Void> mono = resultHandler().handleResult(exchange, result);
    StepVerifier.create(mono).expectNextCount(0).expectErrorMessage("Could not resolve view with name 'account'.").verify();
}
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 27 with HandlerResult

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

the class ViewResolutionResultHandlerTests method testDefaultViewName.

private void testDefaultViewName(Object returnValue, MethodParameter returnType) throws URISyntaxException {
    this.bindingContext.getModel().addAttribute("id", "123");
    HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext);
    ViewResolutionResultHandler handler = resultHandler(new TestViewResolver("account"));
    MockServerWebExchange exchange = get("/account").toExchange();
    handler.handleResult(exchange, result).block(Duration.ofMillis(5000));
    assertResponseBody(exchange, "account: {id=123}");
    exchange = get("/account/").toExchange();
    handler.handleResult(exchange, result).block(Duration.ofMillis(5000));
    assertResponseBody(exchange, "account: {id=123}");
    exchange = get("/account.123").toExchange();
    handler.handleResult(exchange, result).block(Duration.ofMillis(5000));
    assertResponseBody(exchange, "account: {id=123}");
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange)

Example 28 with HandlerResult

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

the class ResponseBodyResultHandlerTests method testSupports.

private void testSupports(Object controller, Method method) {
    HandlerResult handlerResult = getHandlerResult(controller, method);
    assertTrue(this.resultHandler.supports(handlerResult));
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult)

Example 29 with HandlerResult

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

the class ResponseEntityResultHandlerTests method handleMonoWithWildcardBodyType.

// SPR-14877
@Test
public void handleMonoWithWildcardBodyType() throws Exception {
    MockServerWebExchange exchange = get("/path").toExchange();
    exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(APPLICATION_JSON));
    MethodParameter type = on(TestController.class).resolveReturnType(Mono.class, ResponseEntity.class);
    HandlerResult result = new HandlerResult(new TestController(), Mono.just(ok().body("body")), type);
    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)

Example 30 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)

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