use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project copybara by google.
the class GerritApiTest method setUp.
@Before
public void setUp() throws Exception {
OptionsBuilder options = new OptionsBuilder().setWorkdirToRealTempDir().setEnvironment(GitTestUtil.getGitEnv()).setOutputRootToTmpDir();
credentialsFile = Files.createTempFile("credentials", "test");
Files.write(credentialsFile, "https://user:SECRET@copybara-not-real.com".getBytes(UTF_8));
GitRepository repo = newBareRepo(Files.createTempDirectory("test_repo"), getGitEnv(), /*verbose=*/
true).init().withCredentialHelper("store --file=" + credentialsFile);
httpTransport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
String requestString = method + " " + url;
MockLowLevelHttpRequest request = new MockLowLevelHttpRequest();
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
request.setResponse(response);
for (Entry<Predicate<String>, byte[]> entry : requestToResponse.entrySet()) {
if (entry.getKey().test(requestString)) {
byte[] content = entry.getValue();
assertWithMessage("'" + method + " " + url + "'").that(content).isNotNull();
response.setContent(content);
return request;
}
}
response.setStatusCode(404);
response.setContent(("NO URL MATCHED! (Returning 404) REQUEST: " + requestString));
return request;
}
};
GerritOptions gerritOptions = new GerritOptions(() -> options.general, options.git) {
@Override
protected HttpTransport getHttpTransport() {
return httpTransport;
}
@Override
protected GitRepository getCredentialsRepo() throws RepoException {
return repo;
}
};
gerritApi = gerritOptions.newGerritApi(getHost() + "/foo/bar/baz");
}
use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project copybara by google.
the class GithubApiTest method getTransport.
@Override
public GitHubApiTransport getTransport() throws Exception {
credentialsFile = Files.createTempFile("credentials", "test");
Files.write(credentialsFile, "https://user:SECRET@github.com".getBytes(UTF_8));
GitRepository repo = newBareRepo(Files.createTempDirectory("test_repo"), getGitEnv(), /*verbose=*/
true).init().withCredentialHelper("store --file=" + credentialsFile);
requestToResponse = new HashMap<>();
requestValidators = new HashMap<>();
httpTransport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
String requestString = method + " " + url;
MockLowLevelHttpRequest request = new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
Predicate<String> validator = requestValidators.get(method + " " + url);
if (validator != null) {
assertWithMessage("Request content did not match expected values.").that(validator.test(getContentAsString())).isTrue();
}
return super.execute();
}
};
byte[] content = requestToResponse.get(requestString);
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
if (content == null) {
response.setContent(String.format("{ 'message' : 'This is not the repo you are looking for! %s %s'," + " 'documentation_url' : 'http://github.com/some_url'}", method, url));
response.setStatusCode(404);
} else {
response.setContent(content);
}
request.setResponse(response);
return request;
}
};
return new GitHubApiTransportImpl(repo, httpTransport, "some_storage_file", new TestingConsole());
}
use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project java-docs-samples by GoogleCloudPlatform.
the class FirebaseChannelTest method firebaseDelete.
@Test
public void firebaseDelete() 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.firebaseDelete(FIREBASE_DB_URL + "/my/path");
verify(mockHttpTransport, times(1)).buildRequest("DELETE", FIREBASE_DB_URL + "/my/path");
}
use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project java-docs-samples by GoogleCloudPlatform.
the class FirebaseChannelTest method firebaseGet.
@Test
public void firebaseGet() 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.firebaseGet(FIREBASE_DB_URL + "/my/path");
verify(mockHttpTransport, times(1)).buildRequest("GET", FIREBASE_DB_URL + "/my/path");
}
use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project java-docs-samples by GoogleCloudPlatform.
the class FirebaseChannelTest method firebasePatch.
@Test
public void firebasePatch() 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.firebasePatch(FIREBASE_DB_URL + "/my/path", game);
verify(mockHttpTransport, times(1)).buildRequest("PATCH", FIREBASE_DB_URL + "/my/path");
}
Aggregations