Autoconfigure CloudFoundryRouteServiceRoutePredicateFactory and add cloudFoundryRouteService in DSL (#294)
This commit is contained in:
committed by
Spencer Gibb
parent
0a62617320
commit
fdef92b3b7
@@ -89,6 +89,7 @@ import org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping;
|
||||
import org.springframework.cloud.gateway.handler.predicate.AfterRoutePredicateFactory;
|
||||
import org.springframework.cloud.gateway.handler.predicate.BeforeRoutePredicateFactory;
|
||||
import org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicateFactory;
|
||||
import org.springframework.cloud.gateway.handler.predicate.CloudFoundryRouteServiceRoutePredicateFactory;
|
||||
import org.springframework.cloud.gateway.handler.predicate.CookieRoutePredicateFactory;
|
||||
import org.springframework.cloud.gateway.handler.predicate.HeaderRoutePredicateFactory;
|
||||
import org.springframework.cloud.gateway.handler.predicate.HostRoutePredicateFactory;
|
||||
@@ -432,6 +433,11 @@ public class GatewayAutoConfiguration {
|
||||
return new WeightRoutePredicateFactory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CloudFoundryRouteServiceRoutePredicateFactory cloudFoundryRouteServiceRoutePredicateFactory() {
|
||||
return new CloudFoundryRouteServiceRoutePredicateFactory();
|
||||
}
|
||||
|
||||
// GatewayFilter Factory beans
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.springframework.cloud.gateway.handler.AsyncPredicate;
|
||||
import org.springframework.cloud.gateway.handler.predicate.AfterRoutePredicateFactory;
|
||||
import org.springframework.cloud.gateway.handler.predicate.BeforeRoutePredicateFactory;
|
||||
import org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicateFactory;
|
||||
import org.springframework.cloud.gateway.handler.predicate.CloudFoundryRouteServiceRoutePredicateFactory;
|
||||
import org.springframework.cloud.gateway.handler.predicate.CookieRoutePredicateFactory;
|
||||
import org.springframework.cloud.gateway.handler.predicate.HeaderRoutePredicateFactory;
|
||||
import org.springframework.cloud.gateway.handler.predicate.HostRoutePredicateFactory;
|
||||
@@ -248,6 +249,12 @@ public class PredicateSpec extends UriSpec {
|
||||
.setWeight(weight)));
|
||||
}
|
||||
|
||||
public BooleanSpec cloudFoundryRouteService() {
|
||||
return predicate(
|
||||
getBean(CloudFoundryRouteServiceRoutePredicateFactory.class).apply(c -> {
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* A predicate which is always true
|
||||
* @return a {@link BooleanSpec} to be used to add logical operators
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package org.springframework.cloud.gateway.handler.predicate;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
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 com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
|
||||
/**
|
||||
* @author Toshiaki Maki
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
||||
@DirtiesContext
|
||||
public class CloudFoundryRouteServiceRoutePredicateFactoryIntegrationTests
|
||||
extends BaseWebClientTests {
|
||||
@LocalServerPort
|
||||
int port;
|
||||
|
||||
@Test
|
||||
public void predicateWorkWithProperties() {
|
||||
testClient.get().uri("/").header("Host", "props.routeservice.example.com")
|
||||
.header("X-CF-Forwarded-Url",
|
||||
"http://localhost:" + port + "/actuator/health")
|
||||
.header("X-CF-Proxy-Signature", "foo")
|
||||
.header("X-CF-Proxy-Metadata", "bar").exchange()
|
||||
.expectBody(JsonNode.class)
|
||||
.consumeWith(r -> assertThat(r.getResponseBody().has("status")).isTrue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void predicateWillNotWorkUnlessHeadersAreEnough() {
|
||||
testClient.get().uri("/").header("Host", "props.routeservice.example.com")
|
||||
.header("X-CF-Forwarded-Url",
|
||||
"http://localhost:" + port + "/actuator/health")
|
||||
.header("X-CF-Proxy-Metadata", "bar").exchange().expectStatus().isOk()
|
||||
.expectHeader().valueEquals(ROUTE_ID_HEADER, "default_path_to_httpbin");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void predicateWorkWithDsl() {
|
||||
testClient.get().uri("/").header("Host", "dsl.routeservice.example.com")
|
||||
.header("X-CF-Forwarded-Url",
|
||||
"http://localhost:" + port + "/actuator/health")
|
||||
.header("X-CF-Proxy-Signature", "foo")
|
||||
.header("X-CF-Proxy-Metadata", "bar").exchange()
|
||||
.expectBody(JsonNode.class)
|
||||
.consumeWith(r -> assertThat(r.getResponseBody().has("status")).isTrue());
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@SpringBootConfiguration
|
||||
@Import(DefaultTestConfig.class)
|
||||
public static class TestConfig {
|
||||
@Bean
|
||||
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
|
||||
return builder.routes().route(r -> r.cloudFoundryRouteService().and()
|
||||
.header("Host", "dsl.routeservice.example.com")
|
||||
.filters(f -> f.requestHeaderToRequestUri("X-CF-Forwarded-Url"))
|
||||
.uri("http://example.com")).build();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -253,6 +253,14 @@ spring:
|
||||
filters:
|
||||
- RequestHeaderToRequestUri=X-CF-Forwarded-Url
|
||||
|
||||
# =====================================
|
||||
- id: cloudfoundry_routeservice_test
|
||||
uri: ${test.uri}
|
||||
predicates:
|
||||
- CloudFoundryRouteService=
|
||||
filters:
|
||||
- RequestHeaderToRequestUri=X-CF-Forwarded-Url
|
||||
|
||||
# =====================================
|
||||
- id: default_path_to_httpbin
|
||||
uri: ${test.uri}
|
||||
|
||||
Reference in New Issue
Block a user