Browse Source

Break out many tests from GatewayIntegrationTests

pull/41/head
Spencer Gibb 8 years ago
parent
commit
a93932c5e7
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 15
      src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteLocator.java
  2. 2
      src/main/java/org/springframework/cloud/gateway/filter/route/RemoveRequestHeaderRouteFilter.java
  3. 49
      src/test/java/org/springframework/cloud/gateway/filter/route/AddRequestHeaderRouteFilterIntegrationTests.java
  4. 58
      src/test/java/org/springframework/cloud/gateway/filter/route/AddRequestParameterRouteFilterIntegrationTests.java
  5. 64
      src/test/java/org/springframework/cloud/gateway/filter/route/HystrixRouteFilterIntegrationTests.java
  6. 50
      src/test/java/org/springframework/cloud/gateway/filter/route/RedirectToRouteFilterIntegrationTests.java
  7. 56
      src/test/java/org/springframework/cloud/gateway/filter/route/RemoveNonProxyHeadersRouteFilterIntegrationTests.java
  8. 50
      src/test/java/org/springframework/cloud/gateway/filter/route/RemoveRequestHeaderRouteFilterIntegrationTests.java
  9. 46
      src/test/java/org/springframework/cloud/gateway/filter/route/RemoveResponseHeaderRouteFilterIntegrationTests.java
  10. 47
      src/test/java/org/springframework/cloud/gateway/filter/route/RewritePathRouteFilterIntegrationTests.java
  11. 66
      src/test/java/org/springframework/cloud/gateway/filter/route/SecureHeadersRouteFilterIntegrationTests.java
  12. 45
      src/test/java/org/springframework/cloud/gateway/filter/route/SetPathRouteFilterIntegrationTests.java
  13. 47
      src/test/java/org/springframework/cloud/gateway/filter/route/SetResponseRouteFilterIntegrationTests.java
  14. 56
      src/test/java/org/springframework/cloud/gateway/filter/route/SetStatusRouteFilterIntegrationTests.java
  15. 55
      src/test/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateIntegrationTests.java
  16. 54
      src/test/java/org/springframework/cloud/gateway/handler/predicate/UrlRoutePredicateIntegrationTests.java
  17. 53
      src/test/java/org/springframework/cloud/gateway/test/AdhocTestSuite.java
  18. 67
      src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java
  19. 87
      src/test/java/org/springframework/cloud/gateway/test/FormIntegrationTests.java
  20. 418
      src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java
  21. 23
      src/test/java/org/springframework/cloud/gateway/test/TestUtils.java
  22. 16
      src/test/resources/application-removenonproxyheaders.yml

15
src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteLocator.java

