From abcc4ac979ca7b0d10e06d3e97b65be38ac41c82 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 31 May 2017 09:30:04 -0400 Subject: [PATCH] Fix composability issue in ExchangeMutatorWebFilter This commit replaces the UnaryOperatory inputs with Function instead. Unfortunately the composability methods inherited from Function don't work in terms of generics for composing multiple UnaryOperator's. Issue: SPR-15599 --- .../server/ExchangeMutatorWebFilter.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeMutatorWebFilter.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeMutatorWebFilter.java index b5467923af9..7cb247394be 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeMutatorWebFilter.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeMutatorWebFilter.java @@ -18,7 +18,6 @@ package org.springframework.test.web.reactive.server; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; -import java.util.function.UnaryOperator; import reactor.core.publisher.Mono; @@ -46,16 +45,24 @@ import org.springframework.web.server.WebFilterChain; *

Example usage: * *

- * ExchangeMutatorWebFilter mutator = new ExchangeMutatorWebFilter(exchange -> ...);
+ * Function<ServerWebExchange, ServerWebExchange> fn1 = ...;
+ * Function<ServerWebExchange, ServerWebExchange> fn2 = ...;
+ *
+ * ExchangeMutatorWebFilter mutator = new ExchangeMutatorWebFilter(fn1().andThen(fn2()));
  * WebTestClient client = WebTestClient.bindToController(new MyController()).webFilter(mutator).build();
  * 
* + * *

It is also possible to apply "per request" transformations: * *

- * ExchangeMutatorWebFilter mutator = new ExchangeMutatorWebFilter(exchange -> ...);
+ * ExchangeMutatorWebFilter mutator = new ExchangeMutatorWebFilter();
  * WebTestClient client = WebTestClient.bindToController(new MyController()).webFilter(mutator).build();
- * client.filter(mutator.perClient(exchange -> ...)).get().uri("/").exchange();
+ *
+ * Function<ServerWebExchange, ServerWebExchange> fn1 = ...;
+ * Function<ServerWebExchange, ServerWebExchange> fn2 = ...;
+ *
+ * client.filter(mutator.perClient(fn1().andThen(fn2()))).get().uri("/").exchange();
  * 
* * @author Rossen Stoyanchev @@ -69,7 +76,11 @@ public class ExchangeMutatorWebFilter implements WebFilter { new ConcurrentHashMap<>(4); - public ExchangeMutatorWebFilter(UnaryOperator processor) { + public ExchangeMutatorWebFilter() { + this(exchange -> exchange); + } + + public ExchangeMutatorWebFilter(Function processor) { Assert.notNull(processor, "'processor' is required"); this.processor = processor; } @@ -100,7 +111,7 @@ public class ExchangeMutatorWebFilter implements WebFilter { * @param processor the exchange processor to use * @return client filter for use with {@link WebTestClient#filter} */ - public ExchangeFilterFunction perClient(UnaryOperator processor) { + public ExchangeFilterFunction perClient(Function processor) { return (request, next) -> { String id = getRequestId(request.headers()); this.perRequestProcessors.compute(id,