Search in sources :

Example 86 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class RouterFunctionsTests method nestMatch.

@Test
public void nestMatch() {
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
    RouterFunction<ServerResponse> routerFunction = request -> Mono.just(handlerFunction);
    MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://example.com").build();
    ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
    RequestPredicate requestPredicate = mock(RequestPredicate.class);
    given(requestPredicate.nest(request)).willReturn(Optional.of(request));
    RouterFunction<ServerResponse> result = RouterFunctions.nest(requestPredicate, routerFunction);
    assertThat(result).isNotNull();
    Mono<HandlerFunction<ServerResponse>> resultHandlerFunction = result.route(request);
    StepVerifier.create(resultHandlerFunction).expectNext(handlerFunction).expectComplete().verify();
}
Also used : StepVerifier(reactor.test.StepVerifier) ResponseStatusException(org.springframework.web.server.ResponseStatusException) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MultiValueMap(org.springframework.util.MultiValueMap) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) HttpHandler(org.springframework.http.server.reactive.HttpHandler) WebFilter(org.springframework.web.server.WebFilter) BDDMockito.given(org.mockito.BDDMockito.given) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Optional(java.util.Optional) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ResponseCookie(org.springframework.http.ResponseCookie) Mockito.mock(org.mockito.Mockito.mock) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 87 with Mono

use of reactor.core.publisher.Mono 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 88 with Mono

use of reactor.core.publisher.Mono 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 89 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class DefaultServerResponseBuilderTests method buildVoidPublisher.

@Test
public void buildVoidPublisher() {
    Mono<Void> mono = Mono.empty();
    Mono<ServerResponse> result = ServerResponse.ok().build(mono);
    MockServerHttpRequest request = MockServerHttpRequest.get("https://example.com").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    result.flatMap(res -> res.writeTo(exchange, EMPTY_CONTEXT)).block();
    MockServerHttpResponse response = exchange.getResponse();
    StepVerifier.create(response.getBody()).expectComplete().verify();
}
Also used : StepVerifier(reactor.test.StepVerifier) ZonedDateTime(java.time.ZonedDateTime) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) CacheControl(org.springframework.http.CacheControl) URI(java.net.URI) ResponseCookie(org.springframework.http.ResponseCookie) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) DateTimeFormatter(java.time.format.DateTimeFormatter) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) ViewResolver(org.springframework.web.reactive.result.view.ViewResolver) Collections(java.util.Collections) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 90 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class ModelAttributeMethodArgumentResolver method resolveArgument.

@Override
public Mono<Object> resolveArgument(MethodParameter parameter, BindingContext context, ServerWebExchange exchange) {
    ResolvableType type = ResolvableType.forMethodParameter(parameter);
    Class<?> resolvedType = type.resolve();
    ReactiveAdapter adapter = (resolvedType != null ? getAdapterRegistry().getAdapter(resolvedType) : null);
    ResolvableType valueType = (adapter != null ? type.getGeneric() : type);
    Assert.state(adapter == null || !adapter.isMultiValue(), () -> getClass().getSimpleName() + " does not support multi-value reactive type wrapper: " + parameter.getGenericParameterType());
    String name = ModelInitializer.getNameForParameter(parameter);
    Mono<?> valueMono = prepareAttributeMono(name, valueType, context, exchange);
    // unsafe(): we're intercepting, already serialized Publisher signals
    Sinks.One<BindingResult> bindingResultSink = Sinks.unsafe().one();
    Map<String, Object> model = context.getModel().asMap();
    model.put(BindingResult.MODEL_KEY_PREFIX + name, bindingResultSink.asMono());
    return valueMono.flatMap(value -> {
        WebExchangeDataBinder binder = context.createDataBinder(exchange, value, name);
        return (bindingDisabled(parameter) ? Mono.empty() : bindRequestParameters(binder, exchange)).doOnError(bindingResultSink::tryEmitError).doOnSuccess(aVoid -> {
            validateIfApplicable(binder, parameter);
            BindingResult bindingResult = binder.getBindingResult();
            model.put(BindingResult.MODEL_KEY_PREFIX + name, bindingResult);
            model.put(name, value);
            // Ignore result: serialized and buffered (should never fail)
            bindingResultSink.tryEmitValue(bindingResult);
        }).then(Mono.fromCallable(() -> {
            BindingResult errors = binder.getBindingResult();
            if (adapter != null) {
                return adapter.fromPublisher(errors.hasErrors() ? Mono.error(new WebExchangeBindException(parameter, errors)) : valueMono);
            } else {
                if (errors.hasErrors() && !hasErrorsArgument(parameter)) {
                    throw new WebExchangeBindException(parameter, errors);
                }
                return value;
            }
        }));
    });
}
Also used : Sinks(reactor.core.publisher.Sinks) Errors(org.springframework.validation.Errors) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) BindingResult(org.springframework.validation.BindingResult) BindingContext(org.springframework.web.reactive.BindingContext) Constructor(java.lang.reflect.Constructor) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HandlerMethodArgumentResolverSupport(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport) Model(org.springframework.ui.Model) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) Nullable(org.springframework.lang.Nullable) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) ReactiveAdapter(org.springframework.core.ReactiveAdapter) ClassUtils(org.springframework.util.ClassUtils) ValidationAnnotationUtils(org.springframework.validation.annotation.ValidationAnnotationUtils) Mono(reactor.core.publisher.Mono) List(java.util.List) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) BeanUtils(org.springframework.beans.BeanUtils) Assert(org.springframework.util.Assert) BindingResult(org.springframework.validation.BindingResult) Sinks(reactor.core.publisher.Sinks) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Aggregations

Mono (reactor.core.publisher.Mono)308 Test (org.junit.jupiter.api.Test)143 StepVerifier (reactor.test.StepVerifier)117 List (java.util.List)116 Flux (reactor.core.publisher.Flux)110 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)102 Collections (java.util.Collections)85 Map (java.util.Map)75 HttpStatus (org.springframework.http.HttpStatus)66 ServerWebExchange (org.springframework.web.server.ServerWebExchange)65 Duration (java.time.Duration)60 ArrayList (java.util.ArrayList)58 Test (org.junit.Test)53 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)52 DataBuffer (org.springframework.core.io.buffer.DataBuffer)51 Assert (org.springframework.util.Assert)49 Mockito.mock (org.mockito.Mockito.mock)48 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)46 Optional (java.util.Optional)44 StandardCharsets (java.nio.charset.StandardCharsets)43