11package com .github .scribejava .httpclient .apache ;
22
3- import com .github .scribejava .core .model .OAuthAsyncRequestCallback ;
4- import com .github .scribejava .core .model .OAuthRequest ;
5- import com .github .scribejava .core .model .Response ;
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertNotNull ;
5+ import static org .junit .Assert .assertNull ;
6+ import static org .junit .Assert .assertTrue ;
7+ import static org .junit .Assert .fail ;
8+
9+ import java .io .ByteArrayInputStream ;
10+ import java .io .IOException ;
11+ import java .util .concurrent .CancellationException ;
12+ import java .util .concurrent .ExecutionException ;
13+
614import org .apache .http .HttpResponse ;
715import org .apache .http .ProtocolVersion ;
816import org .apache .http .entity .BasicHttpEntity ;
1119import org .junit .Before ;
1220import org .junit .Test ;
1321
14- import java .io .ByteArrayInputStream ;
15- import java .io .IOException ;
16- import java .util .concurrent .CancellationException ;
17- import java .util .concurrent .ExecutionException ;
18-
19- import static org .junit .Assert .assertEquals ;
20- import static org .junit .Assert .assertNotNull ;
21- import static org .junit .Assert .assertNull ;
22- import static org .junit .Assert .assertTrue ;
23- import static org .junit .Assert .fail ;
22+ import com .github .scribejava .core .exceptions .OAuthException ;
23+ import com .github .scribejava .core .model .OAuthAsyncRequestCallback ;
24+ import com .github .scribejava .core .model .OAuthRequest ;
25+ import com .github .scribejava .core .model .Response ;
2426
2527public class OauthAsyncCompletionHandlerTest {
2628
2729 private static final AllGoodResponseConverter ALL_GOOD_RESPONSE_CONVERTER = new AllGoodResponseConverter ();
2830 private static final ExceptionResponseConverter EXCEPTION_RESPONSE_CONVERTER = new ExceptionResponseConverter ();
31+ private static final OAuthExceptionResponseConverter OAUTH_EXCEPTION_RESPONSE_CONVERTER =
32+ new OAuthExceptionResponseConverter ();
2933
3034 private OAuthAsyncCompletionHandler <String > handler ;
3135 private TestCallback callback ;
@@ -63,8 +67,8 @@ public void setUp() {
6367 @ Test
6468 public void shouldReleaseLatchOnSuccess () throws Exception {
6569 handler = new OAuthAsyncCompletionHandler <>(callback , ALL_GOOD_RESPONSE_CONVERTER );
66- final HttpResponse response = new BasicHttpResponse ( new BasicStatusLine (
67- new ProtocolVersion ("4" , 1 , 1 ), 200 , "ok" ));
70+ final HttpResponse response =
71+ new BasicHttpResponse ( new BasicStatusLine ( new ProtocolVersion ("4" , 1 , 1 ), 200 , "ok" ));
6872 final BasicHttpEntity entity = new BasicHttpEntity ();
6973 entity .setContent (new ByteArrayInputStream (new byte [0 ]));
7074 response .setEntity (entity );
@@ -78,8 +82,8 @@ public void shouldReleaseLatchOnSuccess() throws Exception {
7882 @ Test
7983 public void shouldReleaseLatchOnIOException () throws Exception {
8084 handler = new OAuthAsyncCompletionHandler <>(callback , EXCEPTION_RESPONSE_CONVERTER );
81- final HttpResponse response = new BasicHttpResponse ( new BasicStatusLine (
82- new ProtocolVersion ("4" , 1 , 1 ), 200 , "ok" ));
85+ final HttpResponse response =
86+ new BasicHttpResponse ( new BasicStatusLine ( new ProtocolVersion ("4" , 1 , 1 ), 200 , "ok" ));
8387 final BasicHttpEntity entity = new BasicHttpEntity ();
8488 entity .setContent (new ByteArrayInputStream (new byte [0 ]));
8589 response .setEntity (entity );
@@ -96,11 +100,32 @@ public void shouldReleaseLatchOnIOException() throws Exception {
96100 }
97101 }
98102
103+ @ Test
104+ public void shouldReportOAuthException () throws Exception {
105+ handler = new OAuthAsyncCompletionHandler <>(callback , OAUTH_EXCEPTION_RESPONSE_CONVERTER );
106+ final HttpResponse response =
107+ new BasicHttpResponse (new BasicStatusLine (new ProtocolVersion ("4" , 1 , 1 ), 200 , "ok" ));
108+ final BasicHttpEntity entity = new BasicHttpEntity ();
109+ entity .setContent (new ByteArrayInputStream (new byte [0 ]));
110+ response .setEntity (entity );
111+ handler .completed (response );
112+ assertNull (callback .getResponse ());
113+ assertNotNull (callback .getThrowable ());
114+ assertTrue (callback .getThrowable () instanceof OAuthException );
115+ // verify latch is released
116+ try {
117+ handler .getResult ();
118+ fail ();
119+ } catch (ExecutionException expected ) {
120+ // expected
121+ }
122+ }
123+
99124 @ Test
100125 public void shouldReleaseLatchOnCancel () throws Exception {
101126 handler = new OAuthAsyncCompletionHandler <>(callback , ALL_GOOD_RESPONSE_CONVERTER );
102- final HttpResponse response = new BasicHttpResponse ( new BasicStatusLine (
103- new ProtocolVersion ("4" , 1 , 1 ), 200 , "ok" ));
127+ final HttpResponse response =
128+ new BasicHttpResponse ( new BasicStatusLine ( new ProtocolVersion ("4" , 1 , 1 ), 200 , "ok" ));
104129 final BasicHttpEntity entity = new BasicHttpEntity ();
105130 entity .setContent (new ByteArrayInputStream (new byte [0 ]));
106131 response .setEntity (entity );
@@ -120,8 +145,8 @@ public void shouldReleaseLatchOnCancel() throws Exception {
120145 @ Test
121146 public void shouldReleaseLatchOnFailure () throws Exception {
122147 handler = new OAuthAsyncCompletionHandler <>(callback , ALL_GOOD_RESPONSE_CONVERTER );
123- final HttpResponse response = new BasicHttpResponse ( new BasicStatusLine (
124- new ProtocolVersion ("4" , 1 , 1 ), 200 , "ok" ));
148+ final HttpResponse response =
149+ new BasicHttpResponse ( new BasicStatusLine ( new ProtocolVersion ("4" , 1 , 1 ), 200 , "ok" ));
125150 final BasicHttpEntity entity = new BasicHttpEntity ();
126151 entity .setContent (new ByteArrayInputStream (new byte [0 ]));
127152 response .setEntity (entity );
@@ -153,4 +178,12 @@ public String convert(Response response) throws IOException {
153178 throw new IOException ("Failed to convert" );
154179 }
155180 }
181+
182+ private static class OAuthExceptionResponseConverter implements OAuthRequest .ResponseConverter <String > {
183+
184+ @ Override
185+ public String convert (Response response ) throws IOException {
186+ throw new OAuthException ("bad oauth" );
187+ }
188+ }
156189}
0 commit comments