Search in sources :

Example 21 with HttpCallback

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

the class RetryPolicyTests method retryEndOn501.

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

        // Send 408, 500, 502, all retried, with a 501 ending
        private final int[] codes = new int[] { 408, 500, 502, 501 };

        private int count = 0;

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            httpCallback.onSuccess(new MockHttpResponse(httpRequest, codes[count++]));
        }
    }).policies(new RetryPolicy(new FixedDelay(3, Duration.of(0, ChronoUnit.MILLIS)))).build();
    final HttpResponse[] httpResponse = new HttpResponse[1];
    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) {
            httpResponse[0] = response;
            latch.countDown();
        }

        @Override
        public void onError(Throwable error) {
            try {
                throw new RuntimeException(error);
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "retryEndOn501");
    assertNotNull(httpResponse[0]);
    assertEquals(501, httpResponse[0].getStatusCode());
}
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 22 with HttpCallback

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

the class HttpLoggingPolicyTests method redactQueryParameters.

/**
 * Tests that a query string will be properly redacted before it is logged.
 */
@ParameterizedTest
@MethodSource("redactQueryParametersSupplier")
@ResourceLock("SYSTEM_OUT")
public void redactQueryParameters(String requestUrl, String expectedQueryString, Set<String> allowedQueryParameters) {
    HttpPipeline pipeline = new HttpPipelineBuilder().policies(new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC).setAllowedQueryParamNames(allowedQueryParameters))).httpClient(new NoOpHttpClient()).build();
    CountDownLatch latch = new CountDownLatch(1);
    // pipeline.send(new HttpRequest(HttpMethod.POST, requestUrl), CONTEXT, new HttpCallback() {..})
    pipeline.send(new HttpRequest(HttpMethod.POST, requestUrl), 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, "redactQueryParameters");
    assertTrue(convertOutputStreamToString(logCaptureStream).contains(expectedQueryString));
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) HttpPipeline(com.azure.android.core.http.HttpPipeline) 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) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

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