41
41
import java .security .cert .CertificateException ;
42
42
import java .security .cert .X509Certificate ;
43
43
import java .util .Enumeration ;
44
+ import java .util .concurrent .ExecutionException ;
44
45
import java .util .concurrent .Future ;
45
46
import java .util .concurrent .atomic .AtomicBoolean ;
46
47
47
48
import static org .testng .Assert .assertEquals ;
48
49
import static org .testng .Assert .assertNotNull ;
50
+ import static org .testng .Assert .fail ;
49
51
50
52
public abstract class HostnameVerifierTest extends AbstractBasicTest {
51
53
@@ -219,9 +221,10 @@ public void negativeHostnameVerifierTest() throws Throwable {
219
221
File file = new File (url .toURI ());
220
222
221
223
try {
222
- Future <Response > f = client .preparePost (getTargetUrl ()).setBody (file ).setHeader ("Content-Type" , "text/html" ).execute ();
223
- } catch (ConnectException ex ) {
224
- assertEquals (ConnectException .class , ex .getClass ());
224
+ client .preparePost (getTargetUrl ()).setBody (file ).setHeader ("Content-Type" , "text/html" ).execute ().get ();
225
+ fail ("ConnectException expected" );
226
+ } catch (ExecutionException ex ) {
227
+ assertEquals (ex .getCause ().getClass (), ConnectException .class );
225
228
}
226
229
}
227
230
@@ -236,15 +239,16 @@ public void remoteIDHostnameVerifierTest() throws Throwable {
236
239
File file = new File (url .toURI ());
237
240
238
241
try {
239
- Future <Response > f = client .preparePost (getTargetUrl ()).setBody (file ).setHeader ("Content-Type" , "text/html" ).execute ();
240
- } catch (ConnectException ex ) {
241
- assertEquals (ConnectException .class , ex .getClass ());
242
+ client .preparePost (getTargetUrl ()).setBody (file ).setHeader ("Content-Type" , "text/html" ).execute ().get ();
243
+ fail ("ConnectException expected" );
244
+ } catch (ExecutionException ex ) {
245
+ assertEquals (ex .getCause ().getClass (), ConnectException .class );
242
246
}
243
247
}
244
248
245
249
@ Test (groups = {"standalone" , "default_provider" })
246
- public void remotePosHostnameVerifierTest () throws Throwable {
247
-
250
+ public void remoteNegHostnameVerifierTest () throws Throwable {
251
+ // request is made to 127.0.0.1, but cert presented for localhost - this should fail
248
252
final AsyncHttpClient client = getAsyncHttpClient (new Builder ().setHostnameVerifier (new CheckHost ("localhost" )).setSSLContext (createSSLContext ()).build ());
249
253
250
254
ClassLoader cl = getClass ().getClassLoader ();
@@ -253,11 +257,28 @@ public void remotePosHostnameVerifierTest() throws Throwable {
253
257
File file = new File (url .toURI ());
254
258
255
259
try {
256
- Future <Response > f = client .preparePost (getTargetUrl ()).setBody (file ).setHeader ("Content-Type" , "text/html" ).execute ();
257
- } catch (ConnectException ex ) {
258
- assertEquals (ConnectException .class , ex .getClass ());
260
+ client .preparePost (getTargetUrl ()).setBody (file ).setHeader ("Content-Type" , "text/html" ).execute ().get ();
261
+ fail ("ConnectException expected" );
262
+ } catch (ExecutionException ex ) {
263
+ assertEquals (ex .getCause ().getClass (), ConnectException .class );
259
264
}
260
265
}
266
+
267
+ @ Test (groups = {"standalone" , "default_provider" })
268
+ public void remotePosHostnameVerifierTest () throws Throwable {
269
+
270
+ final AsyncHttpClient client = getAsyncHttpClient (new Builder ().setHostnameVerifier (new CheckHost ("127.0.0.1" )).setSSLContext (createSSLContext ()).build ());
271
+
272
+ ClassLoader cl = getClass ().getClassLoader ();
273
+ // override system properties
274
+ URL url = cl .getResource ("SimpleTextFile.txt" );
275
+ File file = new File (url .toURI ());
276
+
277
+ Response resp = client .preparePost (getTargetUrl ()).setBody (file ).setHeader ("Content-Type" , "text/html" ).execute ().get ();
278
+ assertNotNull (resp );
279
+ assertEquals (resp .getStatusCode (), HttpServletResponse .SC_OK );
280
+ assertEquals (resp .getResponseBody (), "This is a simple test file" );
281
+ }
261
282
262
283
public static class PositiveHostVerifier implements HostnameVerifier {
263
284
@@ -269,7 +290,7 @@ public boolean verify(String s, SSLSession sslSession) {
269
290
public static class NegativeHostVerifier implements HostnameVerifier {
270
291
271
292
public boolean verify (String s , SSLSession sslSession ) {
272
- return true ;
293
+ return false ;
273
294
}
274
295
}
275
296
@@ -334,6 +355,4 @@ public void checkServerTrusted(
334
355
}
335
356
}
336
357
};
337
-
338
-
339
358
}
0 commit comments