use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project google-auth-library-java by google.
the class ServiceAccountCredentialsTest method refreshAccessToken_retriesForbiddenError.
@Test
public void refreshAccessToken_retriesForbiddenError() throws IOException {
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2";
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
MockTokenServerTransport transport = transportFactory.transport;
ServiceAccountCredentials credentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, transportFactory, null);
transport.addServiceAccount(SA_CLIENT_EMAIL, accessToken1);
TestUtils.assertContainsBearerToken(credentials.getRequestMetadata(CALL_URI), accessToken1);
transport.addResponseSequence(new MockLowLevelHttpResponse().setStatusCode(403));
transport.addServiceAccount(SA_CLIENT_EMAIL, accessToken2);
credentials.refresh();
TestUtils.assertContainsBearerToken(credentials.getRequestMetadata(CALL_URI), accessToken2);
}
use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project google-auth-library-java by google.
the class MockTokenCheckingTransport method makeErrorResponse.
private MockLowLevelHttpResponse makeErrorResponse() {
MockLowLevelHttpResponse errorResponse = new MockLowLevelHttpResponse();
errorResponse.addHeader("custom_header", "value");
errorResponse.setStatusCode(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED);
errorResponse.setContentType(Json.MEDIA_TYPE);
errorResponse.setContent("{\"error\":\"invalid credentials\"}");
return errorResponse;
}
use of com.google.api.client.testing.http.MockLowLevelHttpResponse 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.MockLowLevelHttpResponse 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.MockLowLevelHttpResponse in project google-api-java-client by google.
the class MockTokenServerTransport method buildRequest.
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
if (url.equals(tokenServerUrl)) {
MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url) {
@Override
public LowLevelHttpResponse execute() throws IOException {
String content = this.getContentAsString();
Map<String, String> query = TestUtils.parseQuery(content);
String accessToken = null;
String foundId = query.get("client_id");
if (foundId != null) {
if (!clients.containsKey(foundId)) {
throw new IOException("Client ID not found.");
}
String foundSecret = query.get("client_secret");
String expectedSecret = clients.get(foundId);
if (foundSecret == null || !foundSecret.equals(expectedSecret)) {
throw new IOException("Client secret not found.");
}
String foundRefresh = query.get("refresh_token");
if (!refreshTokens.containsKey(foundRefresh)) {
throw new IOException("Refresh Token not found.");
}
accessToken = refreshTokens.get(foundRefresh);
} else if (query.containsKey("grant_type")) {
String grantType = query.get("grant_type");
if (!EXPECTED_GRANT_TYPE.equals(grantType)) {
throw new IOException("Unexpected Grant Type.");
}
String assertion = query.get("assertion");
JsonWebSignature signature = JsonWebSignature.parse(JSON_FACTORY, assertion);
String foundEmail = signature.getPayload().getIssuer();
if (!serviceAccounts.containsKey(foundEmail)) {
throw new IOException("Service Account Email not found as issuer.");
}
accessToken = serviceAccounts.get(foundEmail);
String foundScopes = (String) signature.getPayload().get("scope");
if (foundScopes == null || foundScopes.length() == 0) {
throw new IOException("Scopes not found.");
}
} else {
throw new IOException("Unknown token type.");
}
// 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;
}
return super.buildRequest(method, url);
}
Aggregations