use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.
the class FirebaseChannelTest method sendFirebaseMessage_create.
@Test
public void sendFirebaseMessage_create() 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", new Game());
verify(mockHttpTransport, times(1)).buildRequest("PATCH", FIREBASE_DB_URL + "/channels/my_key.json");
}
use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.
the class FirebaseChannelTest method firebasePut.
@Test
public void firebasePut() 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;
Game game = new Game();
firebaseChannel.firebasePut(FIREBASE_DB_URL + "/my/path", game);
verify(mockHttpTransport, times(1)).buildRequest("PUT", FIREBASE_DB_URL + "/my/path");
}
use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project java-docs-samples by GoogleCloudPlatform.
the class TicTacToeServletTest method doGet_existingGame.
@Test
public void doGet_existingGame() 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;
// Insert a game
Objectify ofy = ObjectifyService.ofy();
Game game = new Game("some-other-user-id", null, " ", true);
ofy.save().entity(game).now();
String gameKey = game.getId();
when(mockRequest.getParameter("gameKey")).thenReturn(gameKey);
Mockito.doReturn(null).when(servletUnderTest).getServletContext();
servletUnderTest.doGet(mockRequest, mockResponse);
// Make sure the game object was updated with the other player
game = ofy.load().type(Game.class).first().safe();
assertEquals(game.userX, "some-other-user-id");
assertEquals(game.userO, USER_ID);
verify(mockHttpTransport, times(2)).buildRequest(eq("PATCH"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$"));
verify(requestDispatcher).forward(mockRequest, mockResponse);
verify(mockRequest).setAttribute(eq("token"), anyString());
verify(mockRequest).setAttribute("game_key", game.id);
verify(mockRequest).setAttribute("me", USER_ID);
verify(mockRequest).setAttribute("channel_id", USER_ID + gameKey);
verify(mockRequest).setAttribute(eq("initial_message"), anyString());
verify(mockRequest).setAttribute(eq("game_link"), anyString());
}
use of com.google.api.client.testing.http.MockLowLevelHttpResponse 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.MockLowLevelHttpResponse 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