@ -1,14 +1,19 @@ @@ -1,14 +1,19 @@
package org.springframework.cloud.gateway.discovery;
import java.net.URI;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.gateway.api.RouteLocator;
import org.springframework.cloud.gateway.api.FilterDefinition;
import org.springframework.cloud.gateway.api.PredicateDefinition;
import org.springframework.cloud.gateway.api.Route;
import org.springframework.cloud.gateway.api.RouteLocator;
import org.springframework.cloud.gateway.filter.route.RewritePathRouteFilter;
import org.springframework.cloud.gateway.handler.predicate.UrlRoutePredicate;
import reactor.core.publisher.Flux;
import static org.springframework.cloud.gateway.support.NameUtils.normalizeFilterName;
import static org.springframework.cloud.gateway.support.NameUtils.normalizePredicateName;
import java.net.URI;
import reactor.core.publisher.Flux;
/**
* TODO: developer configuration, in zuul, this was opt out, should be opt in
@ -34,7 +39,7 @@ public class DiscoveryClientRouteLocator implements RouteLocator { @@ -34,7 +39,7 @@ public class DiscoveryClientRouteLocator implements RouteLocator {
// add a predicate that matches the url at /serviceId/**
PredicateDefinition predicate = new PredicateDefinition();
predicate.setName("Url");
predicate.setName(normalizePredicateName(UrlRoutePredicate.class));
predicate.setArgs("/" + serviceId + "/**");
route.getPredicates().add(predicate);
@ -42,7 +47,7 @@ public class DiscoveryClientRouteLocator implements RouteLocator { @@ -42,7 +47,7 @@ public class DiscoveryClientRouteLocator implements RouteLocator {
// add a filter that removes /serviceId by default
FilterDefinition filter = new FilterDefinition();
filter.setName("RewritePath");
filter.setName(normalizeFilterName(RewritePathRouteFilter.class));
String regex = "/" + serviceId + "/(?<remaining>.*)";
String replacement = "/${remaining}";
filter.setArgs(regex, replacement);

2
src/main/java/org/springframework/cloud/gateway/filter/route/RemoveRequestHeaderRouteFilter.java

@ -8,7 +8,7 @@ import org.springframework.http.server.reactive.ServerHttpRequest; @@ -8,7 +8,7 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
*/
public class RemoveRequestHeaderRouteFilter implements RouteFilter {
public static final String FAKE_HEADER = "_______force_______";
private static final String FAKE_HEADER = "_______force_______";
@Override
public WebFilter apply(String... args) {

49
src/test/java/org/springframework/cloud/gateway/filter/route/AddRequestHeaderRouteFilterIntegrationTests.java

@ -0,0 +1,49 @@ @@ -0,0 +1,49 @@
package org.springframework.cloud.gateway.filter.route;
import java.util.Map;
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.cloud.gateway.test.BaseWebClientTests;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.test.TestUtils.getMap;
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class AddRequestHeaderRouteFilterIntegrationTests extends BaseWebClientTests {
@Test
public void addRequestHeaderFilterWorks() {
Mono<Map> result = webClient.get()
.uri("/headers")
.header("Host", "www.addrequestheader.org")
.exchange()
.then(response -> response.body(toMono(Map.class)));
StepVerifier.create(result)
.consumeNextWith(
response -> {
Map<String, Object> headers = getMap(response, "headers");
assertThat(headers).containsEntry("X-Request-Foo", "Bar");
})
.expectComplete()
.verify(DURATION);
}
@EnableAutoConfiguration
@SpringBootConfiguration
public static class TestConfig { }
}

58
src/test/java/org/springframework/cloud/gateway/filter/route/AddRequestParameterRouteFilterIntegrationTests.java

@ -0,0 +1,58 @@ @@ -0,0 +1,58 @@
package org.springframework.cloud.gateway.filter.route;
import java.util.Map;
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.cloud.gateway.test.BaseWebClientTests;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.test.TestUtils.getMap;
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class AddRequestParameterRouteFilterIntegrationTests extends BaseWebClientTests {
@Test
public void addRequestParameterFilterWorksBlankQuery() {
testRequestParameterFilter("");
}
@Test
public void addRequestParameterFilterWorksNonBlankQuery() {
testRequestParameterFilter("?baz=bam");
}
private void testRequestParameterFilter(String query) {
Mono<Map> result = webClient.get()
.uri("/get" + query)
.header("Host", "www.addrequestparameter.org")
.exchange()
.then(response -> response.body(toMono(Map.class)));
StepVerifier.create(result)
.consumeNextWith(
response -> {
Map<String, Object> args = getMap(response, "args");
assertThat(args).containsEntry("foo", "bar");
})
.expectComplete()
.verify(DURATION);
}
@EnableAutoConfiguration
@SpringBootConfiguration
public static class TestConfig { }
}

64
src/test/java/org/springframework/cloud/gateway/filter/route/HystrixRouteFilterIntegrationTests.java

@ -0,0 +1,64 @@ @@ -0,0 +1,64 @@
package org.springframework.cloud.gateway.filter.route;
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.cloud.gateway.test.BaseWebClientTests;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.ClientResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.test.TestUtils.assertStatus;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class HystrixRouteFilterIntegrationTests extends BaseWebClientTests {
@Test
public void hystrixFilterWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/get")
.header("Host", "www.hystrixsuccess.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, HttpStatus.OK);
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders.getFirst(ROUTE_ID_HEADER))
.isEqualTo("hystrix_success_test");
})
.expectComplete()
.verify(DURATION);
}
@Test
public void hystrixFilterTimesout() {
Mono<ClientResponse> result = webClient.get()
.uri("/delay/3")
.header("Host", "www.hystrixfailure.org")
.exchange();
StepVerifier.create(result)
.expectError() //TODO: can we get more specific as to the error?
.verify();
}
@EnableAutoConfiguration
@SpringBootConfiguration
@Import(DefaultTestConfig.class)
public static class TestConfig { }
}

50
src/test/java/org/springframework/cloud/gateway/filter/route/RedirectToRouteFilterIntegrationTests.java

@ -0,0 +1,50 @@ @@ -0,0 +1,50 @@
package org.springframework.cloud.gateway.filter.route;
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.cloud.gateway.test.BaseWebClientTests;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.ClientResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.test.TestUtils.assertStatus;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class RedirectToRouteFilterIntegrationTests extends BaseWebClientTests {
@Test
public void redirectToFilterWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/")
.header("Host", "www.redirectto.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, HttpStatus.FOUND);
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders.getFirst(HttpHeaders.LOCATION))
.isEqualTo("http://example.org");
})
.expectComplete()
.verify(DURATION);
}
@EnableAutoConfiguration
@SpringBootConfiguration
public static class TestConfig { }
}

56
src/test/java/org/springframework/cloud/gateway/filter/route/RemoveNonProxyHeadersRouteFilterIntegrationTests.java

