Search in sources :

Example 1 with HttpRequest

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

the class HttpRequestMapperTests method stringByteNoContentType.

@Test
public void stringByteNoContentType() throws NoSuchMethodException, IOException {
    Class<StringByteNoContentTypeMethods> clazz = StringByteNoContentTypeMethods.class;
    Method byteBodyMethod = clazz.getDeclaredMethod("byteBody", (new byte[0]).getClass(), Callback.class);
    Method stringBodyMethod = clazz.getDeclaredMethod("stringBody", String.class, Callback.class);
    HttpRequestMapper requestMapper = new HttpRequestMapper("https://raw.host.com", byteBodyMethod, new JacksonSerder());
    byte[] bytesBody = new byte[2];
    bytesBody[0] = 'A';
    bytesBody[1] = 'B';
    HttpRequest httpRequest = requestMapper.map(toObjectArray(bytesBody));
    assertEquals("application/octet-stream", httpRequest.getHeaders().getValue("Content-Type"));
    assertArrayEquals(bytesBody, httpRequest.getBody());
    requestMapper = new HttpRequestMapper("https://raw.host.com", stringBodyMethod, new JacksonSerder());
    httpRequest = requestMapper.map(toObjectArray("hello"));
    assertEquals("application/octet-stream", httpRequest.getHeaders().getValue("Content-Type"));
    assertEquals("hello", new String(httpRequest.getBody()));
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) Method(java.lang.reflect.Method) HttpMethod(com.azure.android.core.http.HttpMethod) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with HttpRequest

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

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

the class HttpClientTests method sendRequest.

private String sendRequest(String requestPath, String method) {
    CountDownLatch latch = new CountDownLatch(1);
    final String[] content = new String[1];
    final Throwable[] throwable = new Throwable[1];
    content[0] = null;
    throwable[0] = null;
    createHttpClient().send(new HttpRequest(HttpMethod.GET, REQUEST_HOST + ":" + getWireMockPort() + "/" + requestPath), CancellationToken.NONE, new HttpCallback() {

        @Override
        public void onSuccess(HttpResponse response) {
            try {
                content[0] = response.getBodyAsString();
            } finally {
                latch.countDown();
            }
        }

        @Override
        public void onError(Throwable error) {
            try {
                throwable[0] = error;
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "method");
    if (throwable[0] != null) {
        throw new RuntimeException(throwable[0]);
    } else {
        return content[0];
    }
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) HttpCallback(com.azure.android.core.http.HttpCallback) HttpResponse(com.azure.android.core.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with HttpRequest

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

the class HttpRequestMapperTests method jsonBodySubstitution.

@ParameterizedTest
@MethodSource("jsonBodySubstitutionSupplier")
public void jsonBodySubstitution(Method method, Object[] arguments, String expectedBodyContentType, Object expectedBody) throws IOException {
    HttpRequestMapper mapper = new HttpRequestMapper("https://raw.host.com", method, new JacksonSerder());
    HttpRequest httpRequest = mapper.map(arguments);
    byte[] content = httpRequest.getBody();
    if (expectedBody == null) {
        Assertions.assertNull(content);
    } else {
        assertEquals(expectedBodyContentType, httpRequest.getHeaders().getValue("Content-Type"));
        assertEquals(expectedBody, new String(content, StandardCharsets.UTF_8));
    }
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with HttpRequest

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

the class RestProxy method invoke.

@Override
public Object invoke(final Object restProxy, final Method swaggerMethod, final Object[] swaggerMethodArgs) {
    final SwaggerMethodParser methodParser = this.interfaceParser.getMethodParser(swaggerMethod, this.logger);
    final Callback<Response<?>> restCallback;
    restCallback = (Callback<Response<?>>) swaggerMethodArgs[methodParser.callbackArgIndex];
    Objects.requireNonNull(restCallback);
    final CancellationToken cancellationToken;
    if (methodParser.cancellationTokenArgIndex == -1) {
        cancellationToken = CancellationToken.NONE;
    } else {
        cancellationToken = (CancellationToken) swaggerMethodArgs[methodParser.cancellationTokenArgIndex];
    }
    final HttpRequest httpRequest;
    try {
        httpRequest = methodParser.mapToHttpRequest(swaggerMethodArgs);
    } catch (IOException e) {
        restCallback.onFailure(e);
        return null;
    } catch (HttpResponseException e) {
        restCallback.onFailure(e);
        return null;
    }
    this.httpPipeline.send(httpRequest, RequestContext.NONE, cancellationToken, new HttpPipelineCallback(methodParser, restCallback));
    return null;
}
Also used : HttpResponse(com.azure.android.core.http.HttpResponse) HttpRequest(com.azure.android.core.http.HttpRequest) CancellationToken(com.azure.android.core.util.CancellationToken) HttpResponseException(com.azure.android.core.http.exception.HttpResponseException) IOException(java.io.IOException)

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