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());
}
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);
}
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;
}
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();
}
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();
}
Aggregations