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