Search in sources :

Example 81 with HttpTransport

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

the class MethodOverrideTest method testInterceptMaxLength.

public void testInterceptMaxLength() throws IOException {
    HttpTransport transport = new MockHttpTransport();
    GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL);
    url.set("a", "foo");
    HttpRequest request = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
    new MethodOverride().intercept(request);
    assertEquals(HttpMethods.GET, request.getRequestMethod());
    assertNull(request.getHeaders().get(MethodOverride.HEADER));
    assertNull(request.getContent());
    char[] arr = new char[MethodOverride.MAX_URL_LENGTH];
    Arrays.fill(arr, 'x');
    url.set("a", new String(arr));
    request.setUrl(url);
    new MethodOverride().intercept(request);
    assertEquals(HttpMethods.POST, request.getRequestMethod());
    assertEquals(HttpMethods.GET, request.getHeaders().get(MethodOverride.HEADER));
    assertEquals(HttpTesting.SIMPLE_GENERIC_URL, request.getUrl());
    char[] arr2 = new char[arr.length + 2];
    Arrays.fill(arr2, 'x');
    arr2[0] = 'a';
    arr2[1] = '=';
    UrlEncodedContent content = (UrlEncodedContent) request.getContent();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    content.writeTo(out);
    assertEquals(new String(arr2), out.toString());
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) UrlEncodedContent(com.google.api.client.http.UrlEncodedContent) GenericUrl(com.google.api.client.http.GenericUrl) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 82 with HttpTransport

use of com.google.api.client.http.HttpTransport in project incubator-heron by apache.

the class GcsUploader method createStorage.

private Storage createStorage(Credential credential) throws GeneralSecurityException, IOException {
    final HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    final JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    return new Storage.Builder(httpTransport, jsonFactory, credential).build();
}
Also used : HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) Storage(com.google.api.services.storage.Storage) JsonFactory(com.google.api.client.json.JsonFactory)

Example 83 with HttpTransport

use of com.google.api.client.http.HttpTransport in project data-transfer-project by google.

the class GoogleCloudExtension method initialize.

/*
   * Initializes the GoogleCloudExtension based on the ExtensionContext.
   */
@Override
public void initialize(ExtensionContext context) {
    Preconditions.checkArgument(!initialized, "Attempting to initialize GoogleCloudExtension more than once");
    HttpTransport httpTransport = context.getService(HttpTransport.class);
    JsonFactory jsonFactory = context.getService(JsonFactory.class);
    injector = Guice.createInjector(new GoogleCloudExtensionModule(httpTransport, jsonFactory, context.cloud(), context.environment()));
    initialized = true;
}
Also used : HttpTransport(com.google.api.client.http.HttpTransport) JsonFactory(com.google.api.client.json.JsonFactory)

Example 84 with HttpTransport

use of com.google.api.client.http.HttpTransport in project copybara by google.

the class GithubPrOriginTest method setup.

