Polish Forwarded header support

This commit is contained in:
Rossen Stoyanchev
2018-05-23 21:20:37 -04:00
parent 1e4a3a2370
commit 72022899de
4 changed files with 102 additions and 111 deletions

View File

@@ -103,6 +103,17 @@ public class ForwardedHeaderFilterTests {
assertEquals("/prefix", actual);
}
private String filterAndGetContextPath() throws ServletException, IOException {
return filterAndGetWrappedRequest().getContextPath();
}
private HttpServletRequest filterAndGetWrappedRequest() throws ServletException, IOException {
MockHttpServletResponse response = new MockHttpServletResponse();
this.filter.doFilterInternal(this.request, response, this.filterChain);
return (HttpServletRequest) this.filterChain.getRequest();
}
@Test
public void contextPathPreserveEncoding() throws Exception {
this.request.setContextPath("/app%20");
@@ -183,8 +194,8 @@ public class ForwardedHeaderFilterTests {
@Test
public void caseInsensitiveForwardedPrefix() throws Exception {
this.request = new MockHttpServletRequest() {
// Make it case-sensitive (SPR-14372)
@Override
@Override // SPR-14372: make it case-sensitive
public String getHeader(String header) {
Enumeration<String> names = getHeaderNames();
while (names.hasMoreElements()) {
@@ -204,15 +215,21 @@ public class ForwardedHeaderFilterTests {
}
@Test
public void shouldFilter() throws Exception {
public void shouldFilter() {
testShouldFilter("Forwarded");
testShouldFilter(X_FORWARDED_HOST);
testShouldFilter(X_FORWARDED_PORT);
testShouldFilter(X_FORWARDED_PROTO);
}
private void testShouldFilter(String headerName) {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader(headerName, "1");
assertFalse(this.filter.shouldNotFilter(request));
}
@Test
public void shouldNotFilter() throws Exception {
public void shouldNotFilter() {
assertTrue(this.filter.shouldNotFilter(new MockHttpServletRequest()));
}
@@ -417,7 +434,6 @@ public class ForwardedHeaderFilterTests {
this.request.addHeader(X_FORWARDED_HOST, "example.com");
this.request.addHeader(X_FORWARDED_PORT, "443");
this.filter.setRelativeRedirects(true);
String location = sendRedirect("/a");
assertEquals("/a", location);
@@ -426,7 +442,6 @@ public class ForwardedHeaderFilterTests {
@Test
public void sendRedirectWhenRequestOnlyAndNoXForwardedThenUsesRelativeRedirects() throws Exception {
this.filter.setRelativeRedirects(true);
String location = sendRedirect("/a");
assertEquals("/a", location);
@@ -441,34 +456,12 @@ public class ForwardedHeaderFilterTests {
res.sendRedirect(location);
}
};
MockHttpServletResponse response = doWithFiltersAndGetResponse(this.filter, filter);
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain filterChain = new MockFilterChain(new HttpServlet() {}, this.filter, filter);
filterChain.doFilter(request, response);
return response.getRedirectedUrl();
}
@SuppressWarnings("serial")
private MockHttpServletResponse doWithFiltersAndGetResponse(Filter... filters)
throws ServletException, IOException {
MockHttpServletResponse response = new MockHttpServletResponse();
FilterChain filterChain = new MockFilterChain(new HttpServlet() {}, filters);
filterChain.doFilter(request, response);
return response;
}
private String filterAndGetContextPath() throws ServletException, IOException {
return filterAndGetWrappedRequest().getContextPath();
}
private HttpServletRequest filterAndGetWrappedRequest() throws ServletException, IOException {
MockHttpServletResponse response = new MockHttpServletResponse();
this.filter.doFilterInternal(this.request, response, this.filterChain);
return (HttpServletRequest) this.filterChain.getRequest();
}
private void testShouldFilter(String headerName) throws ServletException {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader(headerName, "1");
assertFalse(this.filter.shouldNotFilter(request));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,18 +24,21 @@ import reactor.core.publisher.Mono;
import org.springframework.http.HttpHeaders;
import org.springframework.lang.Nullable;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilterChain;
import static org.junit.Assert.*;
import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.*;
/**
* @author Arjen Poutsma
*/
public class ForwardedHeaderFilterTests {
private static final String BASE_URL = "http://example.com/path";
private final ForwardedHeaderFilter filter = new ForwardedHeaderFilter();
private final TestWebFilterChain filterChain = new TestWebFilterChain();
@@ -43,8 +46,7 @@ public class ForwardedHeaderFilterTests {
@Test
public void removeOnly() {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest
.get("/")
ServerWebExchange exchange = MockServerWebExchange.from(get(BASE_URL)
.header("Forwarded", "for=192.0.2.60;proto=http;by=203.0.113.43")
.header("X-Forwarded-Host", "example.com")
.header("X-Forwarded-Port", "8080")
@@ -65,66 +67,57 @@ public class ForwardedHeaderFilterTests {
@Test
public void xForwardedRequest() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest
.get("http://example.com/path")
ServerWebExchange exchange = MockServerWebExchange.from(get(BASE_URL)
.header("X-Forwarded-Host", "84.198.58.199")
.header("X-Forwarded-Port", "443")
.header("X-Forwarded-Proto", "https"));
this.filter.filter(exchange, this.filterChain).block(Duration.ZERO);
URI uri = this.filterChain.uri;
assertEquals(new URI("https://84.198.58.199/path"), uri);
assertEquals(new URI("https://84.198.58.199/path"), filterAndGetUri(exchange));
}
@Test
public void forwardedRequest() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest
.get("http://example.com/path")
ServerWebExchange exchange = MockServerWebExchange.from(get(BASE_URL)
.header("Forwarded", "host=84.198.58.199;proto=https"));
this.filter.filter(exchange, this.filterChain).block(Duration.ZERO);
URI uri = this.filterChain.uri;
assertEquals(new URI("https://84.198.58.199/path"), uri);
assertEquals(new URI("https://84.198.58.199/path"), filterAndGetUri(exchange));
}
@Test
public void requestUriWithForwardedPrefix() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest
.get("http://example.com/path")
ServerWebExchange exchange = MockServerWebExchange.from(get(BASE_URL)
.header("X-Forwarded-Prefix", "/prefix"));
this.filter.filter(exchange, this.filterChain).block(Duration.ZERO);
URI uri = this.filterChain.uri;
assertEquals(new URI("http://example.com/prefix/path"), uri);
assertEquals(new URI("http://example.com/prefix/path"), filterAndGetUri(exchange));
}
@Test
public void requestUriWithForwardedPrefixTrailingSlash() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest
.get("http://example.com/path")
ServerWebExchange exchange = MockServerWebExchange.from(get(BASE_URL)
.header("X-Forwarded-Prefix", "/prefix/"));
this.filter.filter(exchange, this.filterChain).block(Duration.ZERO);
assertEquals(new URI("http://example.com/prefix/path"), filterAndGetUri(exchange));
}
URI uri = this.filterChain.uri;
assertEquals(new URI("http://example.com/prefix/path"), uri);
@Nullable
private URI filterAndGetUri(ServerWebExchange exchange) {
this.filter.filter(exchange, this.filterChain).block(Duration.ZERO);
return this.filterChain.uri;
}
private static class TestWebFilterChain implements WebFilterChain {
@Nullable
private HttpHeaders httpHeaders;
private HttpHeaders headers;
@Nullable
private URI uri;
@Nullable
public HttpHeaders getHeaders() {
return this.httpHeaders;
return this.headers;
}
@Nullable
@@ -134,12 +127,10 @@ public class ForwardedHeaderFilterTests {
@Override
public Mono<Void> filter(ServerWebExchange exchange) {
this.httpHeaders = exchange.getRequest().getHeaders();
this.headers = exchange.getRequest().getHeaders();
this.uri = exchange.getRequest().getURI();
return Mono.empty();
}
}
}