@ -0,0 +1,56 @@ @@ -0,0 +1,56 @@
package org.springframework.cloud.gateway.filter.route;
import java.util.Map;
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.cloud.gateway.test.BaseWebClientTests;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.filter.route.RemoveNonProxyHeadersRouteFilter.DEFAULT_HEADERS_TO_REMOVE;
import static org.springframework.cloud.gateway.test.TestUtils.getMap;
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
//TODO: why does this break other tests if not in a profile?
@ActiveProfiles("removenonproxyheaders")
@DirtiesContext
public class RemoveNonProxyHeadersRouteFilterIntegrationTests extends BaseWebClientTests {
@Test
public void removeNonProxyHeadersFilterWorks() {
Mono<Map> result = webClient.get()
.uri("/headers")
.header("Host", "www.removerequestheader.org")
.header("Proxy-Authorization", "myauth")
.exchange()
.then(response -> response.body(toMono(Map.class)));
StepVerifier.create(result)
.consumeNextWith(
response -> {
Map<String, Object> headers = getMap(response, "headers");
for (String header : DEFAULT_HEADERS_TO_REMOVE) {
assertThat(headers).doesNotContainKey(header);
}
})
.expectComplete()
.verify(DURATION);
}
@EnableAutoConfiguration
@SpringBootConfiguration
public static class TestConfig { }
}

50
src/test/java/org/springframework/cloud/gateway/filter/route/RemoveRequestHeaderRouteFilterIntegrationTests.java

@ -0,0 +1,50 @@ @@ -0,0 +1,50 @@
package org.springframework.cloud.gateway.filter.route;
import java.util.Map;
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.cloud.gateway.test.BaseWebClientTests;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.test.TestUtils.getMap;
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class RemoveRequestHeaderRouteFilterIntegrationTests extends BaseWebClientTests {
@Test
public void removeRequestHeaderFilterWorks() {
Mono<Map> result = webClient.get()
.uri("/headers")
.header("Host", "www.removerequestheader.org")
.header("X-Request-Foo", "Bar")
.exchange()
.then(response -> response.body(toMono(Map.class)));
StepVerifier.create(result)
.consumeNextWith(
response -> {
Map<String, Object> headers = getMap(response, "headers");
assertThat(headers).doesNotContainKey("X-Request-Foo");
})
.expectComplete()
.verify(DURATION);
}
@EnableAutoConfiguration
@SpringBootConfiguration
public static class TestConfig { }
}

46
src/test/java/org/springframework/cloud/gateway/filter/route/RemoveResponseHeaderRouteFilterIntegrationTests.java

@ -0,0 +1,46 @@ @@ -0,0 +1,46 @@
package org.springframework.cloud.gateway.filter.route;
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.cloud.gateway.test.BaseWebClientTests;
import org.springframework.http.HttpHeaders;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.ClientResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class RemoveResponseHeaderRouteFilterIntegrationTests extends BaseWebClientTests {
@Test
public void removeResponseHeaderFilterWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/headers")
.header("Host", "www.removereresponseheader.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders).doesNotContainKey("X-Request-Foo");
})
.expectComplete()
.verify(DURATION);
}
@EnableAutoConfiguration
@SpringBootConfiguration
public static class TestConfig { }
}

47
src/test/java/org/springframework/cloud/gateway/filter/route/RewritePathRouteFilterIntegrationTests.java

@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
package org.springframework.cloud.gateway.filter.route;
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.cloud.gateway.test.BaseWebClientTests;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.ClientResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.test.TestUtils.assertStatus;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class RewritePathRouteFilterIntegrationTests extends BaseWebClientTests {
@Test
public void rewritePathFilterWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/foo/get")
.header("Host", "www.baz.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, HttpStatus.OK);
})
.expectComplete()
.verify(DURATION);
}
@EnableAutoConfiguration
@SpringBootConfiguration
public static class TestConfig { }
}

66
src/test/java/org/springframework/cloud/gateway/filter/route/SecureHeadersRouteFilterIntegrationTests.java

@ -0,0 +1,66 @@ @@ -0,0 +1,66 @@
package org.springframework.cloud.gateway.filter.route;
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.cloud.gateway.test.BaseWebClientTests;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.ClientResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.CONTENT_SECURITY_POLICY_HEADER;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.REFERRER_POLICY_HEADER;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.STRICT_TRANSPORT_SECURITY_HEADER;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.X_CONTENT_TYPE_OPTIONS_HEADER;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.X_DOWNLOAD_OPTIONS_HEADER;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.X_FRAME_OPTIONS_HEADER;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.X_PERMITTED_CROSS_DOMAIN_POLICIES_HEADER;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.X_XSS_PROTECTION_HEADER;
import static org.springframework.cloud.gateway.test.TestUtils.assertStatus;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class SecureHeadersRouteFilterIntegrationTests extends BaseWebClientTests {
@Test
public void secureHeadersFilterWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/headers")
.header("Host", "www.secureheaders.org")
.exchange();
SecureHeadersProperties defaults = new SecureHeadersProperties();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, HttpStatus.OK);
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders.getFirst(X_XSS_PROTECTION_HEADER)).isEqualTo(defaults.getXssProtectionHeader());
assertThat(httpHeaders.getFirst(STRICT_TRANSPORT_SECURITY_HEADER)).isEqualTo(defaults.getStrictTransportSecurity());
assertThat(httpHeaders.getFirst(X_FRAME_OPTIONS_HEADER)).isEqualTo(defaults.getFrameOptions());
assertThat(httpHeaders.getFirst(X_CONTENT_TYPE_OPTIONS_HEADER)).isEqualTo(defaults.getContentTypeOptions());
assertThat(httpHeaders.getFirst(REFERRER_POLICY_HEADER)).isEqualTo(defaults.getReferrerPolicy());
assertThat(httpHeaders.getFirst(CONTENT_SECURITY_POLICY_HEADER)).isEqualTo(defaults.getContentSecurityPolicy());
assertThat(httpHeaders.getFirst(X_DOWNLOAD_OPTIONS_HEADER)).isEqualTo(defaults.getDownloadOptions());
assertThat(httpHeaders.getFirst(X_PERMITTED_CROSS_DOMAIN_POLICIES_HEADER)).isEqualTo(defaults.getPermittedCrossDomainPolicies());
})
.expectComplete()
.verify(DURATION);
}
@EnableAutoConfiguration
@SpringBootConfiguration
public static class TestConfig { }
}

