use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.
the class AbstractGoogleJsonClientTest method testExecuteUnparsed_error.
public void testExecuteUnparsed_error() throws Exception {
HttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String name, String url) {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() {
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
result.setStatusCode(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED);
result.setContentType(Json.MEDIA_TYPE);
result.setContent("{\"error\":{\"code\":401,\"errors\":[{\"domain\":\"global\"," + "\"location\":\"Authorization\",\"locationType\":\"header\"," + "\"message\":\"me\",\"reason\":\"authError\"}],\"message\":\"me\"}}");
return result;
}
};
}
};
JsonFactory jsonFactory = new JacksonFactory();
MockGoogleJsonClient client = new MockGoogleJsonClient.Builder(transport, jsonFactory, HttpTesting.SIMPLE_URL, "", null, false).setApplicationName("Test Application").build();
MockGoogleJsonClientRequest<String> request = new MockGoogleJsonClientRequest<String>(client, "GET", "foo", null, String.class);
try {
request.executeUnparsed();
fail("expected " + GoogleJsonResponseException.class);
} catch (GoogleJsonResponseException e) {
// expected
GoogleJsonError details = e.getDetails();
assertEquals("me", details.getMessage());
assertEquals("me", details.getErrors().get(0).getMessage());
}
}
use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.
the class CommonGoogleJsonClientRequestInitializerTest method testInitialize.
public void testInitialize() throws Exception {
CommonGoogleJsonClientRequestInitializer key = new CommonGoogleJsonClientRequestInitializer("foo");
MockGoogleJsonClient client = new MockGoogleJsonClient.Builder(new MockHttpTransport(), new JacksonFactory(), HttpTesting.SIMPLE_URL, "test/", null, false).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 GoogleAuthorizationCodeFlowTest method testBuilder.
public void testBuilder() {
GoogleAuthorizationCodeFlow.Builder builder = new GoogleAuthorizationCodeFlow.Builder(new MockHttpTransport(), new JacksonFactory(), CLIENT_ID, CLIENT_SECRET, ImmutableList.of("https://www.googleapis.com/auth/userinfo.email"));
assertNull(builder.getApprovalPrompt());
assertNull(builder.getAccessType());
}
use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.
the class AbstractGoogleClientRequestTest method testExecuteUnparsed_error.
public void testExecuteUnparsed_error() throws Exception {
HttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(final String method, final String url) {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() {
assertEquals("GET", method);
assertEquals("https://www.googleapis.com/test/path/v1/tests/foo", url);
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
result.setStatusCode(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED);
result.setContentType(Json.MEDIA_TYPE);
result.setContent(ERROR_CONTENT);
return result;
}
};
}
};
MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName("Test Application").build();
MockGoogleClientRequest<String> request = new MockGoogleClientRequest<String>(client, HttpMethods.GET, URI_TEMPLATE, null, String.class);
try {
request.put("testId", "foo");
request.executeUnparsed();
fail("expected " + HttpResponseException.class);
} catch (HttpResponseException e) {
// expected
assertEquals("401" + StringUtils.LINE_SEPARATOR + ERROR_CONTENT, e.getMessage());
}
}
use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.
the class AbstractGoogleClientRequestTest method subtestBuildHttpRequest_emptyContent.
private void subtestBuildHttpRequest_emptyContent(String method, boolean expectEmptyContent) throws Exception {
HttpTransport transport = new MockHttpTransport();
MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName("Test Application").build();
MockGoogleClientRequest<String> request = new MockGoogleClientRequest<String>(client, method, URI_TEMPLATE, null, String.class);
HttpRequest httpRequest = request.buildHttpRequest();
if (expectEmptyContent) {
assertTrue(httpRequest.getContent() instanceof EmptyContent);
} else {
assertNull(httpRequest.getContent());
}
}
Aggregations