Search in sources :

Example 11 with CancellationToken

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

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

Example 13 with CancellationToken

use of com.azure.android.core.util.CancellationToken in project azure-sdk-for-android by Azure.

the class HttpLoggingPolicyTests method validateLoggingDoesNotChangeResponse.

/**
 * Tests that logging the response body doesn't consume the stream before it is returned from the service call.
 */
@ParameterizedTest(name = "[{index}] {displayName}")
@MethodSource("validateLoggingDoesNotConsumeSupplier")
@ResourceLock("SYSTEM_OUT")
public void validateLoggingDoesNotChangeResponse(byte[] content, byte[] data, int contentLength) {
    HttpRequest request = new HttpRequest(HttpMethod.GET, "https://test.com");
    HttpHeaders responseHeaders = 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) {
            httpCallback.onSuccess(new MockHttpResponse(httpRequest, 200, responseHeaders, content));
        }
    }).build();
    CountDownLatch latch = new CountDownLatch(1);
    // pipeline.send(request, CONTEXT)
    pipeline.send(request, RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            try {
                assertArrayEquals(data, response.getBodyAsByteArray());
            } finally {
                latch.countDown();
            }
        }

        @Override
        public void onError(Throwable error) {
            try {
                assertTrue(false, "unexpected call to pipeline::send onError" + error.getMessage());
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "validateLoggingDoesNotChangeResponse");
    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 14 with CancellationToken

use of com.azure.android.core.util.CancellationToken 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 15 with CancellationToken

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

Aggregations

CancellationToken (com.azure.android.core.util.CancellationToken)19 HttpRequest (com.azure.android.core.http.HttpRequest)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 HttpCallback (com.azure.android.core.http.HttpCallback)14 HttpResponse (com.azure.android.core.http.HttpResponse)14 HttpPipeline (com.azure.android.core.http.HttpPipeline)13 HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)13 Test (org.junit.jupiter.api.Test)13 HttpHeaders (com.azure.android.core.http.HttpHeaders)4 CancellationException (java.util.concurrent.CancellationException)4 HttpCallDispatcher (com.azure.android.core.http.HttpCallDispatcher)2 HttpClient (com.azure.android.core.http.HttpClient)2 IOException (java.io.IOException)2 CompletionException (java9.util.concurrent.CompletionException)2 ResourceLock (org.junit.jupiter.api.parallel.ResourceLock)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 HttpResponseException (com.azure.android.core.http.exception.HttpResponseException)1 NoOpHttpClient (com.azure.android.core.test.http.NoOpHttpClient)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1