Checks for null list if there are no query parameters.

fixes gh-374
This commit is contained in:
Spencer Gibb
2018-07-06 16:42:12 -04:00
parent 6a4f608204
commit 6ceb65b7cf
3 changed files with 95 additions and 1 deletions

View File

@@ -92,7 +92,12 @@ public class RoutePredicateHandlerMapping extends AbstractHandlerMapping {
.filterWhen(route -> {
// add the current route we are testing
exchange.getAttributes().put(GATEWAY_PREDICATE_ROUTE_ATTR, route.getId());
return route.getPredicate().apply(exchange);
try {
return route.getPredicate().apply(exchange);
} catch (Exception e) {
logger.error("Error applying predicate for route: "+route.getId(), e);
}
return Mono.just(false);
})
// .defaultIfEmpty() put a static Route not found
// or .switchIfEmpty()

View File

@@ -54,6 +54,9 @@ public class QueryRoutePredicateFactory extends AbstractRoutePredicateFactory<Qu
List<String> values = exchange.getRequest().getQueryParams().get(config.param);
if (values == null) {
return false;
}
for (String value : values) {
if (value.matches(config.regexp)) {
return true;

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.springframework.cloud.gateway.handler.predicate;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.gateway.test.BaseWebClientTests;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class QueryRoutePredicateFactoryTests extends BaseWebClientTests {
@Rule
public OutputCapture output = new OutputCapture();
@Test
public void noQueryParamWorks() {
testClient.get().uri("/get")
.exchange()
.expectStatus().isOk()
.expectHeader().valueEquals(ROUTE_ID_HEADER, "default_path_to_httpbin");;
output.expect(not(containsString("Error applying predicate for route: foo_query_param")));
}
@Test
public void queryParamWorks() {
testClient.get().uri("/get?foo=bar")
.exchange()
.expectStatus().isOk()
.expectHeader().valueEquals(ROUTE_ID_HEADER, "foo_query_param");;
}
@EnableAutoConfiguration
@SpringBootConfiguration
@Import(DefaultTestConfig.class)
public static class TestConfig {
@Value("${test.uri}")
private String uri;
@Bean
RouteLocator queryRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("foo_query_param", r ->
r.query("foo", "bar")
.filters(f -> f.prefixPath("/httpbin"))
.uri(uri))
.build();
}
}
}