45
src/test/java/org/springframework/cloud/gateway/filter/route/SetPathRouteFilterIntegrationTests.java

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
package org.springframework.cloud.gateway.filter.route;
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.cloud.gateway.test.BaseWebClientTests;
import org.springframework.http.HttpStatus;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.ClientResponse;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.test.TestUtils.assertStatus;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class SetPathRouteFilterIntegrationTests extends BaseWebClientTests {
@Test
public void setPathFilterDefaultValuesWork() {
Mono<ClientResponse> result = webClient.get()
.uri("/foo/get")
.header("Host", "www.setpath.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, HttpStatus.OK);
})
.expectComplete()
.verify(DURATION);
}
@EnableAutoConfiguration
@SpringBootConfiguration
public static class TestConfig { }
}

47
src/test/java/org/springframework/cloud/gateway/filter/route/SetResponseRouteFilterIntegrationTests.java

@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
package org.springframework.cloud.gateway.filter.route;
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.cloud.gateway.test.BaseWebClientTests;
import org.springframework.http.HttpHeaders;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.ClientResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class SetResponseRouteFilterIntegrationTests extends BaseWebClientTests {
@Test
public void setResponseHeaderFilterWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/headers")
.header("Host", "www.setreresponseheader.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders).containsKey("X-Request-Foo");
assertThat(httpHeaders.get("X-Request-Foo")).containsExactly("Bar");
})
.expectComplete()
.verify(DURATION);
}
@EnableAutoConfiguration
@SpringBootConfiguration
public static class TestConfig { }
}

56
src/test/java/org/springframework/cloud/gateway/filter/route/SetStatusRouteFilterIntegrationTests.java

@ -0,0 +1,56 @@ @@ -0,0 +1,56 @@
package org.springframework.cloud.gateway.filter.route;
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.cloud.gateway.test.BaseWebClientTests;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.ClientResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.test.TestUtils.assertStatus;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class SetStatusRouteFilterIntegrationTests extends BaseWebClientTests {
@Test
public void setStatusIntWorks() {
setStatusStringTest("www.setstatusint.org", HttpStatus.UNAUTHORIZED);
}
@Test
public void setStatusStringWorks() {
setStatusStringTest("www.setstatusstring.org", HttpStatus.BAD_REQUEST);
}
private void setStatusStringTest(String host, HttpStatus status) {
Mono<ClientResponse> result = webClient.get()
.uri("/headers")
.header("Host", host)
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, status);
})
.expectComplete()
.verify(DURATION);
}
@EnableAutoConfiguration
@SpringBootConfiguration
public static class TestConfig { }
}

55
src/test/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateIntegrationTests.java

@ -0,0 +1,55 @@ @@ -0,0 +1,55 @@
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.cloud.gateway.handler.RoutePredicateHandlerMapping;
import org.springframework.cloud.gateway.test.BaseWebClientTests;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.ClientResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.test.TestUtils.assertStatus;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class HostRoutePredicateIntegrationTests extends BaseWebClientTests {
@Test
public void hostRouteWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/get")
.header("Host", "www.example.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, HttpStatus.OK);
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders.getFirst(HANDLER_MAPPER_HEADER))
.isEqualTo(RoutePredicateHandlerMapping.class.getSimpleName());
assertThat(httpHeaders.getFirst(ROUTE_ID_HEADER))
.isEqualTo("host_example_to_httpbin");
})
.expectComplete()
.verify(DURATION);
}
@EnableAutoConfiguration
@SpringBootConfiguration
@Import(DefaultTestConfig.class)
public static class TestConfig { }
}

54
src/test/java/org/springframework/cloud/gateway/handler/predicate/UrlRoutePredicateIntegrationTests.java

@ -0,0 +1,54 @@ @@ -0,0 +1,54 @@
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.cloud.gateway.handler.RoutePredicateHandlerMapping;
import org.springframework.cloud.gateway.test.BaseWebClientTests;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.reactive.function.client.ClientResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.test.TestUtils.assertStatus;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class UrlRoutePredicateIntegrationTests extends BaseWebClientTests {
@Test
public void urlRouteWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/get")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, HttpStatus.OK);
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders.getFirst(HANDLER_MAPPER_HEADER))
.isEqualTo(RoutePredicateHandlerMapping.class.getSimpleName());
assertThat(httpHeaders.getFirst(ROUTE_ID_HEADER))
.isEqualTo("default_path_to_httpbin");
})
.expectComplete()
.verify(DURATION);
}
@EnableAutoConfiguration
@SpringBootConfiguration
@Import(DefaultTestConfig.class)
public static class TestConfig { }
}

