Fix AfterRoutePredicate
This commit is contained in:
@@ -5,6 +5,8 @@ import java.util.function.Predicate;
|
||||
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicate.parseZonedDateTime;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@@ -12,22 +14,11 @@ public class AfterRoutePredicate implements RoutePredicate {
|
||||
|
||||
@Override
|
||||
public Predicate<ServerWebExchange> apply(String dateString, String[] args) {
|
||||
//TODO: is ZonedDateTime the right thing to use?
|
||||
try {
|
||||
final long epoch = Long.parseLong(dateString);
|
||||
final ZonedDateTime dateTime = parseZonedDateTime(dateString);
|
||||
|
||||
return exchange -> {
|
||||
final long now = System.currentTimeMillis();
|
||||
return epoch >= now;
|
||||
};
|
||||
} catch (NumberFormatException e) {
|
||||
// try ZonedDateTime instead
|
||||
final ZonedDateTime dateTime = ZonedDateTime.parse(dateString);
|
||||
|
||||
return exchange -> {
|
||||
final ZonedDateTime now = ZonedDateTime.now();
|
||||
return dateTime.isAfter(now);
|
||||
};
|
||||
}
|
||||
return exchange -> {
|
||||
final ZonedDateTime now = ZonedDateTime.now();
|
||||
return now.isAfter(dateTime);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
package org.springframework.cloud.gateway.handler.predicate;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.MockServerHttpResponse;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicateTests.getExchange;
|
||||
import static org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicateTests.minusHours;
|
||||
import static org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicateTests.minusHoursMillis;
|
||||
import static org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicateTests.plusHours;
|
||||
import static org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicateTests.plusHoursMillis;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
@@ -18,42 +16,41 @@ public class AfterRoutePredicateTests {
|
||||
|
||||
@Test
|
||||
public void beforeStringWorks() {
|
||||
String dateString = ZonedDateTime.now().minusHours(1).format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
|
||||
String dateString = minusHours(1);
|
||||
|
||||
final boolean result = new AfterRoutePredicate().apply(dateString, null).test(getExchange());
|
||||
final boolean result = runPredicate(dateString);
|
||||
|
||||
assertThat(result).isFalse();
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void afterStringWorks() {
|
||||
String dateString = ZonedDateTime.now().plusHours(1).format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
|
||||
String dateString = plusHours(1);
|
||||
|
||||
final boolean result = new AfterRoutePredicate().apply(dateString, null).test(getExchange());
|
||||
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beforeEpochWorks() {
|
||||
String dateString = String.valueOf(System.currentTimeMillis()-(1000*60*60));
|
||||
|
||||
final boolean result = new AfterRoutePredicate().apply(dateString, null).test(getExchange());
|
||||
final boolean result = runPredicate(dateString);
|
||||
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void afterEpochWorks() {
|
||||
String dateString = String.valueOf(System.currentTimeMillis()+(1000*60*60));
|
||||
public void beforeEpochWorks() {
|
||||
String dateString = minusHoursMillis(1);
|
||||
|
||||
final boolean result = new AfterRoutePredicate().apply(dateString, null).test(getExchange());
|
||||
final boolean result = runPredicate(dateString);
|
||||
|
||||
assertThat(result).isTrue();
|
||||
}
|
||||
|
||||
private ServerWebExchange getExchange() {
|
||||
final MockServerHttpRequest request = MockServerHttpRequest.get("http://example.com").build();
|
||||
return new DefaultServerWebExchange(request, new MockServerHttpResponse());
|
||||
@Test
|
||||
public void afterEpochWorks() {
|
||||
String dateString = plusHoursMillis(1);
|
||||
|
||||
final boolean result = runPredicate(dateString);
|
||||
|
||||
assertThat(result).isFalse();
|
||||
}
|
||||
|
||||
private boolean runPredicate(String dateString) {
|
||||
return new AfterRoutePredicate().apply(dateString, null).test(getExchange());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ spring:
|
||||
- Query=foo, ba.
|
||||
- Query=baz
|
||||
- Cookie=chocolate, ch.p
|
||||
- After=1900-01-20T17:42:47.789-07:00[America/Denver]
|
||||
filters:
|
||||
- AddResponseHeader=X-Response-Foo, Bar
|
||||
|
||||
|
||||
Reference in New Issue
Block a user