Browse Source

Moves GatewaySampleApplicationTests to use local httpbin

pull/305/head
Spencer Gibb 7 years ago
parent
commit
123327bfd2
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 12
      spring-cloud-gateway-core/pom.xml
  2. 11
      spring-cloud-gateway-sample/pom.xml
  3. 28
      spring-cloud-gateway-sample/src/main/java/org/springframework/cloud/gateway/sample/GatewaySampleApplication.java
  4. 7
      spring-cloud-gateway-sample/src/main/kotlin/org/springframework/cloud/gateway/sample/AdditionalRoutes.kt
  5. 8
      spring-cloud-gateway-sample/src/main/resources/application.yml
  6. 45
      spring-cloud-gateway-sample/src/test/java/org/springframework/cloud/gateway/sample/GatewaySampleApplicationTests.java

12
spring-cloud-gateway-core/pom.xml

@ -179,6 +179,18 @@ @@ -179,6 +179,18 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>

11
spring-cloud-gateway-sample/pom.xml

@ -33,11 +33,22 @@ @@ -33,11 +33,22 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.isomorphism</groupId>
<artifactId>token-bucket</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

28
spring-cloud-gateway-sample/src/main/java/org/springframework/cloud/gateway/sample/GatewaySampleApplication.java

@ -42,7 +42,7 @@ import org.springframework.web.reactive.function.server.ServerResponse; @@ -42,7 +42,7 @@ import org.springframework.web.reactive.function.server.ServerResponse;
@Import(AdditionalRoutes.class)
public class GatewaySampleApplication {
@Value("${route.uri:http://httpbin.org:80}")
@Value("${test.uri:http://httpbin.org:80}")
String uri;
@Bean
@ -51,20 +51,23 @@ public class GatewaySampleApplication { @@ -51,20 +51,23 @@ public class GatewaySampleApplication {
// String uri = "http://httpbin.org:80";
// String uri = "http://localhost:9080";
return builder.routes()
.route(r -> r.host("**.abc.org").and().path("/image/png")
.route(r -> r.host("**.abc.org").and().path("/anything/png")
.filters(f ->
f.addResponseHeader("X-TestHeader", "foobar"))
f.prefixPath("/httpbin")
.addResponseHeader("X-TestHeader", "foobar"))
.uri(uri)
)
.route("read_body_pred", r -> r.host("*.readbody.org")
.and().readBody(String.class,
s -> s.trim().equalsIgnoreCase("hello"))
.filters(f ->
f.addRequestHeader("X-TestHeader", "read_body_pred")
f.prefixPath("/httpbin")
.addRequestHeader("X-TestHeader", "read_body_pred")
).uri(uri)
)
.route("rewrite_request_obj", r -> r.host("*.rewriterequestobj.org")
.filters(f -> f.addRequestHeader("X-TestHeader", "rewrite_request")
.filters(f -> f.prefixPath("/httpbin")
.addRequestHeader("X-TestHeader", "rewrite_request")
.modifyRequestBody(String.class, Hello.class,
(exchange, s) -> {
return new Hello(s.toUpperCase());
@ -72,7 +75,8 @@ public class GatewaySampleApplication { @@ -72,7 +75,8 @@ public class GatewaySampleApplication {
).uri(uri)
)
.route("rewrite_request_upper", r -> r.host("*.rewriterequestupper.org")
.filters(f -> f.addRequestHeader("X-TestHeader", "rewrite_request_upper")
.filters(f -> f.prefixPath("/httpbin")
.addRequestHeader("X-TestHeader", "rewrite_request_upper")
.modifyRequestBody(String.class, String.class,
(exchange, s) -> {
return s.toUpperCase();
@ -80,7 +84,8 @@ public class GatewaySampleApplication { @@ -80,7 +84,8 @@ public class GatewaySampleApplication {
).uri(uri)
)
.route("rewrite_response_upper", r -> r.host("*.rewriteresponseupper.org")
.filters(f -> f.addRequestHeader("X-TestHeader", "rewrite_response_upper")
.filters(f -> f.prefixPath("/httpbin")
.addRequestHeader("X-TestHeader", "rewrite_response_upper")
.modifyResponseBody(String.class, String.class,
(exchange, s) -> {
return s.toUpperCase();
@ -88,7 +93,8 @@ public class GatewaySampleApplication { @@ -88,7 +93,8 @@ public class GatewaySampleApplication {
).uri(uri)
)
.route("rewrite_response_obj", r -> r.host("*.rewriteresponseobj.org")
.filters(f -> f.addRequestHeader("X-TestHeader", "rewrite_response_obj")
.filters(f -> f.prefixPath("/httpbin")
.addRequestHeader("X-TestHeader", "rewrite_response_obj")
.modifyResponseBody(Map.class, String.class,
(exchange, map) -> {
Object data = map.get("data");
@ -98,12 +104,14 @@ public class GatewaySampleApplication { @@ -98,12 +104,14 @@ public class GatewaySampleApplication {
)
.route(r -> r.path("/image/webp")
.filters(f ->
f.addResponseHeader("X-AnotherHeader", "baz"))
f.prefixPath("/httpbin")
.addResponseHeader("X-AnotherHeader", "baz"))
.uri(uri)
)
.route(r -> r.order(-1)
.host("**.throttle.org").and().path("/get")
.filters(f -> f.filter(new ThrottleGatewayFilter()
.filters(f -> f.prefixPath("/httpbin")
.filter(new ThrottleGatewayFilter()
.setCapacity(1)
.setRefillTokens(1)
.setRefillPeriod(10)

7
spring-cloud-gateway-sample/src/main/kotlin/org/springframework/cloud/gateway/sample/AdditionalRoutes.kt

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package org.springframework.cloud.gateway.sample
import org.springframework.beans.factory.annotation.Value
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder
import org.springframework.cloud.gateway.route.builder.filters
import org.springframework.cloud.gateway.route.builder.routes
@ -9,14 +10,18 @@ import org.springframework.context.annotation.Configuration @@ -9,14 +10,18 @@ import org.springframework.context.annotation.Configuration
@Configuration
open class AdditionalRoutes {
@Value("\${test.uri:http://httpbin.org:80}")
var uri: String? = null
@Bean
open fun additionalRouteLocator(builder: RouteLocatorBuilder) = builder.routes {
route(id = "test-kotlin") {
host("kotlin.abc.org") and path("/image/png")
filters {
prefixPath("/httpbin")
addResponseHeader("X-TestHeader", "foobar")
}
uri("http://httpbin.org:80")
uri(uri)
}
}

8
spring-cloud-gateway-sample/src/main/resources/application.yml

@ -1,13 +1,17 @@ @@ -1,13 +1,17 @@
test:
hostport: httpbin.org:80
# hostport: httpbin.org:80
# hostport: localhost:5000
uri: http://${test.hostport}
# uri: http://${test.hostport}
uri: lb://httpbin
spring:
jmx:
enabled: false
cloud:
gateway:
default-filters:
- PrefixPath=/httpbin
- AddResponseHeader=X-Response-Default-Foo, Default-Bar
routes:

45
spring-cloud-gateway-sample/src/test/java/org/springframework/cloud/gateway/sample/GatewaySampleApplicationTests.java

@ -17,26 +17,38 @@ @@ -17,26 +17,38 @@
package org.springframework.cloud.gateway.sample;
import java.time.Duration;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
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.test.HttpBinCompatibleController;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.StaticServerList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.SocketUtils;
import java.time.Duration;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/**
* @author Spencer Gibb
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GatewaySampleApplication.class, webEnvironment = RANDOM_PORT)
@SpringBootTest(classes = { GatewaySampleApplicationTests.TestConfig.class},
webEnvironment = RANDOM_PORT)
public class GatewaySampleApplicationTests {
@LocalServerPort
@ -50,6 +62,7 @@ public class GatewaySampleApplicationTests { @@ -50,6 +62,7 @@ public class GatewaySampleApplicationTests {
@BeforeClass
public static void beforeClass() {
managementPort = SocketUtils.findAvailableTcpPort();
System.setProperty("management.server.port", String.valueOf(managementPort));
}
@ -75,7 +88,7 @@ public class GatewaySampleApplicationTests { @@ -75,7 +88,7 @@ public class GatewaySampleApplicationTests {
@Test
public void complexPredicate() {
webClient.get()
.uri("/image/png")
.uri("/anything/png")
.header("Host", "www.abc.org")
.exchange()
.expectHeader().valueEquals("X-TestHeader", "foobar")
@ -90,4 +103,28 @@ public class GatewaySampleApplicationTests { @@ -90,4 +103,28 @@ public class GatewaySampleApplicationTests {
.exchange()
.expectStatus().isOk();
}
@Configuration
@EnableAutoConfiguration
@RibbonClient(name = "httpbin", configuration = RibbonConfig.class)
@Import(GatewaySampleApplication.class)
protected static class TestConfig {
@Bean
public HttpBinCompatibleController httpBinCompatibleController() {
return new HttpBinCompatibleController();
}
}
protected static class RibbonConfig {
@LocalServerPort
int port;
@Bean
@Primary
public ServerList<Server> ribbonServerList() {
return new StaticServerList<>(new Server("localhost", port));
}
}
}

Loading…
Cancel
Save