53
src/test/java/org/springframework/cloud/gateway/test/AdhocTestSuite.java

@ -0,0 +1,53 @@ @@ -0,0 +1,53 @@
package org.springframework.cloud.gateway.test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.springframework.cloud.gateway.filter.route.AddRequestHeaderRouteFilterIntegrationTests;
import org.springframework.cloud.gateway.filter.route.AddRequestParameterRouteFilterIntegrationTests;
import org.springframework.cloud.gateway.filter.route.HystrixRouteFilterIntegrationTests;
import org.springframework.cloud.gateway.filter.route.RedirectToRouteFilterIntegrationTests;
import org.springframework.cloud.gateway.filter.route.RemoveNonProxyHeadersRouteFilterIntegrationTests;
import org.springframework.cloud.gateway.filter.route.RemoveRequestHeaderRouteFilterIntegrationTests;
import org.springframework.cloud.gateway.filter.route.RewritePathRouteFilterTests;
import org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilterIntegrationTests;
import org.springframework.cloud.gateway.filter.route.SetPathRouteFilterIntegrationTests;
import org.springframework.cloud.gateway.filter.route.SetPathRouteFilterTests;
import org.springframework.cloud.gateway.filter.route.SetResponseRouteFilterIntegrationTests;
import org.springframework.cloud.gateway.filter.route.SetStatusRouteFilterIntegrationTests;
import org.springframework.cloud.gateway.handler.predicate.AfterRoutePredicateTests;
import org.springframework.cloud.gateway.handler.predicate.BeforeRoutePredicateTests;
import org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicateTests;
import org.springframework.cloud.gateway.handler.predicate.HostRoutePredicateIntegrationTests;
import org.springframework.cloud.gateway.handler.predicate.UrlRoutePredicateIntegrationTests;
/**
* @author Spencer Gibb
*/
@Ignore
@RunWith(Suite.class)
@SuiteClasses({GatewayIntegrationTests.class,
FormIntegrationTests.class,
// route filter tests
AddRequestHeaderRouteFilterIntegrationTests.class,
AddRequestParameterRouteFilterIntegrationTests.class,
HystrixRouteFilterIntegrationTests.class,
RedirectToRouteFilterIntegrationTests.class,
RemoveNonProxyHeadersRouteFilterIntegrationTests.class,
RemoveRequestHeaderRouteFilterIntegrationTests.class,
SecureHeadersRouteFilterIntegrationTests.class,
SetPathRouteFilterIntegrationTests.class,
SetPathRouteFilterTests.class,
SetResponseRouteFilterIntegrationTests.class,
SetStatusRouteFilterIntegrationTests.class,
RewritePathRouteFilterTests.class,
// route predicate tests
AfterRoutePredicateTests.class,
BeforeRoutePredicateTests.class,
BetweenRoutePredicateTests.class,
HostRoutePredicateIntegrationTests.class,
UrlRoutePredicateIntegrationTests.class,
})
public class AdhocTestSuite {
}

67
src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java

@ -0,0 +1,67 @@ @@ -0,0 +1,67 @@
package org.springframework.cloud.gateway.test;
import java.time.Duration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.cloud.gateway.api.Route;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.reactive.function.client.WebClient;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_HANDLER_MAPPER_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;
/**
* @author Spencer Gibb
*/
public class BaseWebClientTests {
protected static final String HANDLER_MAPPER_HEADER = "X-Gateway-Handler-Mapper-Class";
protected static final String ROUTE_ID_HEADER = "X-Gateway-Route-Id";
protected static final Duration DURATION = Duration.ofSeconds(5);
static {
//TODO: wait for option in boot 2.0
System.setProperty("java.net.preferIPv4Stack", "true");
}
@LocalServerPort
protected int port = 0;
protected WebClient webClient;
protected String baseUri;
@Before
public void setup() {
//TODO: how to set new ReactorClientHttpConnector()
baseUri = "http://localhost:" + port;
this.webClient = WebClient.create(baseUri);
}
@Configuration
protected static class DefaultTestConfig {
private static final Log log = LogFactory.getLog(DefaultTestConfig.class);
@Bean
@Order(500)
public GlobalFilter modifyResponseFilter() {
return (exchange, chain) -> {
log.info("modifyResponseFilter start");
String value = (String) exchange.getAttribute(GATEWAY_HANDLER_MAPPER_ATTR).orElse("N/A");
exchange.getResponse().getHeaders().add(HANDLER_MAPPER_HEADER, value);
Route route = (Route) exchange.getAttribute(GATEWAY_ROUTE_ATTR).orElse(null);
if (route != null) {
exchange.getResponse().getHeaders().add(ROUTE_ID_HEADER, route.getId());
}
return chain.filter(exchange);
};
}
}
}

