Search in sources :

Example 46 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() {
    Object returnValue = Mono.just(notFound().build());
    MethodParameter type = on(TestController.class).resolveReturnType(Mono.class, entity(String.class));
    HandlerResult result = handlerResult(returnValue, type);
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
    assertResponseBodyIsEmpty(exchange);
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Example 47 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 = MockServerWebExchange.from(get("/path"));
    HandlerResult result = handlerResult(returnValue, returnType);
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(exchange.getResponse().getHeaders().getFirst("Content-Type")).isEqualTo("text/plain;charset=UTF-8");
    assertResponseBody(exchange, "abc");
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult)

Example 48 with HandlerResult

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

the class ViewResolutionResultHandlerTests method contentNegotiationWithRedirect.

// SPR-15291
@Test
public void contentNegotiationWithRedirect() {
    HandlerResult handlerResult = new HandlerResult(new Object(), "redirect:/", on(Handler.class).annotNotPresent(ModelAttribute.class).resolveReturnType(String.class), this.bindingContext);
    UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
    viewResolver.setApplicationContext(new StaticApplicationContext());
    ViewResolutionResultHandler resultHandler = resultHandler(viewResolver);
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/account").accept(APPLICATION_JSON));
    resultHandler.handleResult(exchange, handlerResult).block(Duration.ZERO);
    MockServerHttpResponse response = exchange.getResponse();
    assertThat(response.getStatusCode().value()).isEqualTo(303);
    assertThat(response.getHeaders().getLocation().toString()).isEqualTo("/");
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 49 with HandlerResult

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

the class ViewResolutionResultHandlerTests method contentNegotiation.

@Test
public void contentNegotiation() {
    TestBean value = new TestBean("Joe");
    MethodParameter returnType = on(Handler.class).resolveReturnType(TestBean.class);
    HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType, this.bindingContext);
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/account").accept(APPLICATION_JSON));
    TestView defaultView = new TestView("jsonView", APPLICATION_JSON);
    resultHandler(Collections.singletonList(defaultView), new TestViewResolver("account")).handleResult(exchange, handlerResult).block(Duration.ofSeconds(5));
    assertThat(exchange.getResponse().getHeaders().getContentType()).isEqualTo(APPLICATION_JSON);
    assertResponseBody(exchange, "jsonView: {" + "org.springframework.validation.BindingResult.testBean=" + "org.springframework.validation.BeanPropertyBindingResult: 0 errors, " + "testBean=TestBean[name=Joe]" + "}");
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Example 50 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() {
    String returnValue = "account";
    MethodParameter returnType = on(Handler.class).annotPresent(ModelAttribute.class).resolveReturnType(String.class);
    HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext);
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
    Mono<Void> mono = resultHandler().handleResult(exchange, result);
    StepVerifier.create(mono).expectNextCount(0).expectErrorMessage("Could not resolve view with name 'path'.").verify();
}
Also used : ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Aggregations

HandlerResult (org.springframework.web.reactive.HandlerResult)60 Test (org.junit.jupiter.api.Test)36 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)36 Method (java.lang.reflect.Method)24 MethodParameter (org.springframework.core.MethodParameter)23 BindingContext (org.springframework.web.reactive.BindingContext)20 Mono (reactor.core.publisher.Mono)20 HttpStatus (org.springframework.http.HttpStatus)19 StepVerifier (reactor.test.StepVerifier)19 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)18 Instant (java.time.Instant)17 Mockito.mock (org.mockito.Mockito.mock)17 UnsupportedMediaTypeStatusException (org.springframework.web.server.UnsupportedMediaTypeStatusException)17 List (java.util.List)15 ServerWebExchange (org.springframework.web.server.ServerWebExchange)15 ResolvableMethod (org.springframework.web.testfixture.method.ResolvableMethod)15 Duration (java.time.Duration)14 ArrayList (java.util.ArrayList)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)14 Nullable (org.springframework.lang.Nullable)14