Search in sources :

Example 71 with MockHttpTransport

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

the class CommonGoogleProtoClientRequestInitializerTest method testInitialize.

public void testInitialize() throws Exception {
    CommonGoogleProtoClientRequestInitializer key = new CommonGoogleProtoClientRequestInitializer("foo");
    MockGoogleProtoClient client = new MockGoogleProtoClient.Builder(new MockHttpTransport(), HttpTesting.SIMPLE_URL, "test/", null).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) MockGoogleProtoClient(com.google.api.client.googleapis.testing.services.protobuf.MockGoogleProtoClient)

Example 72 with MockHttpTransport

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

use of com.google.api.client.testing.http.MockHttpTransport in project components by Talend.

the class GoogleDriveCredentialWithInstalledApplicationTest method testBuilder.

@Test
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) GoogleAuthorizationCodeFlow(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) Test(org.junit.Test)

Example 74 with MockHttpTransport

use of com.google.api.client.testing.http.MockHttpTransport in project ddf by codice.

the class PaosInInterceptorTest method getHttpUnsuccessfulResponseHandlerHeaderTest.

@Test
public void getHttpUnsuccessfulResponseHandlerHeaderTest() throws IOException {
    Message message = new MessageImpl();
    message.put(Message.HTTP_REQUEST_METHOD, "GET");
    HashMap<String, List> protocolHeaders = new HashMap<>();
    message.put(Message.PROTOCOL_HEADERS, protocolHeaders);
    protocolHeaders.put("X-Custom-Header", Collections.singletonList("Custom"));
    PaosInInterceptor paosInInterceptor = spy(new PaosInInterceptor(Phase.RECEIVE, new SamlSecurity()));
    doReturn(true).when(paosInInterceptor).isRedirect(any(HttpRequest.class), any(HttpResponse.class), any(String.class));
    GenericUrl url = new GenericUrl("https://localhost:8993/PAOSConsumer");
    HttpRequest request = new MockHttpTransport().createRequestFactory().buildGetRequest(url);
    request.getUrl().set("url", "https://localhost:8993/PAOSConsumer");
    // Using request.execute to create an HttpResponse since it's final and cannot be mocked
    HttpResponse response = request.execute();
    response.getHeaders().setLocation("https://localhost:8993/PAOSConsumer");
    response.getHeaders().set("set-cookie", Collections.singletonList("cookie"));
    HttpUnsuccessfulResponseHandler responseHandler = paosInInterceptor.getHttpUnsuccessfulResponseHandler(message);
    boolean returned = responseHandler.handleResponse(request, response, true);
    assertThat(returned, is(true));
    // HttpHeaders ignores header case
    assertThat(request.getHeaders().containsKey("x-custom-header"), is(true));
    assertThat(request.getHeaders().get("x-custom-header"), is(Collections.singletonList("Custom")));
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) Message(org.apache.cxf.message.Message) HashMap(java.util.HashMap) HttpResponse(com.google.api.client.http.HttpResponse) HttpUnsuccessfulResponseHandler(com.google.api.client.http.HttpUnsuccessfulResponseHandler) GenericUrl(com.google.api.client.http.GenericUrl) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) SamlSecurity(org.codice.ddf.security.jaxrs.impl.SamlSecurity) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 75 with MockHttpTransport

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

the class GcsUtilTest method testFileSizeWhenFileNotFoundNonBatch.

@Test
public void testFileSizeWhenFileNotFoundNonBatch() throws Exception {
    MockLowLevelHttpResponse notFoundResponse = new MockLowLevelHttpResponse();
    notFoundResponse.setContent("");
    notFoundResponse.setStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND);
    MockHttpTransport mockTransport = new MockHttpTransport.Builder().setLowLevelHttpResponse(notFoundResponse).build();
    GcsOptions pipelineOptions = gcsOptionsWithTestCredential();
    GcsUtil gcsUtil = pipelineOptions.getGcsUtil();
    gcsUtil.setStorageClient(new Storage(mockTransport, Transport.getJsonFactory(), null));
    thrown.expect(FileNotFoundException.class);
    gcsUtil.fileSize(GcsPath.fromComponents("testbucket", "testobject"));
}
Also used : MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) Storage(com.google.api.services.storage.Storage) GoogleCloudStorage(com.google.cloud.hadoop.gcsio.GoogleCloudStorage) GcsOptions(org.apache.beam.sdk.extensions.gcp.options.GcsOptions) Test(org.junit.Test)

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