Search in sources :

Example 1 with HttpClient

use of com.azure.android.core.http.HttpClient in project azure-sdk-for-android by Azure.

the class RestProxyTests method bytesUploadTest.

@Test
public void bytesUploadTest() throws Exception {
    byte[] reqContent = "The quick brown fox jumps over the lazy dog".getBytes();
    final HttpClient httpClient = createHttpClient();
    // Scenario: Log the body so that body buffering/replay behavior is exercised.
    // 
    // Order in which policies applied will be the order in which they added to builder
    // 
    final HttpPipeline httpPipeline = new HttpPipelineBuilder().httpClient(httpClient).policies(new PortPolicy(getWireMockPort(), true), new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))).build();
    CountDownLatch latch = new CountDownLatch(1);
    CallbackResult<HttpBinJSON> cbResult = new CallbackResult<>();
    RestProxy.create(BytesUploadService.class, httpPipeline, SERIALIZER).put(reqContent, reqContent.length, new Callback<Response<HttpBinJSON>>() {

        @Override
        public void onSuccess(Response<HttpBinJSON> response) {
            cbResult.response = response;
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable error) {
            cbResult.error = error;
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "bytesUploadTest");
    if (cbResult.error != null) {
        Assertions.fail(cbResult.error);
    } else {
        final Response<HttpBinJSON> response = cbResult.response;
        assertEquals("The quick brown fox jumps over the lazy dog", response.getValue().data());
    }
}
Also used : HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) Response(com.azure.android.core.rest.Response) StreamResponse(com.azure.android.core.rest.StreamResponse) HttpPipeline(com.azure.android.core.http.HttpPipeline) HttpClient(com.azure.android.core.http.HttpClient) PortPolicy(com.azure.android.core.http.policy.PortPolicy) HttpLogOptions(com.azure.android.core.http.policy.HttpLogOptions) HttpLoggingPolicy(com.azure.android.core.http.policy.HttpLoggingPolicy) HttpBinJSON(com.azure.android.core.test.implementation.entities.HttpBinJSON) Test(org.junit.jupiter.api.Test)

Example 2 with HttpClient

use of com.azure.android.core.http.HttpClient in project azure-sdk-for-android by Azure.

the class HttpLoggingPolicyTests method validateLoggingDoesNotChangeResponse.

/**
 * Tests that logging the response body doesn't consume the stream before it is returned from the service call.
 */
@ParameterizedTest(name = "[{index}] {displayName}")
@MethodSource("validateLoggingDoesNotConsumeSupplier")
@ResourceLock("SYSTEM_OUT")
public void validateLoggingDoesNotChangeResponse(byte[] content, byte[] data, int contentLength) {
    HttpRequest request = new HttpRequest(HttpMethod.GET, "https://test.com");
    HttpHeaders responseHeaders = new HttpHeaders().put("Content-Type", "application/json").put("Content-Length", Integer.toString(contentLength));
    HttpPipeline pipeline = new HttpPipelineBuilder().policies(new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY))).httpClient(new HttpClient() {

        @Override
        public HttpCallDispatcher getHttpCallDispatcher() {
            return new HttpCallDispatcher();
        }

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            httpCallback.onSuccess(new MockHttpResponse(httpRequest, 200, responseHeaders, content));
        }
    }).build();
    CountDownLatch latch = new CountDownLatch(1);
    // pipeline.send(request, CONTEXT)
    pipeline.send(request, RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            try {
                assertArrayEquals(data, response.getBodyAsByteArray());
            } finally {
                latch.countDown();
            }
        }

        @Override
        public void onError(Throwable error) {
            try {
                assertTrue(false, "unexpected call to pipeline::send onError" + error.getMessage());
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "validateLoggingDoesNotChangeResponse");
    String logString = convertOutputStreamToString(logCaptureStream);
    assertTrue(logString.contains(new String(data, StandardCharsets.UTF_8)));
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) HttpHeaders(com.azure.android.core.http.HttpHeaders) CancellationToken(com.azure.android.core.util.CancellationToken) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) HttpCallback(com.azure.android.core.http.HttpCallback) HttpResponse(com.azure.android.core.http.HttpResponse) HttpCallDispatcher(com.azure.android.core.http.HttpCallDispatcher) CountDownLatch(java.util.concurrent.CountDownLatch) HttpPipeline(com.azure.android.core.http.HttpPipeline) HttpClient(com.azure.android.core.http.HttpClient) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with HttpClient

