Search in sources :

Example 51 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) {
    this.bindingContext.getModel().addAttribute("id", "123");
    HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext);
    ViewResolutionResultHandler handler = resultHandler(new TestViewResolver("account"));
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/account"));
    handler.handleResult(exchange, result).block(Duration.ofMillis(5000));
    assertResponseBody(exchange, "account: {id=123}");
    exchange = MockServerWebExchange.from(get("/account/"));
    handler.handleResult(exchange, result).block(Duration.ofMillis(5000));
    assertResponseBody(exchange, "account: {id=123}");
    exchange = MockServerWebExchange.from(get("/account.123"));
    handler.handleResult(exchange, result).block(Duration.ofMillis(5000));
    assertResponseBody(exchange, "account: {id=123}");
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange)

Example 52 with HandlerResult

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

the class ViewResolutionResultHandlerTests method testHandle.

private ServerWebExchange testHandle(String path, MethodParameter returnType, Object returnValue, String responseBody, ViewResolver... resolvers) {
    Model model = this.bindingContext.getModel();
    model.asMap().clear();
    model.addAttribute("id", "123");
    HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext);
    MockServerWebExchange exchange = MockServerWebExchange.from(get(path));
    resultHandler(resolvers).handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertResponseBody(exchange, responseBody);
    return exchange;
}
Also used : ConcurrentModel(org.springframework.ui.ConcurrentModel) Model(org.springframework.ui.Model) HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange)

Example 53 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);
    assertThat(this.resultHandler.supports(handlerResult)).isTrue();
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult)

Example 54 with HandlerResult

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

the class HandlerFunctionAdapter method handle.

@Override
public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {
    HandlerFunction<?> handlerFunction = (HandlerFunction<?>) handler;
    ServerRequest request = exchange.getRequiredAttribute(RouterFunctions.REQUEST_ATTRIBUTE);
    return handlerFunction.handle(request).map(response -> new HandlerResult(handlerFunction, response, HANDLER_FUNCTION_RETURN_TYPE));
}
Also used : HandlerFunction(org.springframework.web.reactive.function.server.HandlerFunction) HandlerResult(org.springframework.web.reactive.HandlerResult) ServerRequest(org.springframework.web.reactive.function.server.ServerRequest)

Example 55 with HandlerResult

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

the class RequestMappingInfoHandlerMappingTests method testHttpOptions.

private void testHttpOptions(String requestURI, Set<HttpMethod> allowedMethods, @Nullable MediaType acceptPatch) {
    ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.options(requestURI));
    HandlerMethod handlerMethod = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();
    BindingContext bindingContext = new BindingContext();
    InvocableHandlerMethod invocable = new InvocableHandlerMethod(handlerMethod);
    Mono<HandlerResult> mono = invocable.invoke(exchange, bindingContext);
    HandlerResult result = mono.block();
    assertThat(result).isNotNull();
    Object value = result.getReturnValue();
    assertThat(value).isNotNull();
    assertThat(value.getClass()).isEqualTo(HttpHeaders.class);
    HttpHeaders headers = (HttpHeaders) value;
    assertThat(headers.getAllow()).hasSameElementsAs(allowedMethods);
    if (acceptPatch != null && headers.getAllow().contains(HttpMethod.PATCH)) {
        assertThat(headers.getAcceptPatch()).containsExactly(acceptPatch);
    }
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHeaders(org.springframework.http.HttpHeaders) HandlerResult(org.springframework.web.reactive.HandlerResult) BindingContext(org.springframework.web.reactive.BindingContext) HandlerMethod(org.springframework.web.method.HandlerMethod)

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