Search in sources :

Example 1 with Forwarded

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\"");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) InetSocketAddress(java.net.InetSocketAddress) Forwarded(org.springframework.cloud.gateway.filter.headers.ForwardedHeadersFilter.Forwarded) Test(org.junit.Test)

Example 2 with Forwarded

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));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Forwarded(org.springframework.cloud.gateway.filter.headers.ForwardedHeadersFilter.Forwarded) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 Forwarded (org.springframework.cloud.gateway.filter.headers.ForwardedHeadersFilter.Forwarded)2 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 HttpHeaders (org.springframework.http.HttpHeaders)1 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)1