Search in sources :

Example 1 with HandlerFunction

use of org.springframework.web.reactive.function.server.HandlerFunction in project spring-framework by spring-projects.

the class RouterFunctionMappingTests method mappedRequestShouldHoldAttributes.

@Test
void mappedRequestShouldHoldAttributes() {
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route().GET("/match", handlerFunction).build();
    RouterFunctionMapping mapping = new RouterFunctionMapping(routerFunction);
    mapping.setMessageReaders(this.codecConfigurer.getReaders());
    ServerWebExchange exchange = createExchange("https://example.com/match");
    Mono<Object> result = mapping.getHandler(exchange);
    StepVerifier.create(result).expectNext(handlerFunction).expectComplete().verify();
    PathPattern matchingPattern = exchange.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
    assertThat(matchingPattern).isNotNull();
    assertThat(matchingPattern.getPatternString()).isEqualTo("/match");
    ServerRequest serverRequest = exchange.getAttribute(RouterFunctions.REQUEST_ATTRIBUTE);
    assertThat(serverRequest).isNotNull();
    HandlerFunction<?> handler = exchange.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE);
    assertThat(handler).isEqualTo(handlerFunction);
}
Also used : ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) StepVerifier(reactor.test.StepVerifier) RouterFunctions(org.springframework.web.reactive.function.server.RouterFunctions) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) ServerRequest(org.springframework.web.reactive.function.server.ServerRequest) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) RouterFunction(org.springframework.web.reactive.function.server.RouterFunction) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) PathPattern(org.springframework.web.util.pattern.PathPattern) HandlerFunction(org.springframework.web.reactive.function.server.HandlerFunction) HandlerMapping(org.springframework.web.reactive.HandlerMapping) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) PathPattern(org.springframework.web.util.pattern.PathPattern) ServerRequest(org.springframework.web.reactive.function.server.ServerRequest) Test(org.junit.jupiter.api.Test)

Example 2 with HandlerFunction

use of org.springframework.web.reactive.function.server.HandlerFunction in project spring-framework by spring-projects.

the class RouterFunctionMappingTests method changeParser.

@Test
void changeParser() throws Exception {
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route().GET("/foo", handlerFunction).POST("/bar", handlerFunction).build();
    RouterFunctionMapping mapping = new RouterFunctionMapping(routerFunction);
    mapping.setMessageReaders(this.codecConfigurer.getReaders());
    mapping.setUseCaseSensitiveMatch(false);
    mapping.afterPropertiesSet();
    ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("https://example.com/FOO"));
    Mono<Object> result = mapping.getHandler(exchange);
    StepVerifier.create(result).expectNext(handlerFunction).verifyComplete();
}
Also used : ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) StepVerifier(reactor.test.StepVerifier) RouterFunctions(org.springframework.web.reactive.function.server.RouterFunctions) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) ServerRequest(org.springframework.web.reactive.function.server.ServerRequest) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) RouterFunction(org.springframework.web.reactive.function.server.RouterFunction) ServerResponse(org.springframework.web.reactive.function.server.ServerResponse) PathPattern(org.springframework.web.util.pattern.PathPattern) HandlerFunction(org.springframework.web.reactive.function.server.HandlerFunction) HandlerMapping(org.springframework.web.reactive.HandlerMapping) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test)

Example 3 with HandlerFunction

use of org.springframework.web.reactive.function.server.HandlerFunction 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)

Aggregations

HandlerFunction (org.springframework.web.reactive.function.server.HandlerFunction)3 ServerRequest (org.springframework.web.reactive.function.server.ServerRequest)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Test (org.junit.jupiter.api.Test)2 ServerCodecConfigurer (org.springframework.http.codec.ServerCodecConfigurer)2 HandlerMapping (org.springframework.web.reactive.HandlerMapping)2 RouterFunction (org.springframework.web.reactive.function.server.RouterFunction)2 RouterFunctions (org.springframework.web.reactive.function.server.RouterFunctions)2 ServerResponse (org.springframework.web.reactive.function.server.ServerResponse)2 ServerWebExchange (org.springframework.web.server.ServerWebExchange)2 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)2 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)2 PathPattern (org.springframework.web.util.pattern.PathPattern)2 Mono (reactor.core.publisher.Mono)2 StepVerifier (reactor.test.StepVerifier)2 HandlerResult (org.springframework.web.reactive.HandlerResult)1