Search in sources :

Example 6 with MockResponse

use of com.google.api.ads.common.lib.testing.MockResponse in project googleads-java-lib by googleads.

the class BatchJobUploaderTest method testUploadIncrementalBatchJobOperations_notFirst_notLast.

@Test
public void testUploadIncrementalBatchJobOperations_notFirst_notLast() throws Exception {
    BatchJobUploadStatus status = new BatchJobUploadStatus(10, URI.create(mockHttpServer.getServerUrl()));
    String uploadRequestBody = "<mutate>testUpload</mutate>";
    when(uploadBodyProvider.getHttpContent(request, false, false)).thenReturn(new ByteArrayContent(null, uploadRequestBody.getBytes(UTF_8)));
    mockHttpServer.setMockResponse(new MockResponse("testUploadResponse"));
    // Invoked the incremental upload method.
    BatchJobUploadResponse response = uploader.uploadIncrementalBatchJobOperations(request, false, status);
    assertEquals("Should have made one request", 1, mockHttpServer.getAllResponses().size());
    // Check the request.
    String firstRequest = mockHttpServer.getLastResponse().getRequestBody();
    String expectedBody = "testUpload";
    expectedBody = Strings.padEnd(expectedBody, BatchJobUploader.REQUIRED_CONTENT_LENGTH_INCREMENT, ' ');
    assertEquals("Request body is incorrect", expectedBody, firstRequest);
    assertEquals("Request should have succeeded", 200, response.getHttpStatus());
    // Check the BatchJobUploadStatus.
    BatchJobUploadStatus expectedStatus = new BatchJobUploadStatus(status.getTotalContentLength() + expectedBody.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 7 with MockResponse

use of com.google.api.ads.common.lib.testing.MockResponse in project googleads-java-lib by googleads.

the class HttpHandlerTest method testInvokeReturnsInvalidXml.

/**
 * Tests that a poorly formed XML response will result in an AxisFault.
 */
@Test
public void testInvokeReturnsInvalidXml() throws AxisFault {
    MessageContext messageContext = new MessageContext(axisEngine);
    messageContext.setRequestMessage(requestMessage);
    messageContext.setProperty(MessageContext.TRANS_URL, mockHttpServer.getServerUrl());
    messageContext.setProperty(HTTPConstants.MC_GZIP_REQUEST, true);
    mockHttpServer.setMockResponse(new MockResponse("<?xml version='1.0' encoding='UTF-8' standalone='no'?>" + "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" + "foo..."));
    httpHandler.invoke(messageContext);
    // Expect parsing to fail. Tear down will verify the stream was closed.
    thrown.expect(AxisFault.class);
    messageContext.getResponseMessage().getSOAPEnvelope();
}
Also used : MockResponse(com.google.api.ads.common.lib.testing.MockResponse) MessageContext(org.apache.axis.MessageContext) Test(org.junit.Test)

Example 8 with MockResponse

use of com.google.api.ads.common.lib.testing.MockResponse in project googleads-java-lib by googleads.

the class HttpHandlerTest method testInvokeWithoutContentType.

/**
 * Tests that a request with null content type will fail as expected.
 */
@Test
public void testInvokeWithoutContentType() throws AxisFault {
    MessageContext messageContext = new MessageContext(axisEngine);
    messageContext.setRequestMessage(requestMessage);
    messageContext.setProperty(MessageContext.TRANS_URL, mockHttpServer.getServerUrl());
    messageContext.setProperty(HTTPConstants.MC_GZIP_REQUEST, true);
    MockResponse mockResponse = new MockResponse("Something went wrong", 500);
    mockResponse.setContentType(null);
    mockHttpServer.setMockResponse(mockResponse);
    // Expect an AxisFault based on the status code and content type.
    thrown.expect(AxisFault.class);
    httpHandler.invoke(messageContext);
}
Also used : MockResponse(com.google.api.ads.common.lib.testing.MockResponse) MessageContext(org.apache.axis.MessageContext) Test(org.junit.Test)

Aggregations

MockResponse (com.google.api.ads.common.lib.testing.MockResponse)8 Test (org.junit.Test)8 ByteArrayContent (com.google.api.client.http.ByteArrayContent)4 MessageContext (org.apache.axis.MessageContext)4 AxisClient (org.apache.axis.client.AxisClient)1