|
38 | 38 | import static org.junit.Assert.assertNotNull; |
39 | 39 | import static org.junit.Assert.assertNull; |
40 | 40 | import static org.junit.Assert.assertSame; |
| 41 | +import static org.junit.Assert.assertThrows; |
41 | 42 | import static org.junit.Assert.assertTrue; |
42 | 43 | import static org.junit.Assert.fail; |
43 | 44 |
|
@@ -975,6 +976,53 @@ public void idTokenWithAudience_iamFlow_targetAudienceDoesNotMatchAudClaim() thr |
975 | 976 | tokenCredential.getIdToken().getJsonWebSignature().getPayload().getAudience()); |
976 | 977 | } |
977 | 978 |
|
| 979 | + @Test |
| 980 | + public void idTokenWithAudience_oauthEndpoint_non2XXStatusCode() throws IOException { |
| 981 | + MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); |
| 982 | + transportFactory.transport.setError(new IOException("404 Not Found")); |
| 983 | + ServiceAccountCredentials credentials = |
| 984 | + createDefaultBuilder().setScopes(SCOPES).setHttpTransportFactory(transportFactory).build(); |
| 985 | + |
| 986 | + String targetAudience = "audience"; |
| 987 | + IdTokenCredentials tokenCredential = |
| 988 | + IdTokenCredentials.newBuilder() |
| 989 | + .setIdTokenProvider(credentials) |
| 990 | + .setTargetAudience(targetAudience) |
| 991 | + .build(); |
| 992 | + |
| 993 | + // Ensure that a non 2xx status code returns an exception and doesn't continue execution |
| 994 | + assertThrows(IOException.class, tokenCredential::refresh); |
| 995 | + } |
| 996 | + |
| 997 | + @Test |
| 998 | + public void idTokenWithAudience_iamEndpoint_non2XXStatusCode() throws IOException { |
| 999 | + String universeDomain = "test.com"; |
| 1000 | + MockIAMCredentialsServiceTransportFactory transportFactory = |
| 1001 | + new MockIAMCredentialsServiceTransportFactory(universeDomain); |
| 1002 | + transportFactory.getTransport().setTargetPrincipal(CLIENT_EMAIL); |
| 1003 | + transportFactory.getTransport().setIdToken(DEFAULT_ID_TOKEN); |
| 1004 | + transportFactory |
| 1005 | + .getTransport() |
| 1006 | + .addStatusCodeAndMessage(HttpStatusCodes.STATUS_CODE_NOT_FOUND, "Not Found"); |
| 1007 | + ServiceAccountCredentials credentials = |
| 1008 | + createDefaultBuilder() |
| 1009 | + .setScopes(SCOPES) |
| 1010 | + .setHttpTransportFactory(transportFactory) |
| 1011 | + .setUniverseDomain(universeDomain) |
| 1012 | + .build(); |
| 1013 | + |
| 1014 | + String targetAudience = "audience"; |
| 1015 | + IdTokenCredentials tokenCredential = |
| 1016 | + IdTokenCredentials.newBuilder() |
| 1017 | + .setIdTokenProvider(credentials) |
| 1018 | + .setTargetAudience(targetAudience) |
| 1019 | + .build(); |
| 1020 | + |
| 1021 | + // Ensure that a non 2xx status code returns an exception and doesn't continue execution |
| 1022 | + // Non 2xx status codes will be returned as HttpResponseException |
| 1023 | + assertThrows(IOException.class, tokenCredential::refresh); |
| 1024 | + } |
| 1025 | + |
978 | 1026 | @Test |
979 | 1027 | public void getScopes_nullReturnsEmpty() throws IOException { |
980 | 1028 | ServiceAccountCredentials credentials = createDefaultBuilder().build(); |
|
0 commit comments