Skip to content

Commit a03da51

Browse files
committed
Simplify test
1 parent d2dfdaa commit a03da51

File tree

1 file changed

+17
-53
lines changed

1 file changed

+17
-53
lines changed

dialogue-apache-hc5-client/src/test/java/com/palantir/dialogue/hc5/ApacheHttpClientBlockingChannelTest.java

+17-53
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,9 @@
2929
import java.net.InetAddress;
3030
import java.net.URI;
3131
import java.net.URL;
32-
import java.net.URLDecoder;
3332
import java.net.UnknownHostException;
34-
import java.nio.charset.StandardCharsets;
3533
import java.util.Comparator;
3634
import java.util.Map;
37-
import java.util.Objects;
38-
import javax.annotation.Nullable;
3935
import org.apache.hc.core5.http.ClassicHttpRequest;
4036
import org.apache.hc.core5.net.URIAuthority;
4137
import org.junit.jupiter.api.Test;
@@ -49,7 +45,7 @@ class ApacheHttpClientBlockingChannelTest {
4945
@ValueSource(strings = {"GET", "PUT", "POST"})
5046
void createRequest(String method) throws Exception {
5147

52-
BaseUrl baseUrl = BaseUrl.of(new URL("https://www.example.com/foo/api"));
48+
BaseUrl baseUrl = BaseUrl.of(new URL("https://www.example.local/foo/api"));
5349
Endpoint endpoint = new Endpoint() {
5450
private final PathTemplate pathTemplate = PathTemplate.builder()
5551
.fixed("fixed")
@@ -95,13 +91,13 @@ public void renderPath(ListMultimap<String, String> params, UrlBuilder url) {
9591
.isEqualTo(httpRequest.getRequestUri())
9692
.isEqualTo("/foo/api/fixed/baz/42?q=1&test=true");
9793
assertThat(httpRequest.getUri())
98-
.isEqualTo(URI.create("https://www.example.com/foo/api/fixed/baz/42?q=1&test=true"));
94+
.isEqualTo(URI.create("https://www.example.local/foo/api/fixed/baz/42?q=1&test=true"));
9995
}
10096

10197
@ParameterizedTest
10298
@CsvSource({
103-
"http://example.com, example.com, -1,",
104-
"https://example.com, example.com, -1,",
99+
"http://example.local, example.local, -1,",
100+
"https://example.local, example.local, -1,",
105101
"https://localhost:1234, localhost, 1234,",
106102
"https://127.0.0.1, 127.0.0.1, -1,",
107103
"https://[0:0:0:0:0:ffff:c0a8:0102], 0:0:0:0:0:ffff:c0a8:0102, -1,",
@@ -110,57 +106,40 @@ public void renderPath(ListMultimap<String, String> params, UrlBuilder url) {
110106
"https://[::ffff:c0a8:102], ::ffff:c0a8:102, -1,",
111107
"https://127.0.0.1:1234, 127.0.0.1, 1234,",
112108
"https://[::1]:1234, ::1, 1234,",
113-
"https://www.example.com, www.example.com, -1,",
114-
"https://www.example.com:443, www.example.com, 443,",
115-
"https://www.example.com/path/to/foo/bar, www.example.com, -1,",
116-
"https://www.example.com/path/to/foo/bar?baz=quux&hello=world#hash-octothorpe, www.example.com, -1,",
117-
"https://[email protected].com:8443/path/to/foo/bar?baz=quux&hello=world#hash-octothorpe ,"
118-
+ " www.example.com, 8443, user",
109+
"https://www.example.local, www.example.local, -1,",
110+
"https://www.example.local:443, www.example.local, 443,",
111+
"https://www.example.local/path/to/foo/bar, www.example.local, -1,",
112+
"https://www.example.local/path/to/foo/bar?baz=quux&hello=world#hash-octothorpe, www.example.local, -1,",
113+
"https://[email protected].local:8443/path/to/foo/bar?baz=quux&hello=world#hash-octothorpe ,"
114+
+ " www.example.local, 8443, user",
119115
"https://user@[::1]:8443/path/to/foo/bar?baz=quux&hello=world#hash-octothorpe , ::1, 8443, user",
120116
"https://user@[0000:0000:0000:0000:0000:ffff:c0a8:0102]:8443/path/to/foo/bar?baz=quux&hello=world#an-octothorpe"
121117
+ " , 0000:0000:0000:0000:0000:ffff:c0a8:0102, 8443, user",
122-
"https://user:slash%[email protected].com, www.example.com, -1, user:slash%2Fslash",
118+
"https://user:slash%[email protected].local, www.example.local, -1, user:slash%2Fslash",
123119
})
124120
void parseAuthority(String input, String expectedHost, int expectedPort, String expectedUserInfo) throws Exception {
125121
URL url = new URL(input);
126-
URI uri = URI.create(input);
127-
assertThat(url).isEqualTo(uri.toURL());
128-
assertThat(uri).isEqualTo(url.toURI());
129122
assertThat(ApacheHttpClientBlockingChannel.parseAuthority(url))
130-
.isEqualTo(ApacheHttpClientBlockingChannel.parseAuthority(uri.toURL()))
131-
.isEqualTo(ApacheHttpClientBlockingChannel.parseAuthority(new URL(uri.toString())))
132-
.isEqualTo(URIAuthority.create(uri.getRawAuthority()))
133123
.isEqualTo(URIAuthority.create(url.toURI().getRawAuthority()))
134124
.isEqualTo(URIAuthority.create(url.getAuthority()))
135125
.satisfies(authority -> {
136126
assertThat(authority.getHostName())
137127
.usingComparator(hostComparator)
138128
.isEqualTo(expectedHost)
139-
.isEqualTo(uri.getHost())
140129
.isEqualTo(url.getHost());
141-
assertThat(authority.getPort())
142-
.isEqualTo(expectedPort)
143-
.isEqualTo(uri.getPort())
144-
.isEqualTo(url.getPort());
130+
assertThat(authority.getPort()).isEqualTo(expectedPort).isEqualTo(url.getPort());
145131
assertThat(authority.getUserInfo())
146-
.usingComparator(userInfoComparator)
147132
.isEqualTo(expectedUserInfo)
148-
.isEqualTo(decode(uri.getUserInfo()))
149-
.isEqualTo(decode(url.getUserInfo()));
133+
.isEqualTo(url.getUserInfo());
150134
});
151135
}
152136

153-
@Nullable
154-
private static String decode(String userInfo) {
155-
return userInfo == null ? null : URLDecoder.decode(userInfo, StandardCharsets.UTF_8);
156-
}
157-
158137
@Test
159138
void testHostComparator() {
160-
assertThat("www.example.com")
139+
assertThat("www.example.local")
161140
.usingComparator(hostComparator)
162-
.isEqualTo("www.example.com")
163-
.isNotEqualTo("github.com");
141+
.isEqualTo("www.example.local")
142+
.isNotEqualTo("www.example.com");
164143
assertThat("127.0.0.1")
165144
.usingComparator(hostComparator)
166145
.isEqualTo("127.0.0.1")
@@ -180,21 +159,6 @@ void testHostComparator() {
180159
.isNotEqualTo("[::ffff:c0a8:101]");
181160
}
182161

183-
private static final Comparator<? super String> userInfoComparator = (info1, info2) -> {
184-
String u1 = decode(info1);
185-
String u2 = decode(info2);
186-
if (Objects.equals(u1, u2)) {
187-
return 0;
188-
}
189-
if (u1 == null) {
190-
return -1;
191-
}
192-
if (u2 == null) {
193-
return 1;
194-
}
195-
return u1.compareTo(u2);
196-
};
197-
198162
private static final Comparator<? super String> hostComparator = (host1, host2) -> {
199163
if (host1.equals(host2)) {
200164
return 0;
@@ -212,7 +176,7 @@ private static InetAddress tryGetAddress(String host) {
212176
try {
213177
return InetAddress.getByName(host);
214178
} catch (UnknownHostException e) {
215-
throw new RuntimeException(e);
179+
return null;
216180
}
217181
}
218182
}

0 commit comments

Comments
 (0)