Search in sources :

Example 11 with HttpRequest

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

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

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

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

Example 15 with HttpRequest

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

the class AddHeadersPolicyTest method clientProvidedMultipleHeader.

@Test
public void clientProvidedMultipleHeader() throws Exception {
    String customRequestId = "request-id-value";
    final HttpHeaders headers = new HttpHeaders();
    headers.put("x-ms-client-request-id", customRequestId);
    headers.put("my-header1", "my-header1-value");
    headers.put("my-header2", "my-header2-value");
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {

        @Override
        public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
            Assertions.assertEquals(httpRequest.getHeaders().getValue("x-ms-client-request-id"), customRequestId);
            Assertions.assertEquals(httpRequest.getHeaders().getValue("my-header1"), "my-header1-value");
            Assertions.assertEquals(httpRequest.getHeaders().getValue("my-header2"), "my-header2-value");
            httpCallback.onSuccess(mockResponse);
        }
    }).policies(new AddHeadersPolicy(headers)).policies(new RequestIdPolicy()).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, "clientProvidedMultipleHeader");
}
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) CountDownLatch(java.util.concurrent.CountDownLatch) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Aggregations

HttpRequest (com.azure.android.core.http.HttpRequest)37 HttpResponse (com.azure.android.core.http.HttpResponse)22 HttpCallback (com.azure.android.core.http.HttpCallback)18 CountDownLatch (java.util.concurrent.CountDownLatch)18 HttpPipeline (com.azure.android.core.http.HttpPipeline)17 HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)17 CancellationToken (com.azure.android.core.util.CancellationToken)15 Test (org.junit.jupiter.api.Test)13 HttpHeaders (com.azure.android.core.http.HttpHeaders)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 HttpClient (com.azure.android.core.http.HttpClient)4 UrlBuilder (com.azure.android.core.http.util.UrlBuilder)4 IOException (java.io.IOException)4 HttpHeader (com.azure.android.core.http.HttpHeader)3 ClientLogger (com.azure.android.core.logging.ClientLogger)3 JacksonSerder (com.azure.android.core.serde.jackson.JacksonSerder)3 List (java.util.List)3 ResourceLock (org.junit.jupiter.api.parallel.ResourceLock)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 LIBRARY_PACKAGE_NAME (com.azure.android.communication.chat.BuildConfig.LIBRARY_PACKAGE_NAME)2