use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project java-docs-samples by GoogleCloudPlatform.
the class LocalUrlFetchTest method testMockUrlFetch.
@Test
public void testMockUrlFetch() throws IOException {
// See http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
MockHttpTransport mockHttpTransport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
assertEquals(method, "GET");
assertEquals(url, "http://foo.bar");
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(234);
return response;
}
};
}
};
HttpRequestFactory requestFactory = mockHttpTransport.createRequestFactory();
HttpResponse response = requestFactory.buildGetRequest(new GenericUrl("http://foo.bar")).execute();
assertEquals(response.getStatusCode(), 234);
}
use of com.google.api.client.testing.http.MockLowLevelHttpRequest 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();
}
use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project google-api-java-client by google.
the class AbstractGoogleClientRequestTest method testExecuteUsingHead.
public void testExecuteUsingHead() throws Exception {
HttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(final String method, final String url) {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() {
assertEquals("HEAD", method);
assertEquals("https://www.googleapis.com/test/path/v1/tests/foo", url);
return new MockLowLevelHttpResponse();
}
};
}
};
MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName("Test Application").build();
MockGoogleClientRequest<String> request = new MockGoogleClientRequest<String>(client, HttpMethods.GET, URI_TEMPLATE, null, String.class);
request.put("testId", "foo");
request.executeUsingHead();
}
use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project google-api-java-client by google.
the class AbstractGoogleClientRequestTest method testExecute_void.
public void testExecute_void() throws Exception {
HttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(final String method, final String url) {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() {
return new MockLowLevelHttpResponse().setContent("{\"a\":\"ignored\"}").setContentType(Json.MEDIA_TYPE);
}
};
}
};
MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName("Test Application").build();
MockGoogleClientRequest<Void> request = new MockGoogleClientRequest<Void>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
Void v = request.execute();
assertNull(v);
}
use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project google-api-java-client by google.
the class MockMetadataServerTransport method buildRequest.
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
if (url.equals(METADATA_TOKEN_SERVER_URL)) {
MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url) {
@Override
public LowLevelHttpResponse execute() throws IOException {
if (tokenRequestStatusCode != null) {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse().setStatusCode(tokenRequestStatusCode).setContent("Token Fetch Error");
return response;
}
String metadataRequestHeader = getFirstHeaderValue("Metadata-Flavor");
if (!"Google".equals(metadataRequestHeader)) {
throw new IOException("Metadata request header not found.");
}
// Create the JSon response
GenericJson refreshContents = new GenericJson();
refreshContents.setFactory(JSON_FACTORY);
refreshContents.put("access_token", accessToken);
refreshContents.put("expires_in", 3600000);
refreshContents.put("token_type", "Bearer");
String refreshText = refreshContents.toPrettyString();
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse().setContentType(Json.MEDIA_TYPE).setContent(refreshText);
return response;
}
};
return request;
} else if (url.equals(METADATA_SERVER_URL)) {
MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url) {
@Override
public LowLevelHttpResponse execute() {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.addHeader("Metadata-Flavor", "Google");
return response;
}
};
return request;
}
return super.buildRequest(method, url);
}
Aggregations