87
src/test/java/org/springframework/cloud/gateway/test/FormIntegrationTests.java

@ -0,0 +1,87 @@ @@ -0,0 +1,87 @@
package org.springframework.cloud.gateway.test;
import java.nio.charset.Charset;
import java.util.Map;
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.core.io.ClassPathResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.BodyInserters;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.test.TestUtils.getMap;
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
@SuppressWarnings("unchecked")
public class FormIntegrationTests extends BaseWebClientTests {
@Test
public void formUrlencodedWorks() {
LinkedMultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add("foo", "bar");
formData.add("baz", "bam");
MediaType contentType = new MediaType(MediaType.APPLICATION_FORM_URLENCODED, Charset.forName("UTF-8"));
Mono<Map> result = webClient.post()
.uri("/post")
.contentType(contentType)
.exchange(BodyInserters.fromFormData(formData))
.then(response -> response.body(toMono(Map.class)));
StepVerifier.create(result)
.consumeNextWith(map -> {
Map<String, Object> form = getMap(map, "form");
assertThat(form).containsEntry("foo", "bar");
assertThat(form).containsEntry("baz", "bam");
})
.expectComplete()
.verify(DURATION);
}
@Test
public void multipartFormDataWorks() {
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("file", new ClassPathResource("1x1.png"));
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(form, headers);
ResponseEntity<Map> response = restTemplate.exchange(baseUri + "/post", HttpMethod.POST, entity, Map.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
Map<String, Object> files = getMap(response.getBody(), "files");
assertThat(files).containsKey("file");
assertThat((String)files.get("file")).startsWith("data:application/octet-stream;base64,");
}
@EnableAutoConfiguration
@SpringBootConfiguration
//@Import(DefaultTestConfig.class)
public static class TestConfig { }
}

418
src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java

@ -1,153 +1,48 @@ @@ -1,153 +1,48 @@
package org.springframework.cloud.gateway.test;
import java.nio.charset.Charset;
import java.time.Duration;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.gateway.api.Route;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.route.SecureHeadersProperties;
import org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ServerWebExchange;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.CONTENT_SECURITY_POLICY_HEADER;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.REFERRER_POLICY_HEADER;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.STRICT_TRANSPORT_SECURITY_HEADER;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.X_CONTENT_TYPE_OPTIONS_HEADER;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.X_DOWNLOAD_OPTIONS_HEADER;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.X_FRAME_OPTIONS_HEADER;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.X_PERMITTED_CROSS_DOMAIN_POLICIES_HEADER;
import static org.springframework.cloud.gateway.filter.route.SecureHeadersRouteFilter.X_XSS_PROTECTION_HEADER;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_HANDLER_MAPPER_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR;
import static org.springframework.cloud.gateway.test.TestUtils.assertStatus;
import static org.springframework.cloud.gateway.test.TestUtils.getMap;
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext//(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
@SpringBootTest(webEnvironment = RANDOM_PORT/*,
properties = "spring.cloud.gateway.default-filters:AddResponseHeader=\"*/)
@DirtiesContext
@SuppressWarnings("unchecked")
public class GatewayIntegrationTests {
private static final String HANDLER_MAPPER_HEADER = "X-Gateway-Handler-Mapper-Class";
private static final String ROUTE_ID_HEADER = "X-Gateway-Route-Id";
private static final Duration DURATION = Duration.ofSeconds(5);
static {
System.setProperty("java.net.preferIPv4Stack", "true");
}
@LocalServerPort
private int port = 0;
private WebClient webClient;
private String baseUri;
public class GatewayIntegrationTests extends BaseWebClientTests {
@Autowired
private GatewayProperties properties;
@Before
public void setup() {
//TODO: how to set new ReactorClientHttpConnector()
baseUri = "http://localhost:" + port;
this.webClient = WebClient.create(baseUri);
}
@Test
public void addRequestHeaderFilterWorks() {
Mono<Map> result = webClient.get()
.uri("/headers")
.header("Host", "www.addrequestheader.org")
.exchange()
.then(response -> response.body(toMono(Map.class)));
StepVerifier.create(result)
.consumeNextWith(
response -> {
Map<String, Object> headers = getMap(response, "headers");
assertThat(headers).containsEntry("X-Request-Foo", "Bar");
})
.expectComplete()
.verify(DURATION);
}
@Test
public void addRequestParameterFilterWorksBlankQuery() {
testRequestParameterFilter("");
}
@Test
public void addRequestParameterFilterWorksNonBlankQuery() {
testRequestParameterFilter("?baz=bam");
}
private void testRequestParameterFilter(String query) {
Mono<Map> result = webClient.get()
.uri("/get" + query)
.header("Host", "www.addrequestparameter.org")
.exchange()
.then(response -> response.body(toMono(Map.class)));
StepVerifier.create(result)
.consumeNextWith(
response -> {
Map<String, Object> args = getMap(response, "args");
assertThat(args).containsEntry("foo", "bar");
})
.expectComplete()
.verify(DURATION);
}
@Test
public void addResponseHeaderFilterWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/headers")
.header("Host", "www.addresponseheader.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders.getFirst("X-Request-Foo"))
.isEqualTo("Bar");
})
.expectComplete()
.verify(DURATION);
}
@Test
public void complexContentTypeWorks() {
Mono<Map> result = webClient.get()
@ -213,58 +108,6 @@ public class GatewayIntegrationTests { @@ -213,58 +108,6 @@ public class GatewayIntegrationTests {
.verify(DURATION);
}
@Test
public void hostRouteWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/get")
.header("Host", "www.example.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, HttpStatus.OK);
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders.getFirst(HANDLER_MAPPER_HEADER))
.isEqualTo(RoutePredicateHandlerMapping.class.getSimpleName());
assertThat(httpHeaders.getFirst(ROUTE_ID_HEADER))
.isEqualTo("host_example_to_httpbin");
})
.expectComplete()
.verify(DURATION);
}
@Test
public void hystrixFilterWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/get")
.header("Host", "www.hystrixsuccess.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, HttpStatus.OK);
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders.getFirst(ROUTE_ID_HEADER))
.isEqualTo("hystrix_success_test");
})
.expectComplete()
.verify(DURATION);
}
@Test
public void hystrixFilterTimesout() {
Mono<ClientResponse> result = webClient.get()
.uri("/delay/3")
.header("Host", "www.hystrixfailure.org")
.exchange();
StepVerifier.create(result)
.expectError() //TODO: can we get more specific as to the error?
.verify();
}
@Test
public void loadBalancerFilterWorks() {
Mono<ClientResponse> result = webClient.get()
@ -284,49 +127,6 @@ public class GatewayIntegrationTests { @@ -284,49 +127,6 @@ public class GatewayIntegrationTests {
.verify(DURATION);
}
@Test
public void formUrlencodedWorks() {
LinkedMultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add("foo", "bar");
formData.add("baz", "bam");
MediaType contentType = new MediaType(MediaType.APPLICATION_FORM_URLENCODED, Charset.forName("UTF-8"));
Mono<Map> result = webClient.post()
.uri("/post")
.contentType(contentType)
.exchange(BodyInserters.fromFormData(formData))
.then(response -> response.body(toMono(Map.class)));
StepVerifier.create(result)
.consumeNextWith(map -> {
Map<String, Object> form = getMap(map, "form");
assertThat(form).containsEntry("foo", "bar");
assertThat(form).containsEntry("baz", "bam");
})
.expectComplete()
.verify(DURATION);
}
@Test
public void multipartFormDataWorks() {
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("file", new ClassPathResource("1x1.png"));
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(form, headers);
ResponseEntity<Map> response = restTemplate.exchange(baseUri + "/post", HttpMethod.POST, entity, Map.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
Map<String, Object> files = getMap(response.getBody(), "files");
assertThat(files).containsKey("file");
assertThat((String)files.get("file")).startsWith("data:application/octet-stream;base64,");
}
@Test
public void postWorks() {
Mono<Map> result = webClient.post()
@ -341,215 +141,13 @@ public class GatewayIntegrationTests { @@ -341,215 +141,13 @@ public class GatewayIntegrationTests {
.verify(DURATION);
}
@Test
public void redirectToFilterWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/")
.header("Host", "www.redirectto.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, HttpStatus.FOUND);
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders.getFirst(HttpHeaders.LOCATION))
.isEqualTo("http://example.org");
})
.expectComplete()
.verify(DURATION);
}
@Test
@SuppressWarnings("unchecked")
public void removeRequestHeaderFilterWorks() {
Mono<Map> result = webClient.get()
.uri("/headers")
.header("Host", "www.removerequestheader.org")
.header("X-Request-Foo", "Bar")
.exchange()
.then(response -> response.body(toMono(Map.class)));
StepVerifier.create(result)
.consumeNextWith(
response -> {
Map<String, Object> headers = getMap(response, "headers");
assertThat(headers).doesNotContainKey("X-Request-Foo");
})
.expectComplete()
.verify(DURATION);
}
@Test
public void removeResponseHeaderFilterWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/headers")
.header("Host", "www.removereresponseheader.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders).doesNotContainKey("X-Request-Foo");
})
.expectComplete()
.verify(DURATION);
}
@Test
public void rewritePathFilterWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/foo/get")
.header("Host", "www.baz.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, HttpStatus.OK);
})
.expectComplete()
.verify(DURATION);
}
@Test
public void secureHeadersFilterWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/headers")
.header("Host", "www.secureheaders.org")
.exchange();
SecureHeadersProperties defaults = new SecureHeadersProperties();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, HttpStatus.OK);
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders.getFirst(X_XSS_PROTECTION_HEADER)).isEqualTo(defaults.getXssProtectionHeader());
assertThat(httpHeaders.getFirst(STRICT_TRANSPORT_SECURITY_HEADER)).isEqualTo(defaults.getStrictTransportSecurity());
assertThat(httpHeaders.getFirst(X_FRAME_OPTIONS_HEADER)).isEqualTo(defaults.getFrameOptions());
assertThat(httpHeaders.getFirst(X_CONTENT_TYPE_OPTIONS_HEADER)).isEqualTo(defaults.getContentTypeOptions());
assertThat(httpHeaders.getFirst(REFERRER_POLICY_HEADER)).isEqualTo(defaults.getReferrerPolicy());
assertThat(httpHeaders.getFirst(CONTENT_SECURITY_POLICY_HEADER)).isEqualTo(defaults.getContentSecurityPolicy());
assertThat(httpHeaders.getFirst(X_DOWNLOAD_OPTIONS_HEADER)).isEqualTo(defaults.getDownloadOptions());
assertThat(httpHeaders.getFirst(X_PERMITTED_CROSS_DOMAIN_POLICIES_HEADER)).isEqualTo(defaults.getPermittedCrossDomainPolicies());
})
.expectComplete()
.verify(DURATION);
}
@Test
public void setPathFilterDefaultValuesWork() {
Mono<ClientResponse> result = webClient.get()
.uri("/foo/get")
.header("Host", "www.setpath.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, HttpStatus.OK);
})
.expectComplete()
.verify(DURATION);
}
@Test
public void setResponseHeaderFilterWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/headers")
.header("Host", "www.setreresponseheader.org")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders).containsKey("X-Request-Foo");
assertThat(httpHeaders.get("X-Request-Foo")).containsExactly("Bar");
})
.expectComplete()
.verify(DURATION);
}
@Test
public void setStatusIntWorks() {
setStatusStringTest("www.setstatusint.org", HttpStatus.UNAUTHORIZED);
}
@Test
public void setStatusStringWorks() {
setStatusStringTest("www.setstatusstring.org", HttpStatus.BAD_REQUEST);
}
private void setStatusStringTest(String host, HttpStatus status) {
Mono<ClientResponse> result = webClient.get()
.uri("/headers")
.header("Host", host)
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, status);
})
.expectComplete()
.verify(DURATION);
}
@Test
public void urlRouteWorks() {
Mono<ClientResponse> result = webClient.get()
.uri("/get")
.exchange();
StepVerifier.create(result)
.consumeNextWith(
response -> {
assertStatus(response, HttpStatus.OK);
HttpHeaders httpHeaders = response.headers().asHttpHeaders();
assertThat(httpHeaders.getFirst(HANDLER_MAPPER_HEADER))
.isEqualTo(RoutePredicateHandlerMapping.class.getSimpleName());
assertThat(httpHeaders.getFirst(ROUTE_ID_HEADER))
.isEqualTo("default_path_to_httpbin");
})
.expectComplete()
.verify(DURATION);
}
private Map<String, Object> getMap(Map response, String key) {
assertThat(response).containsKey(key).isInstanceOf(Map.class);
return (Map<String, Object>) response.get(key);
}
private void assertStatus(ClientResponse response, HttpStatus status) {
HttpStatus statusCode = response.statusCode();
assertThat(statusCode).isEqualTo(status);
}
@EnableAutoConfiguration
@SpringBootConfiguration
@Import(DefaultTestConfig.class)
public static class TestConfig {
private static final Log log = LogFactory.getLog(TestConfig.class);
@Bean
@Order(500)
public GlobalFilter modifyResponseFilter() {
return (exchange, chain) -> {
log.info("modifyResponseFilter start");
String value = (String) exchange.getAttribute(GATEWAY_HANDLER_MAPPER_ATTR).orElse("N/A");
exchange.getResponse().getHeaders().add(HANDLER_MAPPER_HEADER, value);
Route route = (Route) exchange.getAttribute(GATEWAY_ROUTE_ATTR).orElse(null);
if (route != null) {
exchange.getResponse().getHeaders().add(ROUTE_ID_HEADER, route.getId());
}
return chain.filter(exchange);
};
}
@Bean
@Order(-1)
public GlobalFilter postFilter() {

23
src/test/java/org/springframework/cloud/gateway/test/TestUtils.java

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
package org.springframework.cloud.gateway.test;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.ClientResponse;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Spencer Gibb
*/
public class TestUtils {
public static Map<String, Object> getMap(Map response, String key) {
assertThat(response).containsKey(key).isInstanceOf(Map.class);
return (Map<String, Object>) response.get(key);
}
public static void assertStatus(ClientResponse response, HttpStatus status) {
HttpStatus statusCode = response.statusCode();
assertThat(statusCode).isEqualTo(status);
}
}

16
src/test/resources/application-removenonproxyheaders.yml

@ -0,0 +1,16 @@ @@ -0,0 +1,16 @@
spring:
cloud:
gateway:
default-filters:
- RemoveNonProxyHeaders
routes:
# =====================================
- id: remove_request_header_test
uri: http://httpbin.org:80
predicates:
- Host=**.removerequestheader.org
- Url=/headers
filters:
- RemoveRequestHeader=X-Request-Foo
Loading…
Cancel
Save