Search in sources :

Example 16 with ByteArrayContent

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

the class BatchJobUploaderTest method testUploadIncrementalBatchJobOperations_firstAndLast.

@Test
public void testUploadIncrementalBatchJobOperations_firstAndLast() throws Exception {
    BatchJobUploadStatus status = new BatchJobUploadStatus(0, URI.create(mockHttpServer.getServerUrl()));
    String uploadRequestBody = "testUpload";
    when(uploadBodyProvider.getHttpContent(request, true, true)).thenReturn(new ByteArrayContent(null, uploadRequestBody.getBytes(UTF_8)));
    List<MockResponse> expectedResponses = Lists.newArrayList(new MockResponse("ignore"), new MockResponse("testUploadResponse"));
    mockHttpServer.setMockResponses(expectedResponses);
    // Invoked the incremental upload method.
    BatchJobUploadResponse response = uploader.uploadIncrementalBatchJobOperations(request, true, status);
    assertEquals("Should have made two requests", 2, mockHttpServer.getAllResponses().size());
    // Check the first request.
    String firstRequest = mockHttpServer.getAllResponses().get(0).getRequestBody();
    assertEquals("First request should have an empty body", "", firstRequest);
    assertEquals("First request should include resumable header", "start", mockHttpServer.getAllResponses().get(0).getRequestHeader("x-goog-resumable").get(0));
    // Check the second request.
    assertEquals("Second request body is incorrect", uploadRequestBody, mockHttpServer.getLastResponse().getRequestBody());
    assertEquals("Last request should have succeeded", 200, response.getHttpStatus());
    // Check the BatchJobUploadStatus.
    BatchJobUploadStatus expectedStatus = new BatchJobUploadStatus(uploadRequestBody.getBytes(UTF_8).length, URI.create(mockHttpServer.getServerUrl()));
    BatchJobUploadStatus actualStatus = response.getBatchJobUploadStatus();
    assertEquals("Status total content length is incorrect", expectedStatus.getTotalContentLength(), actualStatus.getTotalContentLength());
    assertEquals("Status resumable upload URI is incorrect", expectedStatus.getResumableUploadUri(), actualStatus.getResumableUploadUri());
}
Also used : MockResponse(com.google.api.ads.common.lib.testing.MockResponse) ByteArrayContent(com.google.api.client.http.ByteArrayContent) Test(org.junit.Test)

Example 17 with ByteArrayContent

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

the class BatchJobUploaderTest method testUploadBatchJobOperations_ioException_fails.

/**
 * Tests that IOExceptions from executing an upload request are propagated properly.
 */
@SuppressWarnings("rawtypes")
@Test
public void testUploadBatchJobOperations_ioException_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);
    BatchJobUploadStatus uploadStatus = new BatchJobUploadStatus(0, URI.create("http://www.example.com"));
    thrown.expect(BatchJobException.class);
    thrown.expectCause(Matchers.sameInstance(ioException));
    uploader.uploadIncrementalBatchJobOperations(request, true, uploadStatus);
}
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 18 with ByteArrayContent

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

the class HttpHandler method createHttpRequest.

/**
 * Creates an HTTP request based on the message context.
 *
 * @param msgContext the Axis message context
 * @return a new {@link HttpRequest} with content and headers populated
 */
