29
29
import java .net .InetAddress ;
30
30
import java .net .URI ;
31
31
import java .net .URL ;
32
- import java .net .URLDecoder ;
33
32
import java .net .UnknownHostException ;
34
- import java .nio .charset .StandardCharsets ;
35
33
import java .util .Comparator ;
36
34
import java .util .Map ;
37
- import java .util .Objects ;
38
- import javax .annotation .Nullable ;
39
35
import org .apache .hc .core5 .http .ClassicHttpRequest ;
40
36
import org .apache .hc .core5 .net .URIAuthority ;
41
37
import org .junit .jupiter .api .Test ;
@@ -49,7 +45,7 @@ class ApacheHttpClientBlockingChannelTest {
49
45
@ ValueSource (strings = {"GET" , "PUT" , "POST" })
50
46
void createRequest (String method ) throws Exception {
51
47
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" ));
53
49
Endpoint endpoint = new Endpoint () {
54
50
private final PathTemplate pathTemplate = PathTemplate .builder ()
55
51
.fixed ("fixed" )
@@ -95,13 +91,13 @@ public void renderPath(ListMultimap<String, String> params, UrlBuilder url) {
95
91
.isEqualTo (httpRequest .getRequestUri ())
96
92
.isEqualTo ("/foo/api/fixed/baz/42?q=1&test=true" );
97
93
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" ));
99
95
}
100
96
101
97
@ ParameterizedTest
102
98
@ 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," ,
105
101
"https://localhost:1234, localhost, 1234," ,
106
102
"https://127.0.0.1, 127.0.0.1, -1," ,
107
103
"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) {
110
106
"https://[::ffff:c0a8:102], ::ffff:c0a8:102, -1," ,
111
107
"https://127.0.0.1:1234, 127.0.0.1, 1234," ,
112
108
"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" ,
119
115
"https://user@[::1]:8443/path/to/foo/bar?baz=quux&hello=world#hash-octothorpe , ::1, 8443, user" ,
120
116
"https://user@[0000:0000:0000:0000:0000:ffff:c0a8:0102]:8443/path/to/foo/bar?baz=quux&hello=world#an-octothorpe"
121
117
+ " , 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" ,
123
119
})
124
120
void parseAuthority (String input , String expectedHost , int expectedPort , String expectedUserInfo ) throws Exception {
125
121
URL url = new URL (input );
126
- URI uri = URI .create (input );
127
- assertThat (url ).isEqualTo (uri .toURL ());
128
- assertThat (uri ).isEqualTo (url .toURI ());
129
122
assertThat (ApacheHttpClientBlockingChannel .parseAuthority (url ))
130
- .isEqualTo (ApacheHttpClientBlockingChannel .parseAuthority (uri .toURL ()))
131
- .isEqualTo (ApacheHttpClientBlockingChannel .parseAuthority (new URL (uri .toString ())))
132
- .isEqualTo (URIAuthority .create (uri .getRawAuthority ()))
133
123
.isEqualTo (URIAuthority .create (url .toURI ().getRawAuthority ()))
134
124
.isEqualTo (URIAuthority .create (url .getAuthority ()))
135
125
.satisfies (authority -> {
136
126
assertThat (authority .getHostName ())
137
127
.usingComparator (hostComparator )
138
128
.isEqualTo (expectedHost )
139
- .isEqualTo (uri .getHost ())
140
129
.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 ());
145
131
assertThat (authority .getUserInfo ())
146
- .usingComparator (userInfoComparator )
147
132
.isEqualTo (expectedUserInfo )
148
- .isEqualTo (decode (uri .getUserInfo ()))
149
- .isEqualTo (decode (url .getUserInfo ()));
133
+ .isEqualTo (url .getUserInfo ());
150
134
});
151
135
}
152
136
153
- @ Nullable
154
- private static String decode (String userInfo ) {
155
- return userInfo == null ? null : URLDecoder .decode (userInfo , StandardCharsets .UTF_8 );
156
- }
157
-
158
137
@ Test
159
138
void testHostComparator () {
160
- assertThat ("www.example.com " )
139
+ assertThat ("www.example.local " )
161
140
.usingComparator (hostComparator )
162
- .isEqualTo ("www.example.com " )
163
- .isNotEqualTo ("github .com" );
141
+ .isEqualTo ("www.example.local " )
142
+ .isNotEqualTo ("www.example .com" );
164
143
assertThat ("127.0.0.1" )
165
144
.usingComparator (hostComparator )
166
145
.isEqualTo ("127.0.0.1" )
@@ -180,21 +159,6 @@ void testHostComparator() {
180
159
.isNotEqualTo ("[::ffff:c0a8:101]" );
181
160
}
182
161
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
-
198
162
private static final Comparator <? super String > hostComparator = (host1 , host2 ) -> {
199
163
if (host1 .equals (host2 )) {
200
164
return 0 ;
@@ -212,7 +176,7 @@ private static InetAddress tryGetAddress(String host) {
212
176
try {
213
177
return InetAddress .getByName (host );
214
178
} catch (UnknownHostException e ) {
215
- throw new RuntimeException ( e ) ;
179
+ return null ;
216
180
}
217
181
}
218
182
}
0 commit comments