1515 */
1616package com .ning .http .client .async ;
1717
18- import static java .nio .charset .StandardCharsets .*;
1918import static com .ning .http .util .DateUtils .millisTime ;
2019import 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 ;
2226
2327import org .testng .Assert ;
2428import org .testng .annotations .Test ;
3943import com .ning .http .client .multipart .Part ;
4044import com .ning .http .client .multipart .StringPart ;
4145
46+ import javax .net .ssl .SSLException ;
47+
4248import java .io .ByteArrayInputStream ;
4349import java .io .IOException ;
4450import java .net .ConnectException ;
@@ -486,7 +492,8 @@ public Response onCompleted(Response response) throws Exception {
486492@ Test (groups = {"standalone" ,"default_provider" ,"async" })
487493public void asyncDoPostBodyIsoTest ()throws Throwable {
488494try (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 ();
490497assertEquals (r .getResponseBody ().getBytes (ISO_8859_1 ),"\u017D \u017D \u017D \u017D \u017D \u017D " .getBytes (ISO_8859_1 ));
491498 }
492499 }
@@ -641,7 +648,7 @@ public Response onCompleted(Response response) throws Exception {
641648 }
642649
643650protected abstract String generatedAcceptEncodingHeader ();
644-
651+
645652@ Test (groups = {"standalone" ,"default_provider" ,"async" })
646653public void asyncDoPostBasicGZIPTest ()throws Throwable {
647654AsyncHttpClientConfig cf =new AsyncHttpClientConfig .Builder ().setCompressionEnforced (true ).build ();
@@ -694,16 +701,17 @@ public void asyncDoPostProxyTest() throws Throwable {
694701 }
695702sb .setLength (sb .length () -1 );
696703
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+ }
702710
703- @ Override
704- public void onThrowable (Throwable t ) {
705- }
706- }).get ();
711+ @ Override
712+ public void onThrowable (Throwable t ) {
713+ }
714+ }).get ();
707715
708716assertEquals (response .getStatusCode (),200 );
709717assertEquals (response .getHeader ("X-Connection" ),"keep-alive" );
@@ -720,7 +728,8 @@ public void asyncRequestVirtualServerPOSTTest() throws Throwable {
720728for (int i =0 ;i <5 ;i ++) {
721729m .put ("param_" +i ,Arrays .asList ("value_" +i ));
722730 }
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 ();
724733
725734Response response =client .executeRequest (request ,new AsyncCompletionHandlerAdapter ()).get ();
726735
@@ -748,7 +757,8 @@ public void asyncDoPutTest() throws Throwable {
748757 }
749758sb .setLength (sb .length () -1 );
750759
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 ();
752762
753763assertEquals (response .getStatusCode (),200 );
754764 }
@@ -803,11 +813,12 @@ public void asyncDoPostDelayCancelTest() throws Throwable {
803813StringBuilder sb =new StringBuilder ();
804814sb .append ("LockThread=true" );
805815
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+ });
811822
812823// Make sure we are connected before cancelling. I know, Thread.sleep
813824// sucks!
@@ -827,12 +838,13 @@ public void asyncDoPostDelayBytesTest() throws Throwable {
827838sb .append ("LockThread=true" );
828839
829840try {
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+ });
836848
837849future .get (10 ,TimeUnit .SECONDS );
838850 }catch (ExecutionException ex ) {
@@ -862,7 +874,8 @@ public void asyncDoPostNullBytesTest() throws Throwable {
862874 }
863875sb .setLength (sb .length () -1 );
864876
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 ());
866879
867880Response response =future .get ();
868881Assert .assertNotNull (response );
@@ -912,12 +925,13 @@ public void asyncConnectInvalidFuture() throws Throwable {
912925final AtomicInteger count =new AtomicInteger ();
913926for (int i =0 ;i <20 ;i ++) {
914927try {
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 ();
921935assertNull (response ,"Should have thrown ExecutionException" );
922936 }catch (ExecutionException ex ) {
923937Throwable cause =ex .getCause ();
@@ -935,12 +949,13 @@ public void asyncConnectInvalidPortFuture() throws Throwable {
935949try (AsyncHttpClient client =getAsyncHttpClient (null )) {
936950int dummyPort =findFreePort ();
937951try {
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 ();
944959assertNull (response ,"Should have thrown ExecutionException" );
945960 }catch (ExecutionException ex ) {
946961Throwable cause =ex .getCause ();
@@ -958,12 +973,13 @@ public void asyncConnectInvalidPort() throws Throwable {
958973int port =findFreePort ();
959974
960975try {
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 ();
967983assertNull (response ,"No ExecutionException was thrown" );
968984 }catch (ExecutionException ex ) {
969985assertEquals (ex .getCause ().getClass (),ConnectException .class );
@@ -994,7 +1010,8 @@ public void onThrowable(Throwable t) {
9941010 }
9951011 }
9961012
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 })
9981015public void asyncConnectInvalidHandlerHost ()throws Throwable {
9991016try (AsyncHttpClient client =getAsyncHttpClient (null )) {
10001017
@@ -1027,15 +1044,16 @@ public void asyncConnectInvalidFuturePort() throws Throwable {
10271044int port =findFreePort ();
10281045
10291046try {
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 ();
10391057assertNull (response ,"No ExecutionException was thrown" );
10401058 }catch (ExecutionException ex ) {
10411059assertEquals (ex .getCause ().getClass (),ConnectException .class );
@@ -1383,7 +1401,8 @@ public void testAsyncHttpProviderConfig() throws Exception {
13831401
13841402@ Test (groups = {"standalone" ,"default_provider" })
13851403public 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 ())) {
13871406FluentCaseInsensitiveStringsMap h =new FluentCaseInsensitiveStringsMap ();
13881407h .add ("Content-Type" ,"application/x-www-form-urlencoded" );
13891408h .add ("LockThread" ,"true" );
@@ -1420,17 +1439,18 @@ public void asyncDoPostCancelTest() throws Throwable {
14201439final AtomicReference <CancellationException >ex =new AtomicReference <>();
14211440ex .set (null );
14221441try {
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 () {
14241444
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+ }
14321452
1433- });
1453+ });
14341454
14351455Thread .sleep (1000 );
14361456future .cancel (true );
@@ -1444,7 +1464,7 @@ public void onThrowable(Throwable t) {
14441464protected String getBrokenTargetUrl () {
14451465return String .format ("http:127.0.0.1:%d/foo/test" ,port1 );
14461466 }
1447-
1467+
14481468@ Test (groups = {"standalone" ,"default_provider" },expectedExceptions = {NullPointerException .class })
14491469public void invalidUri ()throws Exception {
14501470try (AsyncHttpClient client =getAsyncHttpClient (null )) {
@@ -1479,5 +1499,18 @@ public void mirrorByteTest() throws Throwable {
14791499 }
14801500 }
14811501
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+
14821515protected abstract AsyncHttpProviderConfig <?, ?>getProviderConfig ();
14831516}