Skip to content

Commit

Permalink
Add tests for forwarded Header parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneBab committed Oct 14, 2023
1 parent ca7cb3e commit 6d51184
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package freenet.clients.http.utils;

import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.Map;

import org.hamcrest.Matchers;
import org.junit.Test;

import freenet.config.Config;
Expand Down Expand Up @@ -273,6 +278,60 @@ public void disallowedUriWithAllowedHostButDisallowedPortIsIgnored()
"http://127.0.0.1:8888");
}

@Test
public void forwardedHeaderProvidesIp() {
String forwarded = "for=192.0.2.172";
Map<String, String> parsedHeader = UriFilterProxyHeaderParser.parseForwardedHeader(
forwarded);
assertThat(parsedHeader.keySet(), Matchers.containsInAnyOrder("for"));;
assertThat(parsedHeader.values(), Matchers.containsInAnyOrder("192.0.2.172"));
}

@Test
public void forwardedHeaderProvidesIpv6AndPort() {
String forwarded = "for=[2001:db8:cafe::17]:4711";
Map<String, String> parsedHeader = UriFilterProxyHeaderParser.parseForwardedHeader(
forwarded);
assertThat(parsedHeader.keySet(), Matchers.containsInAnyOrder("for"));;
assertThat(parsedHeader.values(), Matchers.containsInAnyOrder("[2001:db8:cafe::17]:4711"));
}

@Test
public void forwardedHeaderProvidesIpCaseInsensitive() {
String forwarded = "For=192.0.2.172";
Map<String, String> parsedHeader = UriFilterProxyHeaderParser.parseForwardedHeader(
forwarded);
assertThat(parsedHeader.keySet(), Matchers.containsInAnyOrder("for"));;
assertThat(parsedHeader.values(), Matchers.containsInAnyOrder("192.0.2.172"));
}

@Test
public void forwardedHeaderProvidesIpProtoBy() {
String forwarded = "for=192.0.2.60;proto=http;by=203.0.113.43";
Map<String, String> parsedHeader = UriFilterProxyHeaderParser.parseForwardedHeader(
forwarded);
assertThat(parsedHeader.keySet(), Matchers.containsInAnyOrder("for", "proto", "by"));;
assertThat(parsedHeader.values(), Matchers.containsInAnyOrder("192.0.2.60", "http", "203.0.113.43"));
}

@Test
public void forwardedHeaderProvidesIpProtoByOnlyAllowTheFirst() {
String forwarded = "for=192.0.2.60;proto=http;by=203.0.113.43, for=198.51.100.17;proto=http;by=203.0.113.43";
Map<String, String> parsedHeader = UriFilterProxyHeaderParser.parseForwardedHeader(
forwarded);
assertThat(parsedHeader.keySet(), Matchers.containsInAnyOrder("for", "proto", "by"));;
assertThat(parsedHeader.values(), Matchers.containsInAnyOrder("192.0.2.60", "http", "203.0.113.43"));
}

@Test
public void forwardedHeaderProvidesIpOnlyAllowTheFirst() {
String forwarded = "for=192.0.2.60, for=198.51.100.17;proto=http;by=203.0.113.43";
Map<String, String> parsedHeader = UriFilterProxyHeaderParser.parseForwardedHeader(
forwarded);
assertThat(parsedHeader.keySet(), Matchers.containsInAnyOrder("for"));;
assertThat(parsedHeader.values(), Matchers.containsInAnyOrder("192.0.2.60"));
}

private void testUriPrefixMatchesExpected(
String fProxyPort,
String fProxyBindTo,
Expand Down

0 comments on commit 6d51184

Please sign in to comment.