Search in sources :

Example 81 with MockHttpTransport

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

the class BatchJobUploaderTest method testUploadBatchJobOperations_initiateFails_fails.

/**
 * Tests that IOExceptions from initiating an upload are propagated properly.
 */
@SuppressWarnings("rawtypes")
@Test
public void testUploadBatchJobOperations_initiateFails_fails() throws Exception {
    final IOException ioException = new IOException("mock IO exception");
    MockLowLevelHttpRequest lowLevelHttpRequest = new MockLowLevelHttpRequest() {

        @Override
        public LowLevelHttpResponse execute() throws IOException {
            throw ioException;
        }
    };
    when(uploadBodyProvider.getHttpContent(request, true, true)).thenReturn(new ByteArrayContent(null, "foo".getBytes(UTF_8)));
    MockHttpTransport transport = new MockHttpTransport.Builder().setLowLevelHttpRequest(lowLevelHttpRequest).build();
    uploader = new BatchJobUploader(adWordsSession, transport, batchJobLogger);
    thrown.expect(BatchJobException.class);
    thrown.expectCause(Matchers.sameInstance(ioException));
    thrown.expectMessage("initiate upload");
    uploader.uploadIncrementalBatchJobOperations(request, true, new BatchJobUploadStatus(0, URI.create("http://www.example.com")));
}
Also used : MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) IOException(java.io.IOException) ByteArrayContent(com.google.api.client.http.ByteArrayContent) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 82 with MockHttpTransport

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

the class HttpHandlerTest method testInvokeSetsTimeout.

/**
 * Tests that the timeout set on the message context is passed to the underlying request.
 */
@Test
public void testInvokeSetsTimeout() {
    MessageContext messageContext = new MessageContext(axisEngine);
    messageContext.setRequestMessage(requestMessage);
    messageContext.setProperty(MessageContext.TRANS_URL, "https://www.example.com");
    // Do not care about XML parsing for this test, so set the response's status code to 302
    // to trigger an AxisFault.
    MockLowLevelHttpResponse lowLevelHttpResponse = new MockLowLevelHttpResponse();
    lowLevelHttpResponse.setContent("Intentional failure");
    lowLevelHttpResponse.setStatusCode(302);
    /*
     * Set timeout on the message context, then create a custom mock transport that will capture
     * invocations of LowLevelHttpRequest.setTimeout(int, int) and record the arguments passed.
     */
    int timeout = 1234567;
    messageContext.setTimeout(timeout);
    final int[] actualTimeouts = new int[] { Integer.MIN_VALUE, Integer.MIN_VALUE };
    MockLowLevelHttpRequest lowLevelHttpRequest = new MockLowLevelHttpRequest() {

        @Override
        public void setTimeout(int connectTimeout, int readTimeout) throws IOException {
            actualTimeouts[0] = connectTimeout;
            actualTimeouts[1] = readTimeout;
            super.setTimeout(connectTimeout, readTimeout);
        }
    };
    lowLevelHttpRequest.setResponse(lowLevelHttpResponse);
    MockHttpTransport mockTransport = new MockHttpTransport.Builder().setLowLevelHttpRequest(lowLevelHttpRequest).build();
    httpHandler = new HttpHandler(mockTransport, streamListener);
    try {
        httpHandler.invoke(messageContext);
        fail("Expected an AxisFault");
    } catch (AxisFault e) {
        assertThat(e.getFaultString(), Matchers.containsString("302"));
    }
    assertArrayEquals("Timeouts not set to expected values", new int[] { timeout, timeout }, actualTimeouts);
}
Also used : AxisFault(org.apache.axis.AxisFault) MockLowLevelHttpResponse(com.google.api.client.testing.http.MockLowLevelHttpResponse) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) MessageContext(org.apache.axis.MessageContext) MockLowLevelHttpRequest(com.google.api.client.testing.http.MockLowLevelHttpRequest) Test(org.junit.Test)

Example 83 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, final int statusCode) 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);
                    result.setStatusCode(statusCode);
                    return result;
                }
            };
        }
    };
    HttpRequest httpRequest = transport.createRequestFactory().buildGetRequest(new GenericUrl("https://google.com")).setParser(new JsonObjectParser(new JacksonFactory()));
    GoogleAuth.configureErrorHandling(httpRequest);
    return httpRequest;
}
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) 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)

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