Search in sources :

Example 66 with MockHttpTransport

use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.

the class AbstractGoogleClientRequestTest method testCheckRequiredParameter.

public void testCheckRequiredParameter() 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, HttpMethods.GET, URI_TEMPLATE, null, String.class);
    // Should not throw an Exception.
    request.checkRequiredParameter("Not Null", "notNull()");
    try {
        request.checkRequiredParameter(null, "content.getTest().getAnotherTest()");
        fail("Expected " + IllegalArgumentException.class);
    } catch (IllegalArgumentException iae) {
    // Expected.
    }
}
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) MockGoogleClient(com.google.api.client.googleapis.testing.services.MockGoogleClient) MockGoogleClientRequest(com.google.api.client.googleapis.testing.services.MockGoogleClientRequest)

Example 67 with MockHttpTransport

use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.

the class AbstractGoogleClientRequestTest method testExecuteUsingHead.

public void testExecuteUsingHead() throws Exception {
    HttpTransport transport = new MockHttpTransport() {

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

                @Override
                public LowLevelHttpResponse execute() {
                    assertEquals("HEAD", method);
                    assertEquals("https://www.googleapis.com/test/path/v1/tests/foo", url);
                    return new MockLowLevelHttpResponse();
                }
            };
        }
    };
    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);
    request.put("testId", "foo");
    request.executeUsingHead();
}
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) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest)

Example 68 with MockHttpTransport

use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.

the class AbstractGoogleClientRequestTest method testExecute_void.

public void testExecute_void() throws Exception {
    HttpTransport transport = new MockHttpTransport() {

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

                @Override
                public LowLevelHttpResponse execute() {
                    return new MockLowLevelHttpResponse().setContent("{\"a\":\"ignored\"}").setContentType(Json.MEDIA_TYPE);
                }
            };
        }
    };
    MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName("Test Application").build();
    MockGoogleClientRequest<Void> request = new MockGoogleClientRequest<Void>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
    Void v = request.execute();
    assertNull(v);
}
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) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest)

Example 69 with MockHttpTransport

use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.

the class MethodOverrideTest method testInterceptMaxLength.

public void testInterceptMaxLength() throws IOException {
    HttpTransport transport = new MockHttpTransport();
    GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL);
    url.set("a", "foo");
    HttpRequest request = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
    new MethodOverride().intercept(request);
    assertEquals(HttpMethods.GET, request.getRequestMethod());
    assertNull(request.getHeaders().get(MethodOverride.HEADER));
    assertNull(request.getContent());
    char[] arr = new char[MethodOverride.MAX_URL_LENGTH];
    Arrays.fill(arr, 'x');
    url.set("a", new String(arr));
    request.setUrl(url);
    new MethodOverride().intercept(request);
    assertEquals(HttpMethods.POST, request.getRequestMethod());
    assertEquals(HttpMethods.GET, request.getHeaders().get(MethodOverride.HEADER));
    assertEquals(HttpTesting.SIMPLE_GENERIC_URL, request.getUrl());
    char[] arr2 = new char[arr.length + 2];
    Arrays.fill(arr2, 'x');
    arr2[0] = 'a';
    arr2[1] = '=';
    UrlEncodedContent content = (UrlEncodedContent) request.getContent();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    content.writeTo(out);
    assertEquals(new String(arr2), out.toString());
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) UrlEncodedContent(com.google.api.client.http.UrlEncodedContent) GenericUrl(com.google.api.client.http.GenericUrl) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 70 with MockHttpTransport

use of com.google.api.client.testing.http.MockHttpTransport in project google-api-java-client by google.

the class MethodOverrideTest method testIntercept.

public void testIntercept() throws Exception {
    subtestIntercept(ImmutableSet.<String>of(), new MockHttpTransport(), new MethodOverride());
    subtestIntercept(OVERRIDDEN_METHODS, new MockHttpTransport(), new MethodOverride.Builder().setOverrideAllMethods(true).build());
    subtestIntercept(OVERRIDDEN_METHODS, new MockHttpTransport.Builder().setSupportedMethods(ImmutableSet.<String>of(HttpMethods.GET, HttpMethods.POST)).build(), new MethodOverride());
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport)

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