use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project elasticsearch by elastic.
the class GceMockUtils method configureMock.
protected static HttpTransport configureMock() {
return new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, final String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(200);
response.setContentType(Json.MEDIA_TYPE);
if (url.startsWith(GCE_METADATA_URL)) {
logger.info("--> Simulate GCE Auth/Metadata response for [{}]", url);
response.setContent(readGoogleInternalJsonResponse(url));
} else {
logger.info("--> Simulate GCE API response for [{}]", url);
response.setContent(readGoogleApiJsonResponse(url));
}
return response;
}
};
}
};
}
use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project beam by apache.
the class GcsUtilTest method testFileSizeWhenFileNotFoundNonBatch.
@Test
public void testFileSizeWhenFileNotFoundNonBatch() throws Exception {
MockLowLevelHttpResponse notFoundResponse = new MockLowLevelHttpResponse();
notFoundResponse.setContent("");
notFoundResponse.setStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
MockHttpTransport mockTransport = new MockHttpTransport.Builder().setLowLevelHttpResponse(notFoundResponse).build();
GcsOptions pipelineOptions = gcsOptionsWithTestCredential();
GcsUtil gcsUtil = pipelineOptions.getGcsUtil();
gcsUtil.setStorageClient(new Storage(mockTransport, Transport.getJsonFactory(), null));
thrown.expect(FileNotFoundException.class);
gcsUtil.fileSize(GcsPath.fromComponents("testbucket", "testobject"));
}
use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project beam by apache.
the class UploadIdResponseInterceptorTest method buildHttpResponse.
/**
* Builds a HttpResponse with the given string response.
*
* @param header header value to provide or null if none.
* @param uploadId upload id to provide in the url upload id param or null if none.
* @param uploadType upload type to provide in url upload type param or null if none.
* @return HttpResponse with the given parameters
* @throws IOException
*/
private HttpResponse buildHttpResponse(String header, String uploadId, String uploadType) throws IOException {
MockHttpTransport.Builder builder = new MockHttpTransport.Builder();
MockLowLevelHttpResponse resp = new MockLowLevelHttpResponse();
builder.setLowLevelHttpResponse(resp);
resp.setStatusCode(200);
GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL);
if (header != null) {
resp.addHeader("X-GUploader-UploadID", header);
}
if (uploadId != null) {
url.put("upload_id", uploadId);
}
if (uploadType != null) {
url.put("uploadType", uploadType);
}
return builder.build().createRequestFactory().buildGetRequest(url).execute();
}
use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.
the class DeleteServletTest method doPost_deleteGame.
@Test
public void doPost_deleteGame() throws Exception {
// Insert a game
Objectify ofy = ObjectifyService.ofy();
Game game = new Game(USER_ID, "my-opponent", " ", true);
ofy.save().entity(game).now();
String gameKey = game.getId();
when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);
// Mock out the firebase response. See
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(200);
return response;
}
};
}
});
FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
Mockito.doReturn(null).when(servletUnderTest).getServletContext();
servletUnderTest.doPost(mockRequest, mockResponse);
verify(mockHttpTransport, times(1)).buildRequest(eq("DELETE"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
}
use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.
the class FirebaseChannelTest method sendFirebaseMessage_delete.
@Test
public void sendFirebaseMessage_delete() throws Exception {
// Mock out the firebase response. See
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setStatusCode(200);
return response;
}
};
}
});
FirebaseChannel.getInstance(null).httpTransport = mockHttpTransport;
firebaseChannel.sendFirebaseMessage("my_key", null);
verify(mockHttpTransport, times(1)).buildRequest("DELETE", FIREBASE_DB_URL + "/channels/my_key.json");
}
Aggregations