Search in sources :

Example 51 with MockHttpTransport

use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.

the class AppIdentityCredentialTest method testAppEngineCredentialWrapperGetAccessToken.

public void testAppEngineCredentialWrapperGetAccessToken() throws IOException {
    final String expectedAccessToken = "ExpectedAccessToken";
    HttpTransport transport = new MockHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    MockAppIdentityService appIdentity = new MockAppIdentityService();
    appIdentity.setAccessTokenText(expectedAccessToken);
    AppIdentityCredential.Builder builder = new AppIdentityCredential.Builder(SCOPES);
    builder.setAppIdentityService(appIdentity);
    AppIdentityCredential appCredential = builder.build();
    GoogleCredential wrapper = new AppIdentityCredential.AppEngineCredentialWrapper(appCredential, transport, jsonFactory);
    assertTrue(wrapper.refreshToken());
    assertEquals(expectedAccessToken, wrapper.getAccessToken());
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) MockAppIdentityService(com.google.api.client.googleapis.extensions.appengine.testing.auth.oauth2.MockAppIdentityService) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory)

Example 52 with MockHttpTransport

use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.

the class AppIdentityCredentialTest method testAppEngineCredentialWrapperNullJsonFactoryThrows.

public void testAppEngineCredentialWrapperNullJsonFactoryThrows() throws IOException {
    HttpTransport transport = new MockHttpTransport();
    try {
        new AppIdentityCredential.AppEngineCredentialWrapper(transport, null);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport)

Example 53 with MockHttpTransport

use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.

the class AppIdentityCredentialTest method testAppEngineCredentialWrapper.

public void testAppEngineCredentialWrapper() throws IOException {
    final String expectedAccessToken = "ExpectedAccessToken";
    final Collection<String> emptyScopes = Collections.emptyList();
    HttpTransport transport = new MockHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    MockAppIdentityService appIdentity = new MockAppIdentityService();
    appIdentity.setAccessTokenText(expectedAccessToken);
    AppIdentityCredential.Builder builder = new AppIdentityCredential.Builder(emptyScopes);
    builder.setAppIdentityService(appIdentity);
    AppIdentityCredential appCredential = builder.build();
    GoogleCredential wrapper = new AppIdentityCredential.AppEngineCredentialWrapper(appCredential, transport, jsonFactory);
    HttpRequest request = transport.createRequestFactory().buildRequest("get", null, null);
    assertTrue(wrapper.createScopedRequired());
    try {
        wrapper.intercept(request);
        fail("Should not be able to use credential without scopes.");
    } catch (Exception expected) {
    }
    assertEquals(1, appIdentity.getGetAccessTokenCallCount());
    GoogleCredential scopedWrapper = wrapper.createScoped(SCOPES);
    assertNotSame(wrapper, scopedWrapper);
    scopedWrapper.intercept(request);
    assertEquals(2, appIdentity.getGetAccessTokenCallCount());
    HttpHeaders headers = request.getHeaders();
    String authHeader = headers.getAuthorization();
    assertTrue(authHeader.contains(expectedAccessToken));
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) JsonFactory(com.google.api.client.json.JsonFactory) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) MockAppIdentityService(com.google.api.client.googleapis.extensions.appengine.testing.auth.oauth2.MockAppIdentityService) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) IOException(java.io.IOException) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport)

Example 54 with MockHttpTransport

use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.

the class DefaultCredentialProviderTest method testGetDefaultCredentials_cloudshell.

public void testGetDefaultCredentials_cloudshell() throws IOException {
    HttpTransport transport = new MockHttpTransport();
    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 : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport)

Example 55 with MockHttpTransport

use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.

the class GoogleCredentialTest method testFromStreamNullStreamThrows.

public void testFromStreamNullStreamThrows() throws IOException {
    HttpTransport transport = new MockHttpTransport();
    try {
        GoogleCredential.fromStream(null, transport, JSON_FACTORY);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport)

Aggregations

MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)83 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)44 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)42 Test (org.junit.Test)32 HttpTransport (com.google.api.client.http.HttpTransport)30 IOException (java.io.IOException)29 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)26 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)23 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)19 HttpRequest (com.google.api.client.http.HttpRequest)14 JsonFactory (com.google.api.client.json.JsonFactory)13 GenericJson (com.google.api.client.json.GenericJson)12 Storage (com.google.api.services.storage.Storage)9 HttpResponse (com.google.api.client.http.HttpResponse)7 MockGoogleClient (com.google.api.client.googleapis.testing.services.MockGoogleClient)6 GenericUrl (com.google.api.client.http.GenericUrl)6 MockGoogleClientRequest (com.google.api.client.googleapis.testing.services.MockGoogleClientRequest)5 Objectify (com.googlecode.objectify.Objectify)5 MockTokenServerTransport (com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)4 ByteArrayContent (com.google.api.client.http.ByteArrayContent)4