use of org.springframework.cloud.gateway.filter.headers.ForwardedHeadersFilter.Forwarded in project spring-cloud-gateway by spring-cloud.
the class ForwardedHeadersFilterTests method forwardedHeaderDoesNotExist.
@Test
public void forwardedHeaderDoesNotExist() throws UnknownHostException {
MockServerHttpRequest request = MockServerHttpRequest.get("http://localhost/get").remoteAddress(new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 80)).header(HttpHeaders.HOST, "myhost").build();
ForwardedHeadersFilter filter = new ForwardedHeadersFilter();
HttpHeaders headers = filter.filter(request.getHeaders(), MockServerWebExchange.from(request));
assertThat(headers.get(FORWARDED_HEADER)).hasSize(1);
List<Forwarded> forwardeds = ForwardedHeadersFilter.parse(headers.get(FORWARDED_HEADER));
assertThat(forwardeds).hasSize(1);
Forwarded forwarded = forwardeds.get(0);
assertThat(forwarded.getValues()).containsEntry("host", "myhost").containsEntry("proto", "http").containsEntry("for", "\"10.0.0.1:80\"");
}
use of org.springframework.cloud.gateway.filter.headers.ForwardedHeadersFilter.Forwarded in project spring-cloud-gateway by spring-cloud.
the class ForwardedHeadersFilterTests method forwardedParsedCorrectly.
@Test
public void forwardedParsedCorrectly() {
String[] valid = new String[] { "for=\"_gazonk\"", "for=192.0.2.60;proto=http;by=203.0.113.43", "for=192.0.2.43, for=198.51.100.17", "for=12.34.56.78;host=example.com;proto=https, for=23.45.67.89", "for=12.34.56.78, for=23.45.67.89;secret=egah2CGj55fSJFs, for=10.1.2.3", "For=\"[2001:db8:cafe::17]:4711\"" };
@SuppressWarnings("unchecked") List<List<Map<String, String>>> expectedFor = new ArrayList<List<Map<String, String>>>() {
{
add(list(map("for", "\"_gazonk\"")));
add(list(map("for", "192.0.2.60", "proto", "http", "by", "203.0.113.43")));
add(list(map("for", "192.0.2.43"), map("for", "198.51.100.17")));
add(list(map("for", "12.34.56.78", "host", "example.com", "proto", "https"), map("for", "23.45.67.89")));
add(list(map("for", "12.34.56.78"), map("for", "23.45.67.89", "secret", "egah2CGj55fSJFs"), map("for", "10.1.2.3")));
add(list(map("for", "\"[2001:db8:cafe::17]:4711\"")));
}
};
for (int i = 0; i < valid.length; i++) {
String value = valid[i];
// simulate spring's parsed headers
String[] values = StringUtils.tokenizeToStringArray(value, ",");
List<Forwarded> results = ForwardedHeadersFilter.parse(Arrays.asList(values));
// System.out.println("results: "+results);
assertThat(results).hasSize(values.length);
assertThat(results.get(0)).isNotNull();
List<Map<String, String>> expected = expectedFor.get(i);
// System.out.println("expected: "+Arrays.asList(expected));
assertThat(expected).hasSize(results.size());
for (int j = 0; j < results.size(); j++) {
Forwarded forwarded = results.get(j);
assertThat(forwarded.getValues()).hasSize(expected.get(j).size()).containsAllEntriesOf(expected.get(j));
}
}
}
Aggregations