use of com.google.api.client.http.LowLevelHttpResponse 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.http.LowLevelHttpResponse 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.http.LowLevelHttpResponse 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.http.LowLevelHttpResponse 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.http.LowLevelHttpResponse 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