15
15
*/
16
16
package com .ning .http .client .async ;
17
17
18
- import static java .nio .charset .StandardCharsets .*;
19
18
import static com .ning .http .util .DateUtils .millisTime ;
20
19
import static com .ning .http .util .MiscUtils .isNonEmpty ;
21
- import static org .testng .Assert .*;
20
+ import static java .nio .charset .StandardCharsets .ISO_8859_1 ;
21
+ import static org .testng .Assert .assertEquals ;
22
+ import static org .testng .Assert .assertNotNull ;
23
+ import static org .testng .Assert .assertNull ;
24
+ import static org .testng .Assert .assertTrue ;
25
+ import static org .testng .Assert .fail ;
22
26
23
27
import org .testng .Assert ;
24
28
import org .testng .annotations .Test ;
39
43
import com .ning .http .client .multipart .Part ;
40
44
import com .ning .http .client .multipart .StringPart ;
41
45
46
+ import javax .net .ssl .SSLException ;
47
+
42
48
import java .io .ByteArrayInputStream ;
43
49
import java .io .IOException ;
44
50
import java .net .ConnectException ;
@@ -486,7 +492,8 @@ public Response onCompleted(Response response) throws Exception {
486
492
@ Test (groups = { "standalone" , "default_provider" , "async" })
487
493
public void asyncDoPostBodyIsoTest () throws Throwable {
488
494
try (AsyncHttpClient client = getAsyncHttpClient (null )) {
489
- Response r = client .preparePost (getTargetUrl ()).addHeader ("X-ISO" , "true" ).setBody ("\u017D \u017D \u017D \u017D \u017D \u017D " ).execute ().get ();
495
+ Response r = client .preparePost (getTargetUrl ()).addHeader ("X-ISO" , "true" ).setBody ("\u017D \u017D \u017D \u017D \u017D \u017D " )
496
+ .execute ().get ();
490
497
assertEquals (r .getResponseBody ().getBytes (ISO_8859_1 ), "\u017D \u017D \u017D \u017D \u017D \u017D " .getBytes (ISO_8859_1 ));
491
498
}
492
499
}
@@ -641,7 +648,7 @@ public Response onCompleted(Response response) throws Exception {
641
648
}
642
649
643
650
protected abstract String generatedAcceptEncodingHeader ();
644
-
651
+
645
652
@ Test (groups = { "standalone" , "default_provider" , "async" })
646
653
public void asyncDoPostBasicGZIPTest () throws Throwable {
647
654
AsyncHttpClientConfig cf = new AsyncHttpClientConfig .Builder ().setCompressionEnforced (true ).build ();
@@ -694,16 +701,17 @@ public void asyncDoPostProxyTest() throws Throwable {
694
701
}
695
702
sb .setLength (sb .length () - 1 );
696
703
697
- Response response = client .preparePost (getTargetUrl ()).setHeaders (h ).setBody (sb .toString ()).execute (new AsyncCompletionHandler <Response >() {
698
- @ Override
699
- public Response onCompleted (Response response ) throws Exception {
700
- return response ;
701
- }
704
+ Response response = client .preparePost (getTargetUrl ()).setHeaders (h ).setBody (sb .toString ())
705
+ .execute (new AsyncCompletionHandler <Response >() {
706
+ @ Override
707
+ public Response onCompleted (Response response ) throws Exception {
708
+ return response ;
709
+ }
702
710
703
- @ Override
704
- public void onThrowable (Throwable t ) {
705
- }
706
- }).get ();
711
+ @ Override
712
+ public void onThrowable (Throwable t ) {
713
+ }
714
+ }).get ();
707
715
708
716
assertEquals (response .getStatusCode (), 200 );
709
717
assertEquals (response .getHeader ("X-Connection" ), "keep-alive" );
@@ -720,7 +728,8 @@ public void asyncRequestVirtualServerPOSTTest() throws Throwable {
720
728
for (int i = 0 ; i < 5 ; i ++) {
721
729
m .put ("param_" + i , Arrays .asList ("value_" + i ));
722
730
}
723
- Request request = new RequestBuilder ("POST" ).setUrl (getTargetUrl ()).setHeaders (h ).setFormParams (m ).setVirtualHost ("localhost:" + port1 ).build ();
731
+ Request request = new RequestBuilder ("POST" ).setUrl (getTargetUrl ()).setHeaders (h ).setFormParams (m )
732
+ .setVirtualHost ("localhost:" + port1 ).build ();
724
733
725
734
Response response = client .executeRequest (request , new AsyncCompletionHandlerAdapter ()).get ();
726
735
@@ -748,7 +757,8 @@ public void asyncDoPutTest() throws Throwable {
748
757
}
749
758
sb .setLength (sb .length () - 1 );
750
759
751
- Response response = client .preparePut (getTargetUrl ()).setHeaders (h ).setBody (sb .toString ()).execute (new AsyncCompletionHandlerAdapter ()).get ();
760
+ Response response = client .preparePut (getTargetUrl ()).setHeaders (h ).setBody (sb .toString ())
761
+ .execute (new AsyncCompletionHandlerAdapter ()).get ();
752
762
753
763
assertEquals (response .getStatusCode (), 200 );
754
764
}
@@ -803,11 +813,12 @@ public void asyncDoPostDelayCancelTest() throws Throwable {
803
813
StringBuilder sb = new StringBuilder ();
804
814
sb .append ("LockThread=true" );
805
815
806
- Future <Response > future = client .preparePost (getTargetUrl ()).setHeaders (h ).setBody (sb .toString ()).execute (new AsyncCompletionHandlerAdapter () {
807
- @ Override
808
- public void onThrowable (Throwable t ) {
809
- }
810
- });
816
+ Future <Response > future = client .preparePost (getTargetUrl ()).setHeaders (h ).setBody (sb .toString ())
817
+ .execute (new AsyncCompletionHandlerAdapter () {
818
+ @ Override
819
+ public void onThrowable (Throwable t ) {
820
+ }
821
+ });
811
822
812
823
// Make sure we are connected before cancelling. I know, Thread.sleep
813
824
// sucks!
@@ -827,12 +838,13 @@ public void asyncDoPostDelayBytesTest() throws Throwable {
827
838
sb .append ("LockThread=true" );
828
839
829
840
try {
830
- Future <Response > future = client .preparePost (getTargetUrl ()).setHeaders (h ).setBody (sb .toString ()).execute (new AsyncCompletionHandlerAdapter () {
831
- @ Override
832
- public void onThrowable (Throwable t ) {
833
- t .printStackTrace ();
834
- }
835
- });
841
+ Future <Response > future = client .preparePost (getTargetUrl ()).setHeaders (h ).setBody (sb .toString ())
842
+ .execute (new AsyncCompletionHandlerAdapter () {
843
+ @ Override
844
+ public void onThrowable (Throwable t ) {
845
+ t .printStackTrace ();
846
+ }
847
+ });
836
848
837
849
future .get (10 , TimeUnit .SECONDS );
838
850
} catch (ExecutionException ex ) {
@@ -862,7 +874,8 @@ public void asyncDoPostNullBytesTest() throws Throwable {
862
874
}
863
875
sb .setLength (sb .length () - 1 );
864
876
865
- Future <Response > future = client .preparePost (getTargetUrl ()).setHeaders (h ).setBody (sb .toString ()).execute (new AsyncCompletionHandlerAdapter ());
877
+ Future <Response > future = client .preparePost (getTargetUrl ()).setHeaders (h ).setBody (sb .toString ())
878
+ .execute (new AsyncCompletionHandlerAdapter ());
866
879
867
880
Response response = future .get ();
868
881
Assert .assertNotNull (response );
@@ -912,12 +925,13 @@ public void asyncConnectInvalidFuture() throws Throwable {
912
925
final AtomicInteger count = new AtomicInteger ();
913
926
for (int i = 0 ; i < 20 ; i ++) {
914
927
try {
915
- Response response = client .preparePost (String .format ("http://127.0.0.1:%d/" , dummyPort )).execute (new AsyncCompletionHandlerAdapter () {
916
- @ Override
917
- public void onThrowable (Throwable t ) {
918
- count .incrementAndGet ();
919
- }
920
- }).get ();
928
+ Response response = client .preparePost (String .format ("http://127.0.0.1:%d/" , dummyPort ))
929
+ .execute (new AsyncCompletionHandlerAdapter () {
930
+ @ Override
931
+ public void onThrowable (Throwable t ) {
932
+ count .incrementAndGet ();
933
+ }
934
+ }).get ();
921
935
assertNull (response , "Should have thrown ExecutionException" );
922
936
} catch (ExecutionException ex ) {
923
937
Throwable cause = ex .getCause ();
@@ -935,12 +949,13 @@ public void asyncConnectInvalidPortFuture() throws Throwable {
935
949
try (AsyncHttpClient client = getAsyncHttpClient (null )) {
936
950
int dummyPort = findFreePort ();
937
951
try {
938
- Response response = client .preparePost (String .format ("http://127.0.0.1:%d/" , dummyPort )).execute (new AsyncCompletionHandlerAdapter () {
939
- @ Override
940
- public void onThrowable (Throwable t ) {
941
- t .printStackTrace ();
942
- }
943
- }).get ();
952
+ Response response = client .preparePost (String .format ("http://127.0.0.1:%d/" , dummyPort ))
953
+ .execute (new AsyncCompletionHandlerAdapter () {
954
+ @ Override
955
+ public void onThrowable (Throwable t ) {
956
+ t .printStackTrace ();
957
+ }
958
+ }).get ();
944
959
assertNull (response , "Should have thrown ExecutionException" );
945
960
} catch (ExecutionException ex ) {
946
961
Throwable cause = ex .getCause ();
@@ -958,12 +973,13 @@ public void asyncConnectInvalidPort() throws Throwable {
958
973
int port = findFreePort ();
959
974
960
975
try {
961
- Response response = client .preparePost (String .format ("http://127.0.0.1:%d/" , port )).execute (new AsyncCompletionHandlerAdapter () {
962
- @ Override
963
- public void onThrowable (Throwable t ) {
964
- t .printStackTrace ();
965
- }
966
- }).get ();
976
+ Response response = client .preparePost (String .format ("http://127.0.0.1:%d/" , port ))
977
+ .execute (new AsyncCompletionHandlerAdapter () {
978
+ @ Override
979
+ public void onThrowable (Throwable t ) {
980
+ t .printStackTrace ();
981
+ }
982
+ }).get ();
967
983
assertNull (response , "No ExecutionException was thrown" );
968
984
} catch (ExecutionException ex ) {
969
985
assertEquals (ex .getCause ().getClass (), ConnectException .class );
@@ -994,7 +1010,8 @@ public void onThrowable(Throwable t) {
994
1010
}
995
1011
}
996
1012
997
- @ Test (groups = { "online" , "default_provider" , "async" }, expectedExceptions = { ConnectException .class , UnresolvedAddressException .class , UnknownHostException .class })
1013
+ @ Test (groups = { "online" , "default_provider" , "async" }, expectedExceptions = { ConnectException .class ,
1014
+ UnresolvedAddressException .class , UnknownHostException .class })
998
1015
public void asyncConnectInvalidHandlerHost () throws Throwable {
999
1016
try (AsyncHttpClient client = getAsyncHttpClient (null )) {
1000
1017
@@ -1027,15 +1044,16 @@ public void asyncConnectInvalidFuturePort() throws Throwable {
1027
1044
int port = findFreePort ();
1028
1045
1029
1046
try {
1030
- Response response = client .prepareGet (String .format ("http://127.0.0.1:%d/" , port )).execute (new AsyncCompletionHandlerAdapter () {
1031
- @ Override
1032
- public void onThrowable (Throwable t ) {
1033
- called .set (true );
1034
- if (t instanceof ConnectException ) {
1035
- rightCause .set (true );
1036
- }
1037
- }
1038
- }).get ();
1047
+ Response response = client .prepareGet (String .format ("http://127.0.0.1:%d/" , port ))
1048
+ .execute (new AsyncCompletionHandlerAdapter () {
1049
+ @ Override
1050
+ public void onThrowable (Throwable t ) {
1051
+ called .set (true );
1052
+ if (t instanceof ConnectException ) {
1053
+ rightCause .set (true );
1054
+ }
1055
+ }
1056
+ }).get ();
1039
1057
assertNull (response , "No ExecutionException was thrown" );
1040
1058
} catch (ExecutionException ex ) {
1041
1059
assertEquals (ex .getCause ().getClass (), ConnectException .class );
@@ -1383,7 +1401,8 @@ public void testAsyncHttpProviderConfig() throws Exception {
1383
1401
1384
1402
@ Test (groups = { "standalone" , "default_provider" })
1385
1403
public void idleRequestTimeoutTest () throws Exception {
1386
- try (AsyncHttpClient client = getAsyncHttpClient (new AsyncHttpClientConfig .Builder ().setPooledConnectionIdleTimeout (5000 ).setRequestTimeout (10000 ).build ())) {
1404
+ try (AsyncHttpClient client = getAsyncHttpClient (new AsyncHttpClientConfig .Builder ().setPooledConnectionIdleTimeout (5000 )
1405
+ .setRequestTimeout (10000 ).build ())) {
1387
1406
FluentCaseInsensitiveStringsMap h = new FluentCaseInsensitiveStringsMap ();
1388
1407
h .add ("Content-Type" , "application/x-www-form-urlencoded" );
1389
1408
h .add ("LockThread" , "true" );
@@ -1420,17 +1439,18 @@ public void asyncDoPostCancelTest() throws Throwable {
1420
1439
final AtomicReference <CancellationException > ex = new AtomicReference <>();
1421
1440
ex .set (null );
1422
1441
try {
1423
- Future <Response > future = client .preparePost (getTargetUrl ()).setHeaders (h ).setBody (sb .toString ()).execute (new AsyncCompletionHandlerAdapter () {
1442
+ Future <Response > future = client .preparePost (getTargetUrl ()).setHeaders (h ).setBody (sb .toString ())
1443
+ .execute (new AsyncCompletionHandlerAdapter () {
1424
1444
1425
- @ Override
1426
- public void onThrowable (Throwable t ) {
1427
- if (t instanceof CancellationException ) {
1428
- ex .set ((CancellationException ) t );
1429
- }
1430
- t .printStackTrace ();
1431
- }
1445
+ @ Override
1446
+ public void onThrowable (Throwable t ) {
1447
+ if (t instanceof CancellationException ) {
1448
+ ex .set ((CancellationException ) t );
1449
+ }
1450
+ t .printStackTrace ();
1451
+ }
1432
1452
1433
- });
1453
+ });
1434
1454
1435
1455
Thread .sleep (1000 );
1436
1456
future .cancel (true );
@@ -1444,7 +1464,7 @@ public void onThrowable(Throwable t) {
1444
1464
protected String getBrokenTargetUrl () {
1445
1465
return String .format ("http:127.0.0.1:%d/foo/test" , port1 );
1446
1466
}
1447
-
1467
+
1448
1468
@ Test (groups = { "standalone" , "default_provider" }, expectedExceptions = { NullPointerException .class })
1449
1469
public void invalidUri () throws Exception {
1450
1470
try (AsyncHttpClient client = getAsyncHttpClient (null )) {
@@ -1479,5 +1499,18 @@ public void mirrorByteTest() throws Throwable {
1479
1499
}
1480
1500
}
1481
1501
1502
+ @ Test
1503
+ public void requestingPlainHttpEndpointOverHttpsThrowsSslException () throws Throwable {
1504
+ try (AsyncHttpClient client = getAsyncHttpClient (null )) {
1505
+ client .prepareGet (getTargetUrl ().replace ("http" , "https" )).execute ().get ();
1506
+ fail ("Request shouldn't succeed" );
1507
+ } catch (ExecutionException e ) {
1508
+ Throwable cause = e .getCause ();
1509
+ assertTrue (cause instanceof ConnectException , "Cause should be a ConnectException but got a " + cause .getClass ().getName ());
1510
+ Throwable rootCause = e .getCause ().getCause ();
1511
+ assertTrue (rootCause instanceof SSLException , "Root cause should be a SslException but got a " + rootCause .getClass ().getName ());
1512
+ }
1513
+ }
1514
+
1482
1515
protected abstract AsyncHttpProviderConfig <?, ?> getProviderConfig ();
1483
1516
}
0 commit comments