Search in sources :

Example 1 with MockMetadataServerTransport

use of com.google.api.client.googleapis.testing.compute.MockMetadataServerTransport in project google-api-java-client by google.

the class DefaultCredentialProviderTest method testDefaultCredentialComputeErrorUnexpected.

public void testDefaultCredentialComputeErrorUnexpected() throws IOException {
    MockMetadataServerTransport transport = new MockMetadataServerTransport(ACCESS_TOKEN);
    transport.setTokenRequestStatusCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR);
    TestDefaultCredentialProvider testProvider = new TestDefaultCredentialProvider();
    Credential defaultCredential = testProvider.getDefaultCredential(transport, JSON_FACTORY);
    assertNotNull(defaultCredential);
    try {
        defaultCredential.refreshToken();
        fail("Expected error refreshing token.");
    } catch (IOException expected) {
        String message = expected.getMessage();
        assertTrue(message.contains(Integer.toString(HttpStatusCodes.STATUS_CODE_SERVER_ERROR)));
        assertTrue(message.contains("Unexpected"));
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) MockMetadataServerTransport(com.google.api.client.googleapis.testing.compute.MockMetadataServerTransport) IOException(java.io.IOException)

Example 2 with MockMetadataServerTransport

use of com.google.api.client.googleapis.testing.compute.MockMetadataServerTransport in project google-api-java-client by google.

the class DefaultCredentialProviderTest method testDefaultCredentialCompute.

public void testDefaultCredentialCompute() throws IOException {
    HttpTransport transport = new MockMetadataServerTransport(ACCESS_TOKEN);
    TestDefaultCredentialProvider testProvider = new TestDefaultCredentialProvider();
    Credential defaultCredential = testProvider.getDefaultCredential(transport, JSON_FACTORY);
    assertNotNull(defaultCredential);
    assertTrue(defaultCredential.refreshToken());
    assertEquals(ACCESS_TOKEN, defaultCredential.getAccessToken());
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) Credential(com.google.api.client.auth.oauth2.Credential) MockMetadataServerTransport(com.google.api.client.googleapis.testing.compute.MockMetadataServerTransport)

Example 3 with MockMetadataServerTransport

use of com.google.api.client.googleapis.testing.compute.MockMetadataServerTransport in project google-api-java-client by google.

the class DefaultCredentialProviderTest method testGetDefaultCredentials_cloudshell_withComputCredentialsPresent.

public void testGetDefaultCredentials_cloudshell_withComputCredentialsPresent() throws IOException {
    MockMetadataServerTransport transport = new MockMetadataServerTransport(ACCESS_TOKEN);
    TestDefaultCredentialProvider testProvider = new TestDefaultCredentialProvider();
    testProvider.setEnv(DefaultCredentialProvider.CLOUD_SHELL_ENV_VAR, "4");
    GoogleCredential defaultCredential = testProvider.getDefaultCredential(transport, JSON_FACTORY);
    assertTrue(defaultCredential instanceof CloudShellCredential);
    assertEquals(((CloudShellCredential) defaultCredential).getAuthPort(), 4);
}
Also used : MockMetadataServerTransport(com.google.api.client.googleapis.testing.compute.MockMetadataServerTransport)

Example 4 with MockMetadataServerTransport

use of com.google.api.client.googleapis.testing.compute.MockMetadataServerTransport in project google-api-java-client by google.

the class ComputeCredentialTest method testExecuteRefreshToken.

public void testExecuteRefreshToken() throws Exception {
    HttpTransport transport = new MockMetadataServerTransport(ACCESS_TOKEN);
    ComputeCredential credential = new ComputeCredential(transport, new JacksonFactory());
    assertTrue(credential.refreshToken());
    assertEquals(ACCESS_TOKEN, credential.getAccessToken());
}
Also used : HttpTransport(com.google.api.client.http.HttpTransport) MockMetadataServerTransport(com.google.api.client.googleapis.testing.compute.MockMetadataServerTransport) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory)

Example 5 with MockMetadataServerTransport

use of com.google.api.client.googleapis.testing.compute.MockMetadataServerTransport in project google-api-java-client by google.

the class DefaultCredentialProviderTest method testDefaultCredentialComputeErrorNotFound.

public void testDefaultCredentialComputeErrorNotFound() throws IOException {
    MockMetadataServerTransport transport = new MockMetadataServerTransport(ACCESS_TOKEN);
    transport.setTokenRequestStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
    TestDefaultCredentialProvider testProvider = new TestDefaultCredentialProvider();
    Credential defaultCredential = testProvider.getDefaultCredential(transport, JSON_FACTORY);
    assertNotNull(defaultCredential);
    try {
        defaultCredential.refreshToken();
        fail("Expected error refreshing token.");
    } catch (IOException expected) {
        String message = expected.getMessage();
        assertTrue(message.contains(Integer.toString(HttpStatusCodes.STATUS_CODE_NOT_FOUND)));
        assertTrue(message.contains("scope"));
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) MockMetadataServerTransport(com.google.api.client.googleapis.testing.compute.MockMetadataServerTransport) IOException(java.io.IOException)

Aggregations

MockMetadataServerTransport (com.google.api.client.googleapis.testing.compute.MockMetadataServerTransport)5 Credential (com.google.api.client.auth.oauth2.Credential)3 HttpTransport (com.google.api.client.http.HttpTransport)2 IOException (java.io.IOException)2 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)1