@Before
public void setup() throws Exception {
    repoGitDir = Files.createTempDirectory("GithubPrDestinationTest-repoGitDir");
    workdir = Files.createTempDirectory("workdir");
    localHub = Files.createTempDirectory("localHub");
    git("init", "--bare", repoGitDir.toString());
    console = new TestingConsole();
    options = new OptionsBuilder().setConsole(console).setOutputRootToTmpDir();
    options.git = new TestGitOptions(localHub, () -> this.options.general, new Validator() {

        @Override
        public void validateFetch(String url, boolean prune, boolean force, Iterable<String> refspecs) {
            for (String refspec : refspecs) {
                // WARNING! This check is important. While using short names like
                // 'master' in git fetch works for local git invocations, other
                // implementations of GitRepository might have problems if we don't
                // pass the whole reference.
                assertThat(refspec).startsWith("refs/");
                assertThat(refspec).contains(":refs/");
            }
        }
    });
    options.github = new GithubOptions(() -> options.general, options.git) {

        @Override
        public GithubApi getApi(String project) throws RepoException {
            assertThat(project).isEqualTo(expectedProject);
            return super.getApi(project);
        }

        @Override
        protected HttpTransport getHttpTransport() {
            return gitApiMockHttpTransport;
        }
    };
    Path credentialsFile = Files.createTempFile("credentials", "test");
    Files.write(credentialsFile, "https://user:SECRET@github.com".getBytes(UTF_8));
    options.git.credentialHelperStorePath = credentialsFile.toString();
    skylark = new SkylarkTestExecutor(options, GitModule.class);
    skylarkParser = new SkylarkParser(ImmutableSet.of(Core.class, Authoring.Module.class, FolderModule.class, GitModule.class));
}
Also used : Path(java.nio.file.Path) SkylarkParser(com.google.copybara.config.SkylarkParser) TestGitOptions(com.google.copybara.testing.git.GitTestUtil.TestGitOptions) GithubApi(com.google.copybara.git.github.api.GithubApi) RepoException(com.google.copybara.exception.RepoException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) HttpTransport(com.google.api.client.http.HttpTransport) GitApiMockHttpTransport(com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport) Authoring(com.google.copybara.authoring.Authoring) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Validator(com.google.copybara.testing.git.GitTestUtil.Validator) Before(org.junit.Before)

Example 85 with HttpTransport

use of com.google.api.client.http.HttpTransport in project google-auth-library-java by google.

the class HttpCredentialsAdapterTest method initialize_populatesOAuth2Credentials_handle401.

@Test
public void initialize_populatesOAuth2Credentials_handle401() throws IOException {
    final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
    final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2";
    MockTokenServerTransportFactory tokenServerTransportFactory = new MockTokenServerTransportFactory();
    tokenServerTransportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
    tokenServerTransportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken);
    OAuth2Credentials credentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setHttpTransportFactory(tokenServerTransportFactory).build();
    credentials.refresh();
    HttpCredentialsAdapter adapter = new HttpCredentialsAdapter(credentials);
    HttpTransport primaryHttpTransport = new MockTokenCheckingTransport(tokenServerTransportFactory.transport, REFRESH_TOKEN);
    HttpRequestFactory requestFactory = primaryHttpTransport.createRequestFactory();
    HttpRequest request = requestFactory.buildGetRequest(new GenericUrl("http://foo"));
    adapter.initialize(request);
    // now switch out the access token so that the original one is invalid,
    // requiring a refresh of the access token
    tokenServerTransportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken2);
    HttpResponse response = request.execute();
    // make sure that the request is successful despite the invalid access token
    assertEquals(200, response.getStatusCode());
    assertEquals(MockTokenCheckingTransport.SUCCESS_CONTENT, response.parseAsString());
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpTransport(com.google.api.client.http.HttpTransport) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) MockTokenCheckingTransport(com.google.auth.oauth2.MockTokenCheckingTransport) HttpResponse(com.google.api.client.http.HttpResponse) OAuth2Credentials(com.google.auth.oauth2.OAuth2Credentials) GenericUrl(com.google.api.client.http.GenericUrl) Test(org.junit.Test)

Aggregations

HttpTransport (com.google.api.client.http.HttpTransport)106 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)40 JsonFactory (com.google.api.client.json.JsonFactory)35 HttpRequest (com.google.api.client.http.HttpRequest)29 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)28 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)28 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)27 IOException (java.io.IOException)24 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)21 HttpResponse (com.google.api.client.http.HttpResponse)21 GenericUrl (com.google.api.client.http.GenericUrl)15 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)12 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)11 Credential (com.google.api.client.auth.oauth2.Credential)10 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)10 Storage (com.google.api.services.storage.Storage)9 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)8 Test (org.junit.Test)8 Monitor (org.datatransferproject.api.launcher.Monitor)7 ErrorTransport (com.google.api.client.googleapis.json.GoogleJsonErrorTest.ErrorTransport)6