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());
}
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();
}
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;
}
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));
}
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());
}
Aggregations