Search in sources :

Example 11 with HttpResponse

use of com.azure.android.core.http.HttpResponse 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 12 with HttpResponse

use of com.azure.android.core.http.HttpResponse 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 13 with HttpResponse

use of com.azure.android.core.http.HttpResponse 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 14 with HttpResponse

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

Example 15 with HttpResponse

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

the class UserAgentPolicyTests method testAppIdNoSdkInfoInUserAgentString.

@Test
void testAppIdNoSdkInfoInUserAgentString() {
    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("myappId", null, null)).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, "testAppIdNoSdkInfoInUserAgentString");
    if (uAgentOrError.error != null) {
        Assertions.fail(uAgentOrError.error);
    }
    Assertions.assertEquals("myappId azsdk-android-null/null (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

HttpResponse (com.azure.android.core.http.HttpResponse)32 HttpCallback (com.azure.android.core.http.HttpCallback)21 CountDownLatch (java.util.concurrent.CountDownLatch)21 HttpRequest (com.azure.android.core.http.HttpRequest)20 HttpPipeline (com.azure.android.core.http.HttpPipeline)18 Test (org.junit.jupiter.api.Test)18 HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)14 CancellationToken (com.azure.android.core.util.CancellationToken)13 HttpHeaders (com.azure.android.core.http.HttpHeaders)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 IOException (java.io.IOException)5 NextPolicyCallback (com.azure.android.core.http.NextPolicyCallback)4 PolicyCompleter (com.azure.android.core.http.PolicyCompleter)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 HttpHeader (com.azure.android.core.http.HttpHeader)3 HttpMethod (com.azure.android.core.http.HttpMethod)3 PagedResponse (com.azure.android.core.rest.util.paging.PagedResponse)3 JacksonSerder (com.azure.android.core.serde.jackson.JacksonSerder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3