Browse Source

Tests for null if no remote address.

fixes gh-430
pull/437/head
Spencer Gibb 6 years ago
parent
commit
8077b3d62b
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 4
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/headers/XForwardedHeadersFilter.java
  2. 19
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/headers/XForwardedHeadersFilterTests.java

4
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/headers/XForwardedHeadersFilter.java

@ -172,7 +172,9 @@ public class XForwardedHeadersFilter implements HttpHeadersFilter, Ordered { @@ -172,7 +172,9 @@ public class XForwardedHeadersFilter implements HttpHeadersFilter, Ordered {
original.entrySet().stream()
.forEach(entry -> updated.addAll(entry.getKey(), entry.getValue()));
if (isForEnabled()) {
if (isForEnabled() &&
request.getRemoteAddress() != null && request.getRemoteAddress().getAddress() != null)
{
String remoteAddr = request.getRemoteAddress().getAddress().getHostAddress();
List<String> xforwarded = original.get(X_FORWARDED_FOR_HEADER);
// prevent duplicates

19
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/headers/XForwardedHeadersFilterTests.java

@ -37,6 +37,25 @@ import static org.springframework.cloud.gateway.filter.headers.XForwardedHeaders @@ -37,6 +37,25 @@ import static org.springframework.cloud.gateway.filter.headers.XForwardedHeaders
*/
public class XForwardedHeadersFilterTests {
@Test
public void remoteAddressIsNull() throws Exception {
MockServerHttpRequest request = MockServerHttpRequest
.get("http://localhost:8080/get")
.header(HttpHeaders.HOST, "myhost")
.build();
XForwardedHeadersFilter filter = new XForwardedHeadersFilter();
HttpHeaders headers = filter.filter(request.getHeaders(), MockServerWebExchange.from(request));
assertThat(headers).containsKeys(X_FORWARDED_HOST_HEADER,
X_FORWARDED_PORT_HEADER, X_FORWARDED_PROTO_HEADER);
assertThat(headers.getFirst(X_FORWARDED_HOST_HEADER)).isEqualTo("localhost:8080");
assertThat(headers.getFirst(X_FORWARDED_PORT_HEADER)).isEqualTo("8080");
assertThat(headers.getFirst(X_FORWARDED_PROTO_HEADER)).isEqualTo("http");
}
@Test
public void xForwardedHeadersDoNotExist() throws Exception {
MockServerHttpRequest request = MockServerHttpRequest

Loading…
Cancel
Save