Search in sources :

Example 6 with HttpCallback

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

the class RequestIdPolicyTests method newRequestIdForEachCall.

@Test
public void newRequestIdForEachCall() {
    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.assertNotEquals(newRequestId, firstRequestId);
            }
            firstRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
            if (firstRequestId == null) {
                Assertions.fail("The firstRequestId should not be null.");
            }
            httpCallback.onSuccess(mockResponse);
        }
    }).policies(new RequestIdPolicy()).build();
    CountDownLatch latch1 = new CountDownLatch(1);
    pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

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

        @Override
        public void onError(Throwable error) {
            try {
                throw new RuntimeException(error);
            } finally {
                latch1.countDown();
            }
        }
    });
    awaitOnLatch(latch1, "newRequestIdForEachCall");
    CountDownLatch latch2 = new CountDownLatch(1);
    pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

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

        @Override
        public void onError(Throwable error) {
            try {
                throw new RuntimeException(error);
            } finally {
                latch2.countDown();
            }
        }
    });
    awaitOnLatch(latch2, "newRequestIdForEachCall");
}
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 7 with HttpCallback

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

the class RetryPolicyTests method retryMax.

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

        int count = -1;

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            Assertions.assertTrue(count++ < maxRetries);
            httpCallback.onSuccess(new MockHttpResponse(httpRequest, 500));
        }
    }).policies(new RetryPolicy(new FixedDelay(maxRetries, 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, "retryMax");
    assertNotNull(httpResponse[0]);
    assertEquals(500, 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 8 with HttpCallback

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

the class RetryPolicyTests method retryConsumesBody.

@Test
public void retryConsumesBody() {
    final AtomicInteger bodyConsumptionCount = new AtomicInteger();
    final InputStream errorBody = new ByteArrayInputStream("Should be consumed".getBytes(StandardCharsets.UTF_8)) {

        @Override
        public void close() throws IOException {
            bodyConsumptionCount.incrementAndGet();
            super.close();
        }
    };
    final HttpPipeline pipeline = new HttpPipelineBuilder().policies(new RetryPolicy(new FixedDelay(2, Duration.ofMillis(1)))).httpClient(new NoOpHttpClient() {

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            httpCallback.onSuccess(new HttpResponse(httpRequest) {

                @Override
                public int getStatusCode() {
                    return 503;
                }

                @Override
                public String getHeaderValue(String name) {
                    return getHeaders().getValue(name);
                }

                @Override
                public HttpHeaders getHeaders() {
                    return new HttpHeaders();
                }

                @Override
                public InputStream getBody() {
                    return errorBody;
                }

                @Override
                public byte[] getBodyAsByteArray() {
                    return collectBytesInInputStream(getBody());
                }

                @Override
                public String getBodyAsString() {
                    return getBodyAsString(StandardCharsets.UTF_8);
                }

                @Override
                public String getBodyAsString(Charset charset) {
                    return new String(getBodyAsByteArray(), charset);
                }

                @Override
                public void close() {
                    try {
                        errorBody.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }
    }).build();
    CountDownLatch latch = new CountDownLatch(1);
    pipeline.send(new HttpRequest(HttpMethod.GET, "https://example.com"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

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

        @Override
        public void onError(Throwable error) {
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "retryConsumesBody");
    assertEquals(2, bodyConsumptionCount.get());
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) HttpHeaders(com.azure.android.core.http.HttpHeaders) CancellationToken(com.azure.android.core.util.CancellationToken) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) HttpCallback(com.azure.android.core.http.HttpCallback) HttpResponse(com.azure.android.core.http.HttpResponse) Charset(java.nio.charset.Charset) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Example 9 with HttpCallback

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

the class UserAgentPolicyTests method testDefaultUserAgentString.

@Test
void testDefaultUserAgentString() {
    UAgentOrError uAgentOrError = new UAgentOrError();
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            uAgentOrError.userAgent = httpRequest.getHeaders().getValue("User-Agent");
            httpCallback.onSuccess(new MockHttpResponse(httpRequest, 200));
        }
    }).policies(new UserAgentPolicy()).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 {
                uAgentOrError.error = error;
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "testDefaultUserAgentString");
    if (uAgentOrError.error != null) {
        Assertions.fail(uAgentOrError.error);
    }
    Assertions.assertEquals("azsdk-android", uAgentOrError.userAgent);
}
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 10 with HttpCallback

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

the class UserAgentPolicyTests method testNoAppIdInUserAgentString.

@Test
void testNoAppIdInUserAgentString() {
    UAgentOrError uAgentOrError = new UAgentOrError();
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            uAgentOrError.userAgent = httpRequest.getHeaders().getValue("User-Agent");
            httpCallback.onSuccess(new MockHttpResponse(httpRequest, 200));
        }
    }).policies(new UserAgentPolicy(null, "azure-storage-blob", "12.0.0")).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 {
                uAgentOrError.error = error;
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "testNoAppIdInUserAgentString");
    if (uAgentOrError.error != null) {
        Assertions.fail(uAgentOrError.error);
    }
    Assertions.assertEquals("azsdk-android-azure-storage-blob/12.0.0 (null; null)", uAgentOrError.userAgent);
}
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