Search in sources :

Example 46 with MockLowLevelHttpResponse

use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project google-auth-library-java by google.

the class MockTokenCheckingTransport method makeSuccessResponse.

private MockLowLevelHttpResponse makeSuccessResponse() {
    MockLowLevelHttpResponse successResponse = new MockLowLevelHttpResponse();
    successResponse.addHeader("custom_header", "value");
    successResponse.setStatusCode(HttpStatusCodes.STATUS_CODE_OK);
    successResponse.setContentType(Json.MEDIA_TYPE);
    successResponse.setContent(SUCCESS_CONTENT);
    return successResponse;
}
Also used : MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse)

Example 47 with MockLowLevelHttpResponse

use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project google-auth-library-java by google.

the class MockMetadataServerTransport method buildRequest.

@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
    if (url.equals(ComputeEngineCredentials.getTokenServerEncodedUrl())) {
        return new MockLowLevelHttpRequest(url) {

            @Override
            public LowLevelHttpResponse execute() throws IOException {
                if (tokenRequestStatusCode != null) {
                    return new MockLowLevelHttpResponse().setStatusCode(tokenRequestStatusCode).setContent("Token Fetch Error");
                }
                String metadataRequestHeader = getFirstHeaderValue("Metadata-Flavor");
                if (!"Google".equals(metadataRequestHeader)) {
                    throw new IOException("Metadata request header not found.");
                }
                // Create the JSON response
                GenericJson refreshContents = new GenericJson();
                refreshContents.setFactory(OAuth2Utils.JSON_FACTORY);
                refreshContents.put("access_token", accessToken);
                refreshContents.put("expires_in", 3600000);
                refreshContents.put("token_type", "Bearer");
                String refreshText = refreshContents.toPrettyString();
                return new MockLowLevelHttpResponse().setContentType(Json.MEDIA_TYPE).setContent(refreshText);
            }
        };
    } else if (url.equals(ComputeEngineCredentials.getMetadataServerUrl())) {
        return new MockLowLevelHttpRequest(url) {

            @Override
            public LowLevelHttpResponse execute() {
                MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                response.addHeader("Metadata-Flavor", "Google");
                return response;
            }
        };
    }
    return super.buildRequest(method, url);
}
Also used : GenericJson(com.google.api.client.json.GenericJson) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) IOException(java.io.IOException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest)

Example 48 with MockLowLevelHttpResponse

use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project google-auth-library-java by google.

the class ServiceAccountCredentialsTest method refreshAccessToken_retriesServerError.

@Test
public void refreshAccessToken_retriesServerError() 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(500));
    transport.addServiceAccount(SA_CLIENT_EMAIL, accessToken2);
    credentials.refresh();
    TestUtils.assertContainsBearerToken(credentials.getRequestMetadata(CALL_URI), accessToken2);
}
Also used : MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) Test(org.junit.Test)

Example 49 with MockLowLevelHttpResponse

use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project endpoints-java by cloudendpoints.

the class GoogleAuthTest method constructHttpRequest.

private HttpRequest constructHttpRequest(final String content) throws IOException {
    HttpTransport transport = new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
                    result.setContentType("application/json");
                    result.setContent(content);
                    return result;
                }
            };
        }
    };
    return transport.createRequestFactory().buildGetRequest(new GenericUrl("https://google.com")).setParser(new JsonObjectParser(new JacksonFactory()));
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) JsonObjectParser(com.google.api.client.json.JsonObjectParser) GenericUrl(com.google.api.client.http.GenericUrl) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest)

Example 50 with MockLowLevelHttpResponse

use of com.google.api.client.testing.http.MockLowLevelHttpResponse in project beam by apache.

the class PackageUtilTest method googleJsonResponseException.

/**
 * Builds a fake GoogleJsonResponseException for testing API error handling.
 */
private static GoogleJsonResponseException googleJsonResponseException(final int status, final String reason, final String message) throws IOException {
    final JsonFactory jsonFactory = new JacksonFactory();
    HttpTransport transport = new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.setReason(reason);
            errorInfo.setMessage(message);
            errorInfo.setFactory(jsonFactory);
            GenericJson error = new GenericJson();
            error.set("code", status);
            error.set("errors", Arrays.asList(errorInfo));
            error.setFactory(jsonFactory);
            GenericJson errorResponse = new GenericJson();
            errorResponse.set("error", error);
            errorResponse.setFactory(jsonFactory);
            return new MockLowLevelHttpRequest().setResponse(new MockLowLevelHttpResponse().setContent(errorResponse.toPrettyString()).setContentType(Json.MEDIA_TYPE).setStatusCode(status));
        }
    };
    HttpRequest request = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
    request.setThrowExceptionOnExecuteError(false);
    HttpResponse response = request.execute();
    return GoogleJsonResponseException.from(jsonFactory, response);
}
Also used : GenericJson(com.google.api.client.json.GenericJson) LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) HttpTransport(com.google.api.client.http.HttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) ErrorInfo(com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo) JsonFactory(com.google.api.client.json.JsonFactory) HttpResponse(com.google.api.client.http.HttpResponse) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest)

Aggregations

MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)53 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)35 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)32 Test (org.junit.Test)28 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)22 IOException (java.io.IOException)20 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)18 HttpTransport (com.google.api.client.http.HttpTransport)9 GenericJson (com.google.api.client.json.GenericJson)9 HttpResponse (com.google.api.client.http.HttpResponse)8 JsonFactory (com.google.api.client.json.JsonFactory)7 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)7 HttpRequest (com.google.api.client.http.HttpRequest)5 Objectify (com.googlecode.objectify.Objectify)5 GenericUrl (com.google.api.client.http.GenericUrl)4 Storage (com.google.api.services.storage.Storage)4 ErrorInfo (com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo)3 MockGoogleClient (com.google.api.client.googleapis.testing.services.MockGoogleClient)3 MockGoogleClientRequest (com.google.api.client.googleapis.testing.services.MockGoogleClientRequest)3 MockTokenServerTransportFactory (com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory)3