Skip to content

Commit

Permalink
Simplify test
Browse files Browse the repository at this point in the history
  • Loading branch information
schlosna committed Nov 30, 2024
1 parent d2dfdaa commit c637a13
Showing 1 changed file with 5 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@
import java.net.InetAddress;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Comparator;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.net.URIAuthority;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -120,41 +116,26 @@ public void renderPath(ListMultimap<String, String> params, UrlBuilder url) {
"https://user@[0000:0000:0000:0000:0000:ffff:c0a8:0102]:8443/path/to/foo/bar?baz=quux&hello=world#an-octothorpe"
+ " , 0000:0000:0000:0000:0000:ffff:c0a8:0102, 8443, user",
"https://user:slash%2Fslash@www.example.com, www.example.com, -1, user:slash%2Fslash",
"https://schloßnagle.example.com, xn--schlonagle-93a.example.com, -1,",
"https://xn--schlonagle-93a.example.com, schloßnagle.example.com, -1,",
})
void parseAuthority(String input, String expectedHost, int expectedPort, String expectedUserInfo) throws Exception {
URL url = new URL(input);
URI uri = URI.create(input);
assertThat(url).isEqualTo(uri.toURL());
assertThat(uri).isEqualTo(url.toURI());
assertThat(ApacheHttpClientBlockingChannel.parseAuthority(url))
.isEqualTo(ApacheHttpClientBlockingChannel.parseAuthority(uri.toURL()))
.isEqualTo(ApacheHttpClientBlockingChannel.parseAuthority(new URL(uri.toString())))
.isEqualTo(URIAuthority.create(uri.getRawAuthority()))
.isEqualTo(URIAuthority.create(url.toURI().getRawAuthority()))
.isEqualTo(URIAuthority.create(url.getAuthority()))
.satisfies(authority -> {
assertThat(authority.getHostName())
.usingComparator(hostComparator)
.isEqualTo(expectedHost)
.isEqualTo(uri.getHost())
.isEqualTo(url.getHost());
assertThat(authority.getPort())
.isEqualTo(expectedPort)
.isEqualTo(uri.getPort())
.isEqualTo(url.getPort());
assertThat(authority.getPort()).isEqualTo(expectedPort).isEqualTo(url.getPort());
assertThat(authority.getUserInfo())
.usingComparator(userInfoComparator)
.isEqualTo(expectedUserInfo)
.isEqualTo(decode(uri.getUserInfo()))
.isEqualTo(decode(url.getUserInfo()));
.isEqualTo(url.getUserInfo());
});
}

@Nullable
private static String decode(String userInfo) {
return userInfo == null ? null : URLDecoder.decode(userInfo, StandardCharsets.UTF_8);
}

@Test
void testHostComparator() {
assertThat("www.example.com")
Expand All @@ -180,21 +161,6 @@ void testHostComparator() {
.isNotEqualTo("[::ffff:c0a8:101]");
}

private static final Comparator<? super String> userInfoComparator = (info1, info2) -> {
String u1 = decode(info1);
String u2 = decode(info2);
if (Objects.equals(u1, u2)) {
return 0;
}
if (u1 == null) {
return -1;
}
if (u2 == null) {
return 1;
}
return u1.compareTo(u2);
};

private static final Comparator<? super String> hostComparator = (host1, host2) -> {
if (host1.equals(host2)) {
return 0;
Expand All @@ -212,7 +178,7 @@ private static InetAddress tryGetAddress(String host) {
try {
return InetAddress.getByName(host);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
return null;
}
}
}

0 comments on commit c637a13

Please sign in to comment.