use of com.google.api.client.testing.http.MockHttpTransport 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");
}
use of com.google.api.client.testing.http.MockHttpTransport 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.MockHttpTransport 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.MockHttpTransport 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.MockHttpTransport 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);
}
Aggregations