Search in sources :

Example 16 with HttpCallback

use of com.azure.android.core.http.HttpCallback 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)

Example 17 with HttpCallback

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

the class ProtocolPolicyTests method withOverwrite.

@Test
public void withOverwrite() {
    final HttpPipeline pipeline = createPipeline("ftp", "ftp://www.bing.com");
    CountDownLatch latch = new CountDownLatch(1);
    pipeline.send(createHttpRequest("http://www.bing.com"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

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

        @Override
        public void onError(Throwable error) {
            try {
                throw new RuntimeException(error);
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "withOverwrite");
}
Also used : HttpPipeline(com.azure.android.core.http.HttpPipeline) HttpCallback(com.azure.android.core.http.HttpCallback) HttpResponse(com.azure.android.core.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 18 with HttpCallback

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

the class RequestIdPolicyTests method sameRequestIdForRetry.

@Test
public void sameRequestIdForRetry() {
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {

        String firstRequestId = null;

        @Override
        public void send(HttpRequest request, CancellationToken cancellationToken, HttpCallback httpCallback) {
            if (firstRequestId != null) {
                String newRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
                Assertions.assertNotNull(newRequestId, "newRequestId should not be null");
                Assertions.assertEquals(newRequestId, firstRequestId);
            }
            firstRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
            if (firstRequestId == null) {
                Assertions.fail("The firstRequestId should not be null.");
            }
            httpCallback.onSuccess(mockResponse);
        // TODO: anuchan add a test with misbehaving httpclient that signal twice and assert we throw meaningful error
        // TODO: anuchan add a test that watch running calls when we schedule (i.e. ensure not holding the thread)
        // 
        // if (firstRequestId != null) {
        // String newRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
        // Assertions.assertNotNull(newRequestId);
        // Assertions.assertEquals(newRequestId, firstRequestId);
        // }
        // firstRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
        // if (firstRequestId == null) {
        // Assertions.fail();
        // }
        // httpCallback.onSuccess(mockResponse);
        }
    }).policies(new RequestIdPolicy(), new RetryPolicy(new FixedDelay(1, Duration.of(5, ChronoUnit.SECONDS)))).build();
    CountDownLatch latch = new CountDownLatch(1);
    pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

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

        @Override
        public void onError(Throwable error) {
            try {
                throw new RuntimeException(error);
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "sameRequestIdForRetry");
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) 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) CountDownLatch(java.util.concurrent.CountDownLatch) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Example 19 with HttpCallback

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

the class RetryPolicyTests method fixedDelayRetry.

@Test
public void fixedDelayRetry() {
    final int maxRetries = 5;
    final long delayMillis = 500;
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {

        int count = -1;

        long previousAttemptMadeAt = -1;

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            if (count > 0) {
                Assertions.assertTrue(System.currentTimeMillis() >= previousAttemptMadeAt + delayMillis);
            }
            Assertions.assertTrue(count++ < maxRetries);
            previousAttemptMadeAt = System.currentTimeMillis();
            httpCallback.onSuccess(new MockHttpResponse(httpRequest, 500));
        }
    }).policies(new RetryPolicy(new FixedDelay(maxRetries, Duration.ofMillis(delayMillis)))).build();
    CountDownLatch latch = new CountDownLatch(1);
    pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

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

        @Override
        public void onError(Throwable error) {
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "fixedDelayRetry");
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) 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) CountDownLatch(java.util.concurrent.CountDownLatch) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Example 20 with HttpCallback

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

the class RetryPolicyTests method exponentialDelayRetry.

@Test
public void exponentialDelayRetry() {
    final int maxRetries = 5;
    final long baseDelayMillis = 100;
    final long maxDelayMillis = 1000;
    ExponentialBackoff exponentialBackoff = new ExponentialBackoff(maxRetries, Duration.ofMillis(baseDelayMillis), Duration.ofMillis(maxDelayMillis));
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {

        int count = -1;

        long previousAttemptMadeAt = -1;

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            if (count > 0) {
                long requestMadeAt = System.currentTimeMillis();
                long expectedToBeMadeAt = previousAttemptMadeAt + ((1 << (count - 1)) * (long) (baseDelayMillis * 0.95));
                Assertions.assertTrue(requestMadeAt >= expectedToBeMadeAt);
            }
            Assertions.assertTrue(count++ < maxRetries);
            previousAttemptMadeAt = System.currentTimeMillis();
            httpCallback.onSuccess(new MockHttpResponse(httpRequest, 503));
        }
    }).policies(new RetryPolicy(exponentialBackoff)).build();
    CountDownLatch latch = new CountDownLatch(1);
    pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

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

        @Override
        public void onError(Throwable error) {
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "exponentialDelayRetry");
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) 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) CountDownLatch(java.util.concurrent.CountDownLatch) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Aggregations

HttpCallback (com.azure.android.core.http.HttpCallback)22 HttpResponse (com.azure.android.core.http.HttpResponse)21 CountDownLatch (java.util.concurrent.CountDownLatch)21 HttpPipeline (com.azure.android.core.http.HttpPipeline)18 HttpRequest (com.azure.android.core.http.HttpRequest)18 Test (org.junit.jupiter.api.Test)15 HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)14 CancellationToken (com.azure.android.core.util.CancellationToken)14 HttpHeaders (com.azure.android.core.http.HttpHeaders)4 ResourceLock (org.junit.jupiter.api.parallel.ResourceLock)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 HttpCallDispatcher (com.azure.android.core.http.HttpCallDispatcher)2 HttpClient (com.azure.android.core.http.HttpClient)2 NoOpHttpClient (com.azure.android.core.test.http.NoOpHttpClient)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Charset (java.nio.charset.Charset)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1