Spencer Gibb
8 years ago
22 changed files with 1008 additions and 416 deletions
@ -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 { } |
||||
|
||||
} |
@ -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 { } |
||||
|
||||
} |
@ -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 { } |
||||
|
||||
} |
@ -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 { } |
||||
|
||||
} |
@ -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 { } |
||||
|
||||
} |
@ -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 { } |
||||
|
||||
} |
@ -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 { } |
||||
|
||||
} |
@ -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 { } |
||||
|
||||
} |
@ -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 { } |
||||
|
||||
} |
@ -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 { } |
||||
|
||||
} |
@ -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 { } |
||||
|
||||
} |
@ -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 { } |
||||
|
||||
} |
@ -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 { } |
||||
|
||||
} |
@ -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 { } |
||||
|
||||
} |
@ -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 { |
||||
} |
@ -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); |
||||
}; |
||||
} |
||||
} |
||||
|
||||
} |
@ -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 { } |
||||
|
||||
} |
@ -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); |
||||
} |
||||
} |
@ -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…
Reference in new issue