From e64b2fbe1bbcaabdf8ac978dc4fbcde30ea90252 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 15 Mar 2017 00:00:39 -0600 Subject: [PATCH] Moves to named Tuple args Moves most RequestPredicateFactories to named Tuple arguments rather than indexed. --- .../DiscoveryClientRouteLocator.java | 3 +- .../RequestPredicateHandlerMapping.java | 30 ++++++++++++++++--- .../AfterRequestPredicateFactory.java | 12 ++++++-- .../BeforeRequestPredicateFactory.java | 12 ++++++-- .../BetweenRequestPredicateFactory.java | 13 ++++---- .../CookieRequestPredicateFactory.java | 14 +++++++-- .../HeaderRequestPredicateFactory.java | 14 +++++++-- .../HostRequestPredicateFactory.java | 12 ++++++-- .../MethodRequestPredicateFactory.java | 13 ++++++-- .../PathRequestPredicateFactory.java | 25 ++++++++++++++-- .../QueryRequestPredicateFactory.java | 22 ++++++++++++-- .../predicate/RequestPredicateFactory.java | 15 ++++++++++ .../cloud/gateway/support/NameUtils.java | 2 +- .../AfterRequestPredicateFactoryTests.java | 3 +- .../BeforeRequestPredicateFactoryTests.java | 3 +- .../BetweenRequestPredicateFactoryTests.java | 5 +++- .../src/test/resources/application.yml | 2 +- 17 files changed, 164 insertions(+), 36 deletions(-) diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteLocator.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteLocator.java index e0abc0696..6cd3d168d 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteLocator.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteLocator.java @@ -28,6 +28,7 @@ import org.springframework.cloud.gateway.model.FilterDefinition; import org.springframework.cloud.gateway.model.PredicateDefinition; import org.springframework.cloud.gateway.model.Route; +import static org.springframework.cloud.gateway.handler.predicate.PathRequestPredicateFactory.PATTERN_KEY; import static org.springframework.cloud.gateway.support.NameUtils.normalizeFilterName; import static org.springframework.cloud.gateway.support.NameUtils.normalizePredicateName; @@ -58,7 +59,7 @@ public class DiscoveryClientRouteLocator implements RouteLocator { // add a predicate that matches the url at /serviceId/** PredicateDefinition predicate = new PredicateDefinition(); predicate.setName(normalizePredicateName(PathRequestPredicateFactory.class)); - predicate.setArgs(Collections.singletonMap("path", "/" + serviceId + "/**")); + predicate.setArgs(Collections.singletonMap(PATTERN_KEY, "/" + serviceId + "/**")); route.getPredicates().add(predicate); //TODO: support for other default predicates diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/RequestPredicateHandlerMapping.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/RequestPredicateHandlerMapping.java index f2f5ee587..96e5c670f 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/RequestPredicateHandlerMapping.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/RequestPredicateHandlerMapping.java @@ -27,6 +27,7 @@ import org.springframework.cloud.gateway.api.RouteLocator; import org.springframework.cloud.gateway.handler.predicate.RequestPredicateFactory; import org.springframework.cloud.gateway.model.PredicateDefinition; import org.springframework.cloud.gateway.model.Route; +import org.springframework.cloud.gateway.support.NameUtils; import org.springframework.tuple.Tuple; import org.springframework.tuple.TupleBuilder; import org.springframework.web.reactive.function.server.PublicDefaultServerRequest; @@ -146,7 +147,6 @@ public class RequestPredicateHandlerMapping extends AbstractHandlerMapping { } } - private RequestPredicate combinePredicates(Route route) { List predicates = route.getPredicates(); RequestPredicate predicate = lookup(route, predicates.get(0)); @@ -159,20 +159,42 @@ public class RequestPredicateHandlerMapping extends AbstractHandlerMapping { return predicate; } + //TODO: decouple from HandlerMapping? private RequestPredicate lookup(Route route, PredicateDefinition predicate) { RequestPredicateFactory found = this.routePredicates.get(predicate.getName()); if (found == null) { throw new IllegalArgumentException("Unable to find RequestPredicateFactory with name " + predicate.getName()); } + Map args = predicate.getArgs(); if (logger.isDebugEnabled()) { logger.debug("Route " + route.getId() + " applying " - + predicate.getArgs() + " to " + predicate.getName()); + + args + " to " + predicate.getName()); } TupleBuilder builder = TupleBuilder.tuple(); - for (Map.Entry entry : predicate.getArgs().entrySet()) { - builder.put(entry.getKey(), entry.getValue()); + List argNames = found.argNames(); + if (!argNames.isEmpty()) { + // ensure size is the same for key replacement later + if (found.validateArgSize() && args.size() != argNames.size()) { + throw new IllegalArgumentException("Wrong number of arguments. Expected " + argNames + + " " + argNames + ". Found "+ args.size() +" " + args +"'"); + } + } + + int entryIdx = 0; + for (Map.Entry entry : args.entrySet()) { + String key = entry.getKey(); + + // RequestPredicateFactory has name hints and this has a fake key name + // replace with the matching key hint + if (key.startsWith(NameUtils.GENERATED_NAME_PREFIX) && !argNames.isEmpty() + && entryIdx < args.size()) { + key = argNames.get(entryIdx); + } + + builder.put(key, entry.getValue()); + entryIdx++; } Tuple tuple = builder.build(); diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/AfterRequestPredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/AfterRequestPredicateFactory.java index ea75dfce8..b2124abd3 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/AfterRequestPredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/AfterRequestPredicateFactory.java @@ -18,6 +18,8 @@ package org.springframework.cloud.gateway.handler.predicate; import java.time.ZonedDateTime; +import java.util.Collections; +import java.util.List; import org.springframework.tuple.Tuple; import org.springframework.web.reactive.function.server.RequestPredicate; @@ -29,10 +31,16 @@ import static org.springframework.cloud.gateway.handler.predicate.BetweenRequest */ public class AfterRequestPredicateFactory implements RequestPredicateFactory { + public static final String DATETIME_KEY = "datetime"; + + @Override + public List argNames() { + return Collections.singletonList(DATETIME_KEY); + } + @Override public RequestPredicate apply(Tuple args) { - validate(1, args); - final ZonedDateTime dateTime = parseZonedDateTime(args.getString(0)); + final ZonedDateTime dateTime = parseZonedDateTime(args.getString(DATETIME_KEY)); return request -> { final ZonedDateTime now = ZonedDateTime.now(); diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BeforeRequestPredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BeforeRequestPredicateFactory.java index 3057fe624..290eaea3e 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BeforeRequestPredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BeforeRequestPredicateFactory.java @@ -18,6 +18,8 @@ package org.springframework.cloud.gateway.handler.predicate; import java.time.ZonedDateTime; +import java.util.Collections; +import java.util.List; import org.springframework.tuple.Tuple; import org.springframework.web.reactive.function.server.RequestPredicate; @@ -29,10 +31,16 @@ import static org.springframework.cloud.gateway.handler.predicate.BetweenRequest */ public class BeforeRequestPredicateFactory implements RequestPredicateFactory { + public static final String DATETIME_KEY = "datetime"; + + @Override + public List argNames() { + return Collections.singletonList(DATETIME_KEY); + } + @Override public RequestPredicate apply(Tuple args) { - validate(1, args); - final ZonedDateTime dateTime = parseZonedDateTime(args.getString(0)); + final ZonedDateTime dateTime = parseZonedDateTime(args.getString(DATETIME_KEY)); return request -> { final ZonedDateTime now = ZonedDateTime.now(); diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BetweenRequestPredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BetweenRequestPredicateFactory.java index a4a376b7c..2867e3ebf 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BetweenRequestPredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/BetweenRequestPredicateFactory.java @@ -30,15 +30,16 @@ import org.springframework.web.reactive.function.server.RequestPredicate; */ public class BetweenRequestPredicateFactory implements RequestPredicateFactory { + public static final String DATETIME1_KEY = "datetime1"; + public static final String DATETIME2_KEY = "datetime2"; + @Override public RequestPredicate apply(Tuple args) { - validate(2, args); - //TODO: is ZonedDateTime the right thing to use? - final ZonedDateTime dateTime1 = parseZonedDateTime(args.getString(0)); - final ZonedDateTime dateTime2 = parseZonedDateTime(args.getString(1)); - Assert.isTrue(dateTime1.isBefore(dateTime2), args.getString(0) + - " must be before " + args.getString(1)); + final ZonedDateTime dateTime1 = parseZonedDateTime(args.getString(DATETIME1_KEY)); + final ZonedDateTime dateTime2 = parseZonedDateTime(args.getString(DATETIME2_KEY)); + Assert.isTrue(dateTime1.isBefore(dateTime2), args.getString(DATETIME1_KEY) + + " must be before " + args.getString(DATETIME2_KEY)); return request -> { final ZonedDateTime now = ZonedDateTime.now(); diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/CookieRequestPredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/CookieRequestPredicateFactory.java index ca4b56833..4af8d06a4 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/CookieRequestPredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/CookieRequestPredicateFactory.java @@ -17,6 +17,7 @@ package org.springframework.cloud.gateway.handler.predicate; +import java.util.Arrays; import java.util.List; import org.springframework.http.HttpCookie; @@ -29,11 +30,18 @@ import org.springframework.web.reactive.function.server.RequestPredicate; */ public class CookieRequestPredicateFactory implements RequestPredicateFactory { + public static final String NAME_KEY = "name"; + public static final String REGEXP_KEY = "regexp"; + + @Override + public List argNames() { + return Arrays.asList(NAME_KEY, REGEXP_KEY); + } + @Override public RequestPredicate apply(Tuple args) { - validate(2, args); - String name = args.getString(0); - String regexp = args.getString(1); + String name = args.getString(NAME_KEY); + String regexp = args.getString(REGEXP_KEY); return request -> { //TODO: bad cast? diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/HeaderRequestPredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/HeaderRequestPredicateFactory.java index 99a878aaf..7c23ccbb8 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/HeaderRequestPredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/HeaderRequestPredicateFactory.java @@ -17,6 +17,7 @@ package org.springframework.cloud.gateway.handler.predicate; +import java.util.Arrays; import java.util.List; import org.springframework.tuple.Tuple; @@ -28,11 +29,18 @@ import org.springframework.web.reactive.function.server.RequestPredicates; */ public class HeaderRequestPredicateFactory implements RequestPredicateFactory { + public static final String HEADER_KEY = "header"; + public static final String REGEXP_KEY = "regexp"; + + @Override + public List argNames() { + return Arrays.asList(HEADER_KEY, REGEXP_KEY); + } + @Override public RequestPredicate apply(Tuple args) { - validate(2, args); - String header = args.getString(0); - String regexp = args.getString(1); + String header = args.getString(HEADER_KEY); + String regexp = args.getString(REGEXP_KEY); return RequestPredicates.headers(headers -> { List values = headers.asHttpHeaders().get(header); diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/HostRequestPredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/HostRequestPredicateFactory.java index 245421379..49a3e16e3 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/HostRequestPredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/HostRequestPredicateFactory.java @@ -17,6 +17,9 @@ package org.springframework.cloud.gateway.handler.predicate; +import java.util.Collections; +import java.util.List; + import org.springframework.tuple.Tuple; import org.springframework.util.AntPathMatcher; import org.springframework.util.PathMatcher; @@ -28,16 +31,21 @@ import org.springframework.web.reactive.function.server.RequestPredicates; */ public class HostRequestPredicateFactory implements RequestPredicateFactory { + public static final String PATTERN_KEY = "pattern"; private PathMatcher pathMatcher = new AntPathMatcher("."); public void setPathMatcher(PathMatcher pathMatcher) { this.pathMatcher = pathMatcher; } + @Override + public List argNames() { + return Collections.singletonList(PATTERN_KEY); + } + @Override public RequestPredicate apply(Tuple args) { - validate(1, args); - String pattern = args.getString(0); + String pattern = args.getString(PATTERN_KEY); return RequestPredicates.headers(headers -> { String host = headers.asHttpHeaders().getFirst("Host"); diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/MethodRequestPredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/MethodRequestPredicateFactory.java index 9be5d35a1..165ad906c 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/MethodRequestPredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/MethodRequestPredicateFactory.java @@ -22,15 +22,24 @@ import org.springframework.tuple.Tuple; import org.springframework.web.reactive.function.server.RequestPredicate; import org.springframework.web.reactive.function.server.RequestPredicates; +import java.util.Arrays; +import java.util.List; + /** * @author Spencer Gibb */ public class MethodRequestPredicateFactory implements RequestPredicateFactory { + public static final String METHOD_KEY = "method"; + + @Override + public List argNames() { + return Arrays.asList(METHOD_KEY); + } + @Override public RequestPredicate apply(Tuple args) { - validate(1, args); - String method = args.getString(0); + String method = args.getString(METHOD_KEY); return RequestPredicates.method(HttpMethod.resolve(method)); } } diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/PathRequestPredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/PathRequestPredicateFactory.java index e56130f0f..e0226b48c 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/PathRequestPredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/PathRequestPredicateFactory.java @@ -20,18 +20,37 @@ package org.springframework.cloud.gateway.handler.predicate; import org.springframework.tuple.Tuple; import org.springframework.web.reactive.function.server.RequestPredicate; import org.springframework.web.reactive.function.server.RequestPredicates; +import org.springframework.web.util.patterns.PathPatternParser; + +import java.util.Collections; +import java.util.List; /** * @author Spencer Gibb */ public class PathRequestPredicateFactory implements RequestPredicateFactory { + public static final String PATTERN_KEY = "pattern"; + + private PathPatternParser pathPatternParser; + + public void setPathPatternParser(PathPatternParser pathPatternParser) { + this.pathPatternParser = pathPatternParser; + } + + @Override + public List argNames() { + return Collections.singletonList(PATTERN_KEY); + } + @Override public RequestPredicate apply(Tuple args) { - validate(1, args); - String pattern = args.getString(0); + String pattern = args.getString(PATTERN_KEY); + + if (this.pathPatternParser != null) { + return RequestPredicates.pathPredicates(this.pathPatternParser).apply(pattern); + } - //TODO: support custom PathPatternParser return RequestPredicates.path(pattern); } } diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/QueryRequestPredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/QueryRequestPredicateFactory.java index ff3d2dc3f..8381dab4c 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/QueryRequestPredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/QueryRequestPredicateFactory.java @@ -22,17 +22,33 @@ import org.springframework.web.reactive.function.server.PublicDefaultServerReque import org.springframework.web.reactive.function.server.RequestPredicate; import org.springframework.web.reactive.function.server.RequestPredicates; +import java.util.Arrays; +import java.util.List; + /** * @author Spencer Gibb */ public class QueryRequestPredicateFactory implements RequestPredicateFactory { + public static final String PARAM_KEY = "param"; + public static final String REGEXP_KEY = "regexp"; + + @Override + public List argNames() { + return Arrays.asList(PARAM_KEY, REGEXP_KEY); + } + + @Override + public boolean validateArgSize() { + return false; + } + @Override public RequestPredicate apply(Tuple args) { validate(1, args); - String param = args.getString(0); + String param = args.getString(PARAM_KEY); - if (args.size() < 2) { + if (!args.hasFieldName(REGEXP_KEY)) { return req -> { //TODO: ServerRequest support for query params with no value PublicDefaultServerRequest request = (PublicDefaultServerRequest) req; @@ -40,7 +56,7 @@ public class QueryRequestPredicateFactory implements RequestPredicateFactory { }; } - String regexp = args.getString(1); + String regexp = args.getString(REGEXP_KEY); return RequestPredicates.queryParam(param, value -> value.matches(regexp)); } diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/RequestPredicateFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/RequestPredicateFactory.java index 453b5682f..f1a909195 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/RequestPredicateFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/handler/predicate/RequestPredicateFactory.java @@ -17,6 +17,9 @@ package org.springframework.cloud.gateway.handler.predicate; +import java.util.Collections; +import java.util.List; + import org.springframework.tuple.Tuple; import org.springframework.util.Assert; import org.springframework.web.reactive.function.server.RequestPredicate; @@ -28,6 +31,18 @@ public interface RequestPredicateFactory { RequestPredicate apply(Tuple args); + /** + * Returns hints about the number of args and the order for shortcut parsing. + * @return + */ + default List argNames() { + return Collections.emptyList(); + } + + default boolean validateArgSize() { + return true; + } + default void validate(int minimumSize, Tuple args) { Assert.isTrue(args != null && args.size() >= minimumSize, "args must have at least "+ minimumSize +" entry(s)"); diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/NameUtils.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/NameUtils.java index 102a9cedf..a941da81f 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/NameUtils.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/NameUtils.java @@ -24,7 +24,7 @@ import org.springframework.cloud.gateway.handler.predicate.RequestPredicateFacto * @author Spencer Gibb */ public class NameUtils { - public static final String GENERATED_NAME_PREFIX = "__:_._gen__+_"; + public static final String GENERATED_NAME_PREFIX = "_genkey_"; public static String generateName(int i) { return GENERATED_NAME_PREFIX + i; diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/AfterRequestPredicateFactoryTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/AfterRequestPredicateFactoryTests.java index 94ce3a826..690227135 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/AfterRequestPredicateFactoryTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/AfterRequestPredicateFactoryTests.java @@ -20,6 +20,7 @@ package org.springframework.cloud.gateway.handler.predicate; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.gateway.handler.predicate.AfterRequestPredicateFactory.DATETIME_KEY; import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactoryTests.getRequest; import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactoryTests.minusHours; import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactoryTests.minusHoursMillis; @@ -69,6 +70,6 @@ public class AfterRequestPredicateFactoryTests { } private boolean runPredicate(String dateString) { - return new AfterRequestPredicateFactory().apply(tuple().of("1", dateString)).test(getRequest()); + return new AfterRequestPredicateFactory().apply(tuple().of(DATETIME_KEY, dateString)).test(getRequest()); } } diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BeforeRequestPredicateFactoryTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BeforeRequestPredicateFactoryTests.java index ac7109b14..c464e84d9 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BeforeRequestPredicateFactoryTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BeforeRequestPredicateFactoryTests.java @@ -20,6 +20,7 @@ package org.springframework.cloud.gateway.handler.predicate; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.gateway.handler.predicate.BeforeRequestPredicateFactory.DATETIME_KEY; import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactoryTests.getRequest; import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactoryTests.minusHours; import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactoryTests.minusHoursMillis; @@ -69,6 +70,6 @@ public class BeforeRequestPredicateFactoryTests { } private boolean runPredicate(String dateString) { - return new BeforeRequestPredicateFactory().apply(tuple().of("1", dateString)).test(getRequest()); + return new BeforeRequestPredicateFactory().apply(tuple().of(DATETIME_KEY, dateString)).test(getRequest()); } } diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BetweenRequestPredicateFactoryTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BetweenRequestPredicateFactoryTests.java index f4a20256c..b27ec8251 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BetweenRequestPredicateFactoryTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/handler/predicate/BetweenRequestPredicateFactoryTests.java @@ -28,6 +28,8 @@ import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.server.adapter.DefaultServerWebExchange; import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactory.DATETIME1_KEY; +import static org.springframework.cloud.gateway.handler.predicate.BetweenRequestPredicateFactory.DATETIME2_KEY; import static org.springframework.tuple.TupleBuilder.tuple; /** @@ -96,7 +98,8 @@ public class BetweenRequestPredicateFactoryTests { } boolean runPredicate(String dateString1, String dateString2) { - return new BetweenRequestPredicateFactory().apply(tuple().of("1", dateString1, "2", dateString2)).test(getRequest()); + return new BetweenRequestPredicateFactory().apply(tuple() + .of(DATETIME1_KEY, dateString1, DATETIME2_KEY, dateString2)).test(getRequest()); } static String minusHoursMillis(int hours) { diff --git a/spring-cloud-gateway-core/src/test/resources/application.yml b/spring-cloud-gateway-core/src/test/resources/application.yml index d23f0f0b0..598de5048 100644 --- a/spring-cloud-gateway-core/src/test/resources/application.yml +++ b/spring-cloud-gateway-core/src/test/resources/application.yml @@ -180,7 +180,7 @@ spring: predicates: - name: Path args: - path: /** + pattern: /** #myservice: # ribbon: