QuotaProjectIdHidingCredentials extends Credentials { @Override public Map<String, List<String>> getRequestMetadata(URI uri) throws IOException { ImmutableMap.Builder<String, List<String>> metaBuilder = ImmutableMap.builder(); for (Map.Entry<String, List<String>> entry : this.wrappedCredentials.getRequestMetadata(uri).entrySet()) { if (entry.getKey().equals(QUOTA_PROJECT_ID_HEADER_KEY)) { continue; } metaBuilder.put(entry); } return metaBuilder.build(); } QuotaProjectIdHidingCredentials(Credentials credentials); @Override String getAuthenticationType(); @Override Map<String, List<String>> getRequestMetadata(URI uri); @Override Map<String, List<String>> getRequestMetadata(); @Override boolean hasRequestMetadata(); @Override boolean hasRequestMetadataOnly(); @Override void refresh();  }
@Test public void quotaProjectIdHidingCredentials_getRequestMetadata() throws IOException { Map<String, List<String>> metaDataWithQuota = ImmutableMap.of( "k1", Collections.singletonList("v1"), QUOTA_PROJECT_ID_KEY, Collections.singletonList(QUOTA_PROJECT_ID_FROM_CREDENTIALS_VALUE)); Credentials credentialsWithQuotaProjectId = Mockito.mock(GoogleCredentials.class); Mockito.when(credentialsWithQuotaProjectId.getRequestMetadata(null)) .thenReturn(metaDataWithQuota); QuotaProjectIdHidingCredentials quotaProjectIdHidingCredentials = new QuotaProjectIdHidingCredentials(credentialsWithQuotaProjectId); Map<String, List<String>> metaDataHidingQuota = quotaProjectIdHidingCredentials.getRequestMetadata(); Assert.assertTrue(metaDataWithQuota.containsKey(QUOTA_PROJECT_ID_KEY)); Assert.assertFalse(metaDataHidingQuota.containsKey(QUOTA_PROJECT_ID_KEY)); Assert.assertEquals(metaDataWithQuota.size() - 1, metaDataHidingQuota.size()); Map<String, List<String>> metaDataWithoutQuota = ImmutableMap.of("k1", Collections.singletonList("v1")); Credentials credentialsWithoutQuotaProjectId = Mockito.mock(GoogleCredentials.class); Mockito.when(credentialsWithoutQuotaProjectId.getRequestMetadata(null)) .thenReturn(metaDataWithoutQuota); QuotaProjectIdHidingCredentials quotaProjectIdHidingCredentialsWithout = new QuotaProjectIdHidingCredentials(credentialsWithoutQuotaProjectId); Map<String, List<String>> metaDataHidingQuotaWithout = quotaProjectIdHidingCredentials.getRequestMetadata(); Assert.assertEquals( quotaProjectIdHidingCredentialsWithout.getRequestMetadata(), metaDataWithoutQuota); }