use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project copybara by google.
the class GitApiMockHttpTransport method buildRequest.
@Override
public LowLevelHttpRequest buildRequest(String method, String url) {
MockLowLevelHttpRequest request = new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = (MockLowLevelHttpResponse) super.execute();
String content = getContent(method, url, this);
requests.add(new RequestRecord(method, url, this.getContentAsString(), content));
response.setContent(content.getBytes(StandardCharsets.UTF_8));
return super.execute();
}
};
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setContentType(Json.MEDIA_TYPE);
request.setResponse(response);
return request;
}
use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project copybara by google.
the class GitHubApiTransportImplTest method testPasswordHeaderSet.
@Test
public void testPasswordHeaderSet() throws Exception {
Map<String, List<String>> headers = new HashMap<>();
httpTransport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
headers.putAll(this.getHeaders());
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.setContent("foo");
return response;
}
};
}
};
transport = new GitHubApiTransportImpl(repo, httpTransport, "store", new TestingConsole());
transport.get("foo/bar", String.class);
assertThat(headers).containsEntry("authorization", ImmutableList.of("Basic dXNlcjpTRUNSRVQ="));
}
use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project copybara by google.
the class GerritApiTest method setUp.
@Before
public void setUp() throws Exception {
OptionsBuilder options = new OptionsBuilder().setWorkdirToRealTempDir().setEnvironment(GitTestUtil.getGitEnv().getEnvironment()).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, DEFAULT_TIMEOUT, /*noVerify=*/
false).init().withCredentialHelper("store --file=" + credentialsFile);
httpTransport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) {
String requestString = method + " " + url;
MockLowLevelHttpRequest request = new MockLowLevelHttpRequest();
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
request.setResponse(response);
apiCalled = new AtomicBoolean(false);
for (Entry<Predicate<String>, byte[]> entry : requestToResponse.entrySet()) {
if (entry.getKey().test(requestString)) {
apiCalled.set(true);
byte[] content = entry.getValue();
assertWithMessage("'" + method + " " + url + "'").that(content).isNotNull();
if (content.length == 0) {
// No content
response.setStatusCode(204);
return request;
}
response.setContent(content);
return request;
}
}
response.setStatusCode(404);
response.setContent(("NO BASE_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() {
return repo;
}
};
gerritApi = gerritOptions.newGerritApi(getHost() + "/foo/bar/baz");
}
use of com.google.api.client.testing.http.MockLowLevelHttpResponse 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, DEFAULT_TIMEOUT, /*noVerify=*/
false).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 {
System.err.println(getContentAsString());
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();
}
};
MockLowLevelHttpResponse response = requestToResponse.get(requestString);
if (response == null) {
response = new MockLowLevelHttpResponse();
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);
}
request.setResponse(response);
return request;
}
};
return new GitHubApiTransportImpl(repo, httpTransport, "some_storage_file", new TestingConsole());
}
use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project copybara by google.
the class GitHubApiTest method trainMockGetWithHeaders.
@Override
public void trainMockGetWithHeaders(String apiPath, byte[] response, ImmutableMap<String, String> headers, int status) {
String path = String.format("GET https://api.github.com%s", apiPath);
MockLowLevelHttpResponse httpResponse = new MockLowLevelHttpResponse().setContent(response);
for (Entry<String, String> entry : headers.entrySet()) {
httpResponse.addHeader(entry.getKey(), entry.getValue());
}
httpResponse.setStatusCode(status);
requestToResponse.put(path, httpResponse);
requestValidators.put(path, (r) -> true);
}
Aggregations