Search in sources :

Example 31 with MockLowLevelHttpRequest

use of com.google.api.client.testing.http.MockLowLevelHttpRequest in project java-docs-samples by GoogleCloudPlatform.

the class LocalUrlFetchTest method testMockUrlFetch.

@Test
public void testMockUrlFetch() throws IOException {
    // See http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing
    MockHttpTransport mockHttpTransport = new MockHttpTransport() {

        @Override
        public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
            assertEquals(method, "GET");
            assertEquals(url, "http://foo.bar");
            return new MockLowLevelHttpRequest() {

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(234);
                    return response;
                }
            };
        }
    };
    HttpRequestFactory requestFactory = mockHttpTransport.createRequestFactory();
    HttpResponse response = requestFactory.buildGetRequest(new GenericUrl("http://foo.bar")).execute();
    assertEquals(response.getStatusCode(), 234);
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 32 with MockLowLevelHttpRequest

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

the class BatchRequestTest method subtestExecute_checkWriteTo.

private void subtestExecute_checkWriteTo(final String expectedOutput, HttpRequest... requests) throws IOException {
    MockHttpTransport transport = new MockHttpTransport() {

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

                @Override
                public LowLevelHttpResponse execute() throws IOException {
                    assertEquals("multipart/mixed; boundary=__END_OF_PART__", getContentType());
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    getStreamingContent().writeTo(out);
                    assertEquals(expectedOutput, out.toString("UTF-8"));
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                    response.setStatusCode(200);
                    response.addHeader("Content-Type", "multipart/mixed; boundary=" + RESPONSE_BOUNDARY);
                    String content2 = "{\"name\": \"" + TEST_NAME + "\", \"number\": \"" + TEST_NUM + "\"}";
                    StringBuilder responseContent = new StringBuilder();
                    responseContent.append("--" + RESPONSE_BOUNDARY + "\n").append("Content-Type: application/http\n").append("Content-Transfer-Encoding: binary\n").append("Content-ID: response-1\n\n").append("HTTP/1.1 200 OK\n").append("Content-Type: application/json; charset=UTF-8\n").append("Content-Length: " + content2.length() + "\n\n").append(content2 + "\n\n").append("--" + RESPONSE_BOUNDARY + "--\n\n");
                    response.setContent(responseContent.toString());
                    return response;
                }
            };
        }
    };
    BatchRequest batchRequest = new BatchRequest(transport, null);
    BatchCallback<Void, Void> callback = new BatchCallback<Void, Void>() {

        @Override
        public void onSuccess(Void t, HttpHeaders responseHeaders) {
        }

        @Override
        public void onFailure(Void e, HttpHeaders responseHeaders) {
        }
    };
    for (HttpRequest request : requests) {
        batchRequest.queue(request, Void.class, Void.class, callback);
    }
    batchRequest.execute();
}
Also used : LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) HttpRequest(com.google.api.client.http.HttpRequest) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest)

Example 33 with MockLowLevelHttpRequest

use of com.google.api.client.testing.http.MockLowLevelHttpRequest 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 34 with MockLowLevelHttpRequest

use of com.google.api.client.testing.http.MockLowLevelHttpRequest 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 35 with MockLowLevelHttpRequest

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

the class MockMetadataServerTransport method buildRequest.

@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
    if (url.equals(METADATA_TOKEN_SERVER_URL)) {
        MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url) {

            @Override
            public LowLevelHttpResponse execute() throws IOException {
                if (tokenRequestStatusCode != null) {
                    MockLowLevelHttpResponse response = new MockLowLevelHttpResponse().setStatusCode(tokenRequestStatusCode).setContent("Token Fetch Error");
                    return response;
                }
                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(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;
    } else if (url.equals(METADATA_SERVER_URL)) {
        MockLowLevelHttpRequest request = new MockLowLevelHttpRequest(url) {

            @Override
            public LowLevelHttpResponse execute() {
                MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
                response.addHeader("Metadata-Flavor", "Google");
                return response;
            }
        };
        return request;
    }
    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)

Aggregations

MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)46 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)35 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)33 IOException (java.io.IOException)27 Test (org.junit.Test)25 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)23 LowLevelHttpRequest (com.google.api.client.http.LowLevelHttpRequest)18 GenericJson (com.google.api.client.json.GenericJson)10 HttpTransport (com.google.api.client.http.HttpTransport)9 JsonFactory (com.google.api.client.json.JsonFactory)8 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)8 GitApiMockHttpTransport (com.google.copybara.testing.OptionsBuilder.GitApiMockHttpTransport)8 DummyRevision (com.google.copybara.testing.DummyRevision)6 Storage (com.google.api.services.storage.Storage)5 Objectify (com.googlecode.objectify.Objectify)5 HttpRequest (com.google.api.client.http.HttpRequest)4 HttpResponse (com.google.api.client.http.HttpResponse)4 Before (org.junit.Before)4 ErrorInfo (com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo)3 MockGoogleClient (com.google.api.client.googleapis.testing.services.MockGoogleClient)3