Search in sources :

Example 6 with MockGoogleClientRequest

use of com.google.api.client.googleapis.testing.services.MockGoogleClientRequest in project google-api-java-client by google.

the class AbstractGoogleClientRequestTest method testUserAgentSuffix.

public void testUserAgentSuffix() throws Exception {
    AssertUserAgentTransport transport = new AssertUserAgentTransport();
    // Specify an Application Name.
    String applicationName = "Test Application";
    transport.expectedUserAgent = applicationName + " " + AbstractGoogleClientRequest.USER_AGENT_SUFFIX + " " + HttpRequest.USER_AGENT_SUFFIX;
    MockGoogleClient client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).setApplicationName(applicationName).build();
    MockGoogleClientRequest<Void> request = new MockGoogleClientRequest<Void>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
    request.executeUnparsed();
    // Don't specify an Application Name.
    transport.expectedUserAgent = AbstractGoogleClientRequest.USER_AGENT_SUFFIX + " " + HttpRequest.USER_AGENT_SUFFIX;
    client = new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).build();
    request = new MockGoogleClientRequest<Void>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
    request.executeUnparsed();
}
Also used : MockGoogleClient(com.google.api.client.googleapis.testing.services.MockGoogleClient) MockGoogleClientRequest(com.google.api.client.googleapis.testing.services.MockGoogleClientRequest)

Example 7 with MockGoogleClientRequest

use of com.google.api.client.googleapis.testing.services.MockGoogleClientRequest 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 8 with MockGoogleClientRequest

use of com.google.api.client.googleapis.testing.services.MockGoogleClientRequest 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)

Example 9 with MockGoogleClientRequest

use of com.google.api.client.googleapis.testing.services.MockGoogleClientRequest 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 10 with MockGoogleClientRequest

use of com.google.api.client.googleapis.testing.services.MockGoogleClientRequest 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)

Aggregations

MockGoogleClientRequest (com.google.api.client.googleapis.testing.services.MockGoogleClientRequest)12 MockGoogleClient (com.google.api.client.googleapis.testing.services.MockGoogleClient)8 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)6 HttpTransport (com.google.api.client.http.HttpTransport)5 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)5 InputStreamContent (com.google.api.client.http.InputStreamContent)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 HttpRequest (com.google.api.client.http.HttpRequest)3 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)3 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)3 GenericUrl (com.google.api.client.http.GenericUrl)2 JsonObjectParser (com.google.api.client.json.JsonObjectParser)2 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)2 ProtoObjectParser (com.google.api.client.protobuf.ProtoObjectParser)2 ObjectParser (com.google.api.client.util.ObjectParser)2 EmptyContent (com.google.api.client.http.EmptyContent)1 HttpResponseException (com.google.api.client.http.HttpResponseException)1