use of com.azure.android.core.http.HttpClient in project azure-sdk-for-android by Azure.

the class HttpLoggingPolicyTests method validateLoggingDoesNotChangeRequest.

/**
 * Tests that logging the request body doesn't consume the stream before it is sent over the network.
 */
@ParameterizedTest(name = "[{index}] {displayName}")
@MethodSource("validateLoggingDoesNotConsumeSupplier")
@ResourceLock("SYSTEM_OUT")
public void validateLoggingDoesNotChangeRequest(byte[] content, byte[] data, int contentLength) {
    final String requestUrl = "https://test.com";
    HttpHeaders requestHeaders = new HttpHeaders().put("Content-Type", "application/json").put("Content-Length", Integer.toString(contentLength));
    HttpPipeline pipeline = new HttpPipelineBuilder().policies(new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY))).httpClient(new HttpClient() {

        @Override
        public HttpCallDispatcher getHttpCallDispatcher() {
            return new HttpCallDispatcher();
        }

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            assertArrayEquals(data, httpRequest.getBody());
            httpCallback.onSuccess(new MockHttpResponse(httpRequest, 200));
        }
    }).build();
    CountDownLatch latch = new CountDownLatch(1);
    // pipeline.send(new HttpRequest(HttpMethod.POST, requestUrl, requestHeaders, content), CONTEXT)
    pipeline.send(new HttpRequest(HttpMethod.POST, requestUrl, requestHeaders, content), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable error) {
            try {
                assertTrue(false, "unexpected call to pipeline::send onError" + error.getMessage());
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "validateLoggingDoesNotChangeRequest");
    String logString = convertOutputStreamToString(logCaptureStream);
    assertTrue(logString.contains(new String(data, StandardCharsets.UTF_8)));
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) HttpHeaders(com.azure.android.core.http.HttpHeaders) CancellationToken(com.azure.android.core.util.CancellationToken) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) HttpCallback(com.azure.android.core.http.HttpCallback) HttpResponse(com.azure.android.core.http.HttpResponse) HttpCallDispatcher(com.azure.android.core.http.HttpCallDispatcher) CountDownLatch(java.util.concurrent.CountDownLatch) HttpPipeline(com.azure.android.core.http.HttpPipeline) HttpClient(com.azure.android.core.http.HttpClient) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

HttpClient (com.azure.android.core.http.HttpClient)3 HttpPipeline (com.azure.android.core.http.HttpPipeline)3 HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 HttpCallDispatcher (com.azure.android.core.http.HttpCallDispatcher)2 HttpCallback (com.azure.android.core.http.HttpCallback)2 HttpHeaders (com.azure.android.core.http.HttpHeaders)2 HttpRequest (com.azure.android.core.http.HttpRequest)2 HttpResponse (com.azure.android.core.http.HttpResponse)2 CancellationToken (com.azure.android.core.util.CancellationToken)2 ResourceLock (org.junit.jupiter.api.parallel.ResourceLock)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 HttpLogOptions (com.azure.android.core.http.policy.HttpLogOptions)1 HttpLoggingPolicy (com.azure.android.core.http.policy.HttpLoggingPolicy)1 PortPolicy (com.azure.android.core.http.policy.PortPolicy)1 Response (com.azure.android.core.rest.Response)1 StreamResponse (com.azure.android.core.rest.StreamResponse)1 HttpBinJSON (com.azure.android.core.test.implementation.entities.HttpBinJSON)1 Test (org.junit.jupiter.api.Test)1