@@ -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,23 @@ 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+
212233 @ Test
213234 public void testTokenRequest_IoException () throws Exception {
214235 Exception ex = new IOException ();
215236 when (mHttpConnection .getInputStream ()).thenThrow (ex );
237+ when (mHttpConnection .getResponseCode ()).thenReturn (HttpURLConnection .HTTP_OK );
216238 mService .performTokenRequest (getTestAuthCodeExchangeRequest (), mAuthCallback );
217239 mAuthCallback .waitForCallback ();
218240 assertNotNull (mAuthCallback .error );
@@ -272,6 +294,14 @@ private void assertTokenResponse(TokenResponse response, TokenRequest expectedRe
272294 assertEquals (TEST_ID_TOKEN , response .idToken );
273295 }
274296
297+ private void assertInvalidGrant (AuthorizationException error ) {
298+ assertNotNull (error );
299+ assertEquals (AuthorizationException .TYPE_OAUTH_TOKEN_ERROR , error .type );
300+ assertEquals (TEST_INVALID_GRANT_CODE , error .code );
301+ assertEquals ("invalid_grant" , error .error );
302+ assertEquals ("invalid_grant description" , error .errorDescription );
303+ }
304+
275305 private void assertRegistrationResponse (RegistrationResponse response ,
276306 RegistrationRequest expectedRequest ) {
277307 assertThat (response ).isNotNull ();
0 commit comments