@@ -94,6 +94,12 @@ public class AuthorizationServiceTest {
9494 + " \" application_type\" : " + RegistrationRequest .APPLICATION_TYPE_NATIVE + "\n "
9595 + "}" ;
9696
97+ private static final String INVALID_GRANT_RESPONSE_JSON = "{\n "
98+ + " \" error\" : \" invalid_grant\" ,\n "
99+ + " \" error_description\" : \" invalid_grant description\" \n "
100+ + "}" ;
101+ private static final int TEST_INVALID_GRANT_CODE = 2002 ;
102+
97103 private AuthorizationCallback mAuthCallback ;
98104 private RegistrationCallback mRegistrationCallback ;
99105 private AuthorizationService mService ;
@@ -170,6 +176,7 @@ public void testAuthorizationRequest_afterDispose() throws Exception {
170176 public void testTokenRequest () throws Exception {
171177 InputStream is = new ByteArrayInputStream (AUTH_CODE_EXCHANGE_RESPONSE_JSON .getBytes ());
172178 when (mHttpConnection .getInputStream ()).thenReturn (is );
179+ when (mHttpConnection .getResponseCode ()).thenReturn (HttpURLConnection .HTTP_OK );
173180 TokenRequest request = getTestAuthCodeExchangeRequest ();
174181 mService .performTokenRequest (request , mAuthCallback );
175182 mAuthCallback .waitForCallback ();
@@ -182,6 +189,7 @@ public void testTokenRequest() throws Exception {
182189 public void testTokenRequest_withBasicAuth () throws Exception {
183190 ClientSecretBasic csb = new ClientSecretBasic (TEST_CLIENT_SECRET );
184191 InputStream is = new ByteArrayInputStream (AUTH_CODE_EXCHANGE_RESPONSE_JSON .getBytes ());
192+ when (mHttpConnection .getResponseCode ()).thenReturn (HttpURLConnection .HTTP_OK );
185193 when (mHttpConnection .getInputStream ()).thenReturn (is );
186194 TokenRequest request = getTestAuthCodeExchangeRequest ();
187195 mService .performTokenRequest (request , csb , mAuthCallback );
@@ -198,6 +206,7 @@ public void testTokenRequest_withPostAuth() throws Exception {
198206 ClientSecretPost csp = new ClientSecretPost (TEST_CLIENT_SECRET );
199207 InputStream is = new ByteArrayInputStream (AUTH_CODE_EXCHANGE_RESPONSE_JSON .getBytes ());
200208 when (mHttpConnection .getInputStream ()).thenReturn (is );
209+ when (mHttpConnection .getResponseCode ()).thenReturn (HttpURLConnection .HTTP_OK );
201210 TokenRequest request = getTestAuthCodeExchangeRequest ();
202211 mService .performTokenRequest (request , csp , mAuthCallback );
203212 mAuthCallback .waitForCallback ();
@@ -209,10 +218,35 @@ public void testTokenRequest_withPostAuth() throws Exception {
209218 assertTokenRequestBody (postBody , expectedRequestBody );
210219 }
211220
221+ @ Test
222+ public void testTokenRequest_withInvalidGrant () throws Exception {
223+ ClientSecretPost csp = new ClientSecretPost (TEST_CLIENT_SECRET );
224+ InputStream is = new ByteArrayInputStream (INVALID_GRANT_RESPONSE_JSON .getBytes ());
225+ when (mHttpConnection .getErrorStream ()).thenReturn (is );
226+ when (mHttpConnection .getResponseCode ()).thenReturn (HttpURLConnection .HTTP_BAD_REQUEST );
227+ TokenRequest request = getTestAuthCodeExchangeRequest ();
228+ mService .performTokenRequest (request , csp , mAuthCallback );
229+ mAuthCallback .waitForCallback ();
230+ assertInvalidGrant (mAuthCallback .error );
231+ }
232+
233+ @ Test
234+ public void testTokenRequest_withInvalidGrant2 () throws Exception {
235+ ClientSecretPost csp = new ClientSecretPost (TEST_CLIENT_SECRET );
236+ InputStream is = new ByteArrayInputStream (INVALID_GRANT_RESPONSE_JSON .getBytes ());
237+ when (mHttpConnection .getErrorStream ()).thenReturn (is );
238+ when (mHttpConnection .getResponseCode ()).thenReturn (199 );
239+ TokenRequest request = getTestAuthCodeExchangeRequest ();
240+ mService .performTokenRequest (request , csp , mAuthCallback );
241+ mAuthCallback .waitForCallback ();
242+ assertInvalidGrant (mAuthCallback .error );
243+ }
244+
212245 @ Test
213246 public void testTokenRequest_IoException () throws Exception {
214247 Exception ex = new IOException ();
215248 when (mHttpConnection .getInputStream ()).thenThrow (ex );
249+ when (mHttpConnection .getResponseCode ()).thenReturn (HttpURLConnection .HTTP_OK );
216250 mService .performTokenRequest (getTestAuthCodeExchangeRequest (), mAuthCallback );
217251 mAuthCallback .waitForCallback ();
218252 assertNotNull (mAuthCallback .error );
@@ -272,6 +306,14 @@ private void assertTokenResponse(TokenResponse response, TokenRequest expectedRe
272306 assertEquals (TEST_ID_TOKEN , response .idToken );
273307 }
274308
309+ private void assertInvalidGrant (AuthorizationException error ) {
310+ assertNotNull (error );
311+ assertEquals (AuthorizationException .TYPE_OAUTH_TOKEN_ERROR , error .type );
312+ assertEquals (TEST_INVALID_GRANT_CODE , error .code );
313+ assertEquals ("invalid_grant" , error .error );
314+ assertEquals ("invalid_grant description" , error .errorDescription );
315+ }
316+
275317 private void assertRegistrationResponse (RegistrationResponse response ,
276318 RegistrationRequest expectedRequest ) {
277319 assertThat (response ).isNotNull ();
0 commit comments