use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.
the class CommonGoogleClientRequestInitializerTest method testInitialize.
public void testInitialize() throws Exception {
CommonGoogleClientRequestInitializer key = new CommonGoogleClientRequestInitializer("foo");
MockGoogleClient client = new MockGoogleClient.Builder(new MockHttpTransport(), HttpTesting.SIMPLE_URL, "test/", null, null).setApplicationName("Test Application").build();
MyRequest request = new MyRequest(client, "GET", "", null, String.class);
assertNull(request.key);
key.initialize(request);
assertEquals("foo", request.key);
}
use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.
the class GoogleJsonResponseExceptionFactoryTesting method newMock.
/**
* Convenience factory method that builds a {@link GoogleJsonResponseException}
* from its arguments. The method builds a dummy {@link HttpRequest} and
* {@link HttpResponse}, sets the response's status to a user-specified HTTP
* error code, suppresses exceptions, and executes the request. This forces
* the underlying framework to create, but not throw, a
* {@link GoogleJsonResponseException}, which the method retrieves and returns
* to the invoker.
*
* @param jsonFactory the JSON factory that will create all JSON required
* by the underlying framework
* @param httpCode the desired HTTP error code. Note: do nut specify any codes
* that indicate successful completion, e.g. 2XX.
* @param reasonPhrase the HTTP reason code that explains the error. For example,
* if {@code httpCode} is {@code 404}, the reason phrase should be
* {@code NOT FOUND}.
* @return the generated {@link GoogleJsonResponseException}, as specified.
* @throws IOException if request transport fails.
*/
public static GoogleJsonResponseException newMock(JsonFactory jsonFactory, int httpCode, String reasonPhrase) throws IOException {
MockLowLevelHttpResponse otherServiceUnavaiableLowLevelResponse = new MockLowLevelHttpResponse().setStatusCode(httpCode).setReasonPhrase(reasonPhrase);
MockHttpTransport otherTransport = new MockHttpTransport.Builder().setLowLevelHttpResponse(otherServiceUnavaiableLowLevelResponse).build();
HttpRequest otherRequest = otherTransport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
otherRequest.setThrowExceptionOnExecuteError(false);
HttpResponse otherServiceUnavailableResponse = otherRequest.execute();
return GoogleJsonResponseException.from(jsonFactory, otherServiceUnavailableResponse);
}
use of com.google.api.client.testing.http.MockHttpTransport 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.MockHttpTransport 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.MockHttpTransport 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());
}
Aggregations