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();
}
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);
}
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();
}
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();
}
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;
}
}));
});
}
Aggregations