Search in sources :

Example 1 with HttpPipelineBuilder

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

the class ChatThreadClientBuilder method createHttpPipeline.

private HttpPipeline createHttpPipeline(HttpClient httpClient, HttpPipelinePolicy authorizationPolicy, List<HttpPipelinePolicy> additionalPolicies) {
    List<HttpPipelinePolicy> policies = new ArrayList<HttpPipelinePolicy>();
    applyRequiredPolicies(policies, authorizationPolicy);
    if (additionalPolicies != null && additionalPolicies.size() > 0) {
        policies.addAll(additionalPolicies);
    }
    return new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])).httpClient(httpClient).build();
}
Also used : HttpPipelinePolicy(com.azure.android.core.http.HttpPipelinePolicy) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) ArrayList(java.util.ArrayList)

Example 2 with HttpPipelineBuilder

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

the class RestProxyXMLTests method canWriteXMLRequest.

@Test
public void canWriteXMLRequest() throws Exception {
    URL url = getClass().getClassLoader().getResource("GetContainerACLs.xml");
    byte[] bytes = Files.readAllBytes(Paths.get(url.toURI()));
    HttpRequest request = new HttpRequest(HttpMethod.PUT, "http://unused/SetContainerACLs");
    request.setBody(bytes);
    SignedIdentifierInner si = new SignedIdentifierInner();
    si.withId("MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=");
    AccessPolicy ap = new AccessPolicy();
    ap.withStart(OffsetDateTime.parse("2009-09-28T08:49:37.0000000Z"));
    ap.withExpiry(OffsetDateTime.parse("2009-09-29T08:49:37.0000000Z"));
    ap.withPermission("rwd");
    si.withAccessPolicy(ap);
    List<SignedIdentifierInner> expectedAcls = Collections.singletonList(si);
    JacksonSerder serderAdapter = new JacksonSerder();
    MockXMLReceiverClient httpClient = new MockXMLReceiverClient();
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(httpClient).build();
    MyXMLService myXMLService = RestProxy.create(MyXMLService.class, pipeline, serderAdapter);
    SignedIdentifiersWrapper wrapper = new SignedIdentifiersWrapper(expectedAcls);
    CountDownLatch latch = new CountDownLatch(1);
    myXMLService.setContainerACLs(wrapper, new Callback<Response<Void>>() {

        @Override
        public void onSuccess(Response<Void> response) {
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable error) {
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "canWriteXMLRequest");
    SignedIdentifiersWrapper actualAclsWrapped = serderAdapter.deserialize(new String(httpClient.receivedBytes, StandardCharsets.UTF_8), SignedIdentifiersWrapper.class, SerdeEncoding.XML);
    List<SignedIdentifierInner> actualAcls = actualAclsWrapped.signedIdentifiers();
    // Ideally we'd just check for "things that matter" about the XML-- e.g. the tag names, structure, and attributes needs to be the same,
    // but it doesn't matter if one document has a trailing newline or has UTF-8 in the header instead of utf-8, or if comments are missing.
    assertEquals(expectedAcls.size(), actualAcls.size());
    assertEquals(expectedAcls.get(0).id(), actualAcls.get(0).id());
    assertEquals(expectedAcls.get(0).accessPolicy().expiry(), actualAcls.get(0).accessPolicy().expiry());
    assertEquals(expectedAcls.get(0).accessPolicy().start(), actualAcls.get(0).accessPolicy().start());
    assertEquals(expectedAcls.get(0).accessPolicy().permission(), actualAcls.get(0).accessPolicy().permission());
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) URL(java.net.URL) HttpResponse(com.azure.android.core.http.HttpResponse) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Example 3 with HttpPipelineBuilder

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

the class RestProxyXMLTests method canReadXMLResponse.

@Test
public void canReadXMLResponse() {
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new MockXMLHTTPClient()).build();
    MyXMLService myXMLService = RestProxy.create(MyXMLService.class, pipeline, new JacksonSerder());
    final SignedIdentifiersWrapperOrError wrapperOrError = new SignedIdentifiersWrapperOrError();
    CountDownLatch latch = new CountDownLatch(1);
    myXMLService.getContainerACLs(new Callback<Response<SignedIdentifiersWrapper>>() {

        @Override
        public void onSuccess(Response<SignedIdentifiersWrapper> response) {
            try {
                wrapperOrError.wrapper = response.getValue();
            } finally {
                latch.countDown();
            }
        }

        @Override
        public void onFailure(Throwable error) {
            try {
                wrapperOrError.error = error;
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "canReadXMLResponse");
    if (wrapperOrError.error != null) {
        fail(wrapperOrError.error);
    } else {
        assertNotNull(wrapperOrError.wrapper);
        assertNotNull(wrapperOrError.wrapper.signedIdentifiers());
        assertNotEquals(0, wrapperOrError.wrapper.signedIdentifiers().size());
    }
}
Also used : JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponse(com.azure.android.core.http.HttpResponse) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Example 4 with HttpPipelineBuilder

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

the class RestProxyTests method bytesUploadTest.

@Test
public void bytesUploadTest() throws Exception {
    byte[] reqContent = "The quick brown fox jumps over the lazy dog".getBytes();
    final HttpClient httpClient = createHttpClient();
    // Scenario: Log the body so that body buffering/replay behavior is exercised.
    // 
    // Order in which policies applied will be the order in which they added to builder
    // 
    final HttpPipeline httpPipeline = new HttpPipelineBuilder().httpClient(httpClient).policies(new PortPolicy(getWireMockPort(), true), new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))).build();
    CountDownLatch latch = new CountDownLatch(1);
    CallbackResult<HttpBinJSON> cbResult = new CallbackResult<>();
    RestProxy.create(BytesUploadService.class, httpPipeline, SERIALIZER).put(reqContent, reqContent.length, new Callback<Response<HttpBinJSON>>() {

        @Override
        public void onSuccess(Response<HttpBinJSON> response) {
            cbResult.response = response;
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable error) {
            cbResult.error = error;
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "bytesUploadTest");
    if (cbResult.error != null) {
        Assertions.fail(cbResult.error);
    } else {
        final Response<HttpBinJSON> response = cbResult.response;
        assertEquals("The quick brown fox jumps over the lazy dog", response.getValue().data());
    }
}
Also used : HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) Response(com.azure.android.core.rest.Response) StreamResponse(com.azure.android.core.rest.StreamResponse) HttpPipeline(com.azure.android.core.http.HttpPipeline) HttpClient(com.azure.android.core.http.HttpClient) PortPolicy(com.azure.android.core.http.policy.PortPolicy) HttpLogOptions(com.azure.android.core.http.policy.HttpLogOptions) HttpLoggingPolicy(com.azure.android.core.http.policy.HttpLoggingPolicy) HttpBinJSON(com.azure.android.core.test.implementation.entities.HttpBinJSON) Test(org.junit.jupiter.api.Test)

Example 5 with HttpPipelineBuilder

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

Aggregations

HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)21 HttpPipeline (com.azure.android.core.http.HttpPipeline)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 HttpResponse (com.azure.android.core.http.HttpResponse)17 HttpRequest (com.azure.android.core.http.HttpRequest)15 Test (org.junit.jupiter.api.Test)15 HttpCallback (com.azure.android.core.http.HttpCallback)14 CancellationToken (com.azure.android.core.util.CancellationToken)13 HttpHeaders (com.azure.android.core.http.HttpHeaders)4 HttpClient (com.azure.android.core.http.HttpClient)3 HttpPipelinePolicy (com.azure.android.core.http.HttpPipelinePolicy)3 JacksonSerder (com.azure.android.core.serde.jackson.JacksonSerder)3 ArrayList (java.util.ArrayList)3 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 HttpLogOptions (com.azure.android.core.http.policy.HttpLogOptions)2 HttpLoggingPolicy (com.azure.android.core.http.policy.HttpLoggingPolicy)2 CookiePolicy (com.azure.android.core.http.policy.CookiePolicy)1