Search in sources :

Example 1 with MockAppIdentityService

use of com.google.api.client.googleapis.extensions.appengine.testing.auth.oauth2.MockAppIdentityService in project google-api-java-client by google.

the class AppIdentityCredentialTest method testUsesAppIdentityService.

public void testUsesAppIdentityService() throws IOException {
    final String expectedAccessToken = "ExpectedAccessToken";
    MockAppIdentityService appIdentity = new MockAppIdentityService();
    appIdentity.setAccessTokenText(expectedAccessToken);
    AppIdentityCredential.Builder builder = new AppIdentityCredential.Builder(SCOPES);
    builder.setAppIdentityService(appIdentity);
    AppIdentityCredential appCredential = builder.build();
    HttpTransport transport = new MockHttpTransport();
    HttpRequest request = transport.createRequestFactory().buildRequest("get", null, null);
    appCredential.intercept(request);
    assertEquals(1, appIdentity.getGetAccessTokenCallCount());
    HttpHeaders headers = request.getHeaders();
    String authHeader = headers.getAuthorization();
    Boolean headerContainsToken = authHeader.contains(expectedAccessToken);
    assertTrue(headerContainsToken);
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) HttpHeaders(com.google.api.client.http.HttpHeaders) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockAppIdentityService(com.google.api.client.googleapis.extensions.appengine.testing.auth.oauth2.MockAppIdentityService)

Example 2 with MockAppIdentityService

use of com.google.api.client.googleapis.extensions.appengine.testing.auth.oauth2.MockAppIdentityService 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 3 with MockAppIdentityService

use of com.google.api.client.googleapis.extensions.appengine.testing.auth.oauth2.MockAppIdentityService 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)

Aggregations

MockAppIdentityService (com.google.api.client.googleapis.extensions.appengine.testing.auth.oauth2.MockAppIdentityService)3 HttpTransport (com.google.api.client.http.HttpTransport)3 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)3 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)2 HttpHeaders (com.google.api.client.http.HttpHeaders)2 HttpRequest (com.google.api.client.http.HttpRequest)2 JsonFactory (com.google.api.client.json.JsonFactory)2 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)2 IOException (java.io.IOException)1