Skip to content

Commit

Permalink
fix: Update HeaderRoutePredicateFactory to use getValuesAsList
Browse files Browse the repository at this point in the history
  • Loading branch information
ohprettyhak committed Jul 1, 2024
1 parent 17c1b5b commit 448b0b2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.springframework.cloud.gateway.handler.predicate;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -59,8 +58,7 @@ public Predicate<ServerWebExchange> apply(Config config) {
return new GatewayPredicate() {
@Override
public boolean test(ServerWebExchange exchange) {
List<String> values = exchange.getRequest().getHeaders().getOrDefault(config.header,
Collections.emptyList());
List<String> values = exchange.getRequest().getHeaders().getValuesAsList(config.header);
if (values.isEmpty()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public void toStringFormat() {
assertThat(predicate.toString()).contains("Header: myheader regexp=myregexp");
}

@Test
public void headerRouteHandlesCommaSeparatedValues() {
testClient.get().uri("/get").header("X-Example-Header", "value1, value2 ,exact_match,value3").exchange()
.expectStatus().isOk().expectHeader()
.valueEquals(HANDLER_MAPPER_HEADER, RoutePredicateHandlerMapping.class.getSimpleName()).expectHeader()
.valueEquals(ROUTE_ID_HEADER, "header_test_comma_separated");
}

@EnableAutoConfiguration
@SpringBootConfiguration
@Import(DefaultTestConfig.class)
Expand All @@ -85,6 +93,8 @@ public static class TestConfig {
RouteLocator queryRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("header_exists_dsl", r -> r.header("X-Foo").filters(f -> f.prefixPath("/httpbin")).uri(uri))
.route("header_test_comma_separated", r -> r.header("X-Example-Header", "exact_match")
.filters(f -> f.prefixPath("/httpbin")).uri(uri))
.build();
}

Expand Down

0 comments on commit 448b0b2

Please sign in to comment.