Skip to content

Commit

Permalink
Checks for null list if there are no query parameters.
Browse files Browse the repository at this point in the history
fixes gh-374
  • Loading branch information
spencergibb committed Jul 6, 2018
1 parent 6a4f608 commit 6ceb65b
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ protected Mono<Route> lookupRoute(ServerWebExchange exchange) {
.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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public Predicate<ServerWebExchange> apply(Config config) {


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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}

}

0 comments on commit 6ceb65b

Please sign in to comment.