private HttpRequest createHttpRequest(MessageContext msgContext) throws SOAPException, IOException {
    Message requestMessage = Preconditions.checkNotNull(msgContext.getRequestMessage(), "Null request message on message context");
    // Construct the output stream.
    String contentType = requestMessage.getContentType(msgContext.getSOAPConstants());
    ByteArrayOutputStream bos = new ByteArrayOutputStream(BUFFER_SIZE);
    if (msgContext.isPropertyTrue(HTTPConstants.MC_GZIP_REQUEST)) {
        logger.debug("Compressing request");
        try (GZIPOutputStream gzipOs = new GZIPOutputStream(bos, BUFFER_SIZE)) {
            requestMessage.writeTo(gzipOs);
        }
    } else {
        logger.debug("Not compressing request");
        requestMessage.writeTo(bos);
    }
    HttpRequest httpRequest = requestFactory.buildPostRequest(new GenericUrl(msgContext.getStrProp(MessageContext.TRANS_URL)), new ByteArrayContent(contentType, bos.toByteArray()));
    int timeoutMillis = msgContext.getTimeout();
    if (timeoutMillis >= 0) {
        logger.debug("Setting read and connect timeout to {} millis", timeoutMillis);
        // These are not the same, but MessageContext has only one definition of timeout.
        httpRequest.setReadTimeout(timeoutMillis);
        httpRequest.setConnectTimeout(timeoutMillis);
    }
    // Copy the request headers from the message context to the post request.
    setHttpRequestHeaders(msgContext, httpRequest);
    return httpRequest;
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) Message(org.apache.axis.Message) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericUrl(com.google.api.client.http.GenericUrl) ByteArrayContent(com.google.api.client.http.ByteArrayContent)

Example 19 with ByteArrayContent

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

the class MockHttpServerTest method testNoResponseBodySet_fails.

@Test
public void testNoResponseBodySet_fails() throws IOException {
    HttpRequest request = mockHttpServer.getHttpTransport().createRequestFactory().buildGetRequest(new GenericUrl(mockHttpServer.getServerUrl()));
    request.setContent(new ByteArrayContent("text", "test content".getBytes(UTF_8)));
    thrown.expect(HttpResponseException.class);
    thrown.expectMessage("No mock response");
    request.execute();
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) GenericUrl(com.google.api.client.http.GenericUrl) ByteArrayContent(com.google.api.client.http.ByteArrayContent) Test(org.junit.Test)

Example 20 with ByteArrayContent

use of com.google.api.client.http.ByteArrayContent in project data-transfer-project by google.

the class SolidUtilities method postContent.

/**
 * Posts an RDF model to a Solid server. *
 */
public String postContent(String url, String slug, String type, Model model) throws IOException {
    StringWriter stringWriter = new StringWriter();
    model.write(stringWriter, "TURTLE");
    HttpContent content = new ByteArrayContent("text/turtle", stringWriter.toString().getBytes());
    HttpRequest postRequest = factory.buildPostRequest(new GenericUrl(url), content);
    HttpHeaders headers = new HttpHeaders();
    headers.setCookie(authCookie);
    headers.set("Link", "<" + type + ">; rel=\"type\"");
    headers.set("Slug", slug);
    postRequest.setHeaders(headers);
    HttpResponse response = postRequest.execute();
    validateResponse(response, 201);
    return response.getHeaders().getLocation();
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) StringWriter(java.io.StringWriter) HttpResponse(com.google.api.client.http.HttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) ByteArrayContent(com.google.api.client.http.ByteArrayContent) HttpContent(com.google.api.client.http.HttpContent)

Aggregations

ByteArrayContent (com.google.api.client.http.ByteArrayContent)43 GenericUrl (com.google.api.client.http.GenericUrl)24 HttpRequest (com.google.api.client.http.HttpRequest)17 IOException (java.io.IOException)12 Test (org.junit.Test)12 HttpResponse (com.google.api.client.http.HttpResponse)10 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)7 HttpContent (com.google.api.client.http.HttpContent)6 HttpHeaders (com.google.api.client.http.HttpHeaders)6 Gson (com.google.gson.Gson)6 MockResponse (com.google.api.ads.common.lib.testing.MockResponse)4 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)4 MockLowLevelHttpRequest (com.google.api.client.testing.http.MockLowLevelHttpRequest)4 File (com.google.api.services.drive.model.File)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Credential (com.google.api.client.auth.oauth2.Credential)3 GoogleCredential (com.google.api.client.googleapis.auth.oauth2.GoogleCredential)3 HttpResponseException (com.google.api.client.http.HttpResponseException)3 InputStreamContent (com.google.api.client.http.InputStreamContent)3 AbstractInputStreamContent (com.google.api.client.http.AbstractInputStreamContent)2