Search in sources :

Example 16 with MockHttpTransport

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());
    }
}
Also used : MockGoogleJsonClientRequest(com.google.api.client.googleapis.testing.services.json.MockGoogleJsonClientRequest) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) JsonFactory(com.google.api.client.json.JsonFactory) MockGoogleJsonClient(com.google.api.client.googleapis.testing.services.json.MockGoogleJsonClient) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

Example 17 with MockHttpTransport

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);
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockGoogleJsonClient(com.google.api.client.googleapis.testing.services.json.MockGoogleJsonClient) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory)

Example 18 with MockHttpTransport

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());
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory)

Example 19 with MockHttpTransport

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());
    }
}
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) MockGoogleClient(com.google.api.client.googleapis.testing.services.MockGoogleClient) MockGoogleClientRequest(com.google.api.client.googleapis.testing.services.MockGoogleClientRequest) HttpResponseException(com.google.api.client.http.HttpResponseException) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest)

Example 20 with MockHttpTransport

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());
    }
}
Also used : LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockGoogleClient(com.google.api.client.googleapis.testing.services.MockGoogleClient) MockGoogleClientRequest(com.google.api.client.googleapis.testing.services.MockGoogleClientRequest) EmptyContent(com.google.api.client.http.EmptyContent)

Aggregations

MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)83 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)44 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)42 Test (org.junit.Test)32 HttpTransport (com.google.api.client.http.HttpTransport)30 IOException (java.io.IOException)29 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)26 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)23 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)19 HttpRequest (com.google.api.client.http.HttpRequest)14 JsonFactory (com.google.api.client.json.JsonFactory)13 GenericJson (com.google.api.client.json.GenericJson)12 Storage (com.google.api.services.storage.Storage)9 HttpResponse (com.google.api.client.http.HttpResponse)7 MockGoogleClient (com.google.api.client.googleapis.testing.services.MockGoogleClient)6 GenericUrl (com.google.api.client.http.GenericUrl)6 MockGoogleClientRequest (com.google.api.client.googleapis.testing.services.MockGoogleClientRequest)5 Objectify (com.googlecode.objectify.Objectify)5 MockTokenServerTransport (com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)4 ByteArrayContent (com.google.api.client.http.ByteArrayContent)4