use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.
the class GoogleCredentialTest method testFromStreamServiceAccountMissingPrivateKeyIdThrows.
public void testFromStreamServiceAccountMissingPrivateKeyIdThrows() throws IOException {
final String serviceAccountId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com";
final String serviceAccountEmail = "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com";
MockHttpTransport transport = new MockTokenServerTransport();
// Write out user file
GenericJson serviceAccountContents = new GenericJson();
serviceAccountContents.setFactory(JSON_FACTORY);
serviceAccountContents.put("client_id", serviceAccountId);
serviceAccountContents.put("client_email", serviceAccountEmail);
serviceAccountContents.put("private_key", SA_KEY_TEXT);
serviceAccountContents.put("type", GoogleCredential.SERVICE_ACCOUNT_FILE_TYPE);
String json = serviceAccountContents.toPrettyString();
InputStream serviceAccountStream = new ByteArrayInputStream(json.getBytes());
try {
GoogleCredential.fromStream(serviceAccountStream, transport, JSON_FACTORY);
fail();
} catch (IOException expected) {
assertTrue(expected.getMessage().contains("private_key_id"));
}
}
use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.
the class GoogleCredentialTest method testFromStreamNullJsonFactoryThrows.
public void testFromStreamNullJsonFactoryThrows() throws IOException {
HttpTransport transport = new MockHttpTransport();
InputStream stream = new ByteArrayInputStream("foo".getBytes());
try {
GoogleCredential.fromStream(stream, transport, null);
fail();
} catch (NullPointerException expected) {
}
}
use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.
the class GoogleCredentialTest method testFromStreamServiceAccountMissingClientEmailThrows.
public void testFromStreamServiceAccountMissingClientEmailThrows() throws IOException {
final String serviceAccountId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com";
MockHttpTransport transport = new MockTokenServerTransport();
// Write out user file
GenericJson serviceAccountContents = new GenericJson();
serviceAccountContents.setFactory(JSON_FACTORY);
serviceAccountContents.put("client_id", serviceAccountId);
serviceAccountContents.put("private_key", SA_KEY_TEXT);
serviceAccountContents.put("private_key_id", SA_KEY_ID);
serviceAccountContents.put("type", GoogleCredential.SERVICE_ACCOUNT_FILE_TYPE);
String json = serviceAccountContents.toPrettyString();
InputStream serviceAccountStream = new ByteArrayInputStream(json.getBytes());
try {
GoogleCredential.fromStream(serviceAccountStream, transport, JSON_FACTORY);
fail();
} catch (IOException expected) {
assertTrue(expected.getMessage().contains("client_email"));
}
}
use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.
the class GoogleCredentialTest method testFromStreamServiceAccountMissingClientIdThrows.
public void testFromStreamServiceAccountMissingClientIdThrows() throws IOException {
final String serviceAccountEmail = "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com";
MockHttpTransport transport = new MockTokenServerTransport();
// Write out user file
GenericJson serviceAccountContents = new GenericJson();
serviceAccountContents.setFactory(JSON_FACTORY);
serviceAccountContents.put("client_email", serviceAccountEmail);
serviceAccountContents.put("private_key", SA_KEY_TEXT);
serviceAccountContents.put("private_key_id", SA_KEY_ID);
serviceAccountContents.put("type", GoogleCredential.SERVICE_ACCOUNT_FILE_TYPE);
String json = serviceAccountContents.toPrettyString();
InputStream serviceAccountStream = new ByteArrayInputStream(json.getBytes());
try {
GoogleCredential.fromStream(serviceAccountStream, transport, JSON_FACTORY);
fail();
} catch (IOException expected) {
assertTrue(expected.getMessage().contains("client_id"));
}
}
use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.
the class BatchRequestTest method subtestExecute_checkWriteTo.
private void subtestExecute_checkWriteTo(final String expectedOutput, HttpRequest... requests) throws IOException {
MockHttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) {
return new MockLowLevelHttpRequest(url) {
@Override
public LowLevelHttpResponse execute() throws IOException {
assertEquals("multipart/mixed; boundary=__END_OF_PART__", getContentType());
ByteArrayOutputStream out = new ByteArrayOutputStream();
getStreamingContent().writeTo(out);
assertEquals(expectedOutput, out.toString("UTF-8"));
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(200);
response.addHeader("Content-Type", "multipart/mixed; boundary=" + RESPONSE_BOUNDARY);
String content2 = "{\"name\": \"" + TEST_NAME + "\", \"number\": \"" + TEST_NUM + "\"}";
StringBuilder responseContent = new StringBuilder();
responseContent.append("--" + RESPONSE_BOUNDARY + "\n").append("Content-Type: application/http\n").append("Content-Transfer-Encoding: binary\n").append("Content-ID: response-1\n\n").append("HTTP/1.1 200 OK\n").append("Content-Type: application/json; charset=UTF-8\n").append("Content-Length: " + content2.length() + "\n\n").append(content2 + "\n\n").append("--" + RESPONSE_BOUNDARY + "--\n\n");
response.setContent(responseContent.toString());
return response;
}
};
}
};
BatchRequest batchRequest = new BatchRequest(transport, null);
BatchCallback<Void, Void> callback = new BatchCallback<Void, Void>() {
@Override
public void onSuccess(Void t, HttpHeaders responseHeaders) {
}
@Override
public void onFailure(Void e, HttpHeaders responseHeaders) {
}
};
for (HttpRequest request : requests) {
batchRequest.queue(request, Void.class, Void.class, callback);
}
batchRequest.execute();
}
Aggregations