Search in sources :

Example 11 with HttpHeaders

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

the class HttpResponseMapperTests method base64EncodedContentMap.

@Test
public void base64EncodedContentMap() throws Throwable {
    Class<Base64EncodedContentMethods> clazz = Base64EncodedContentMethods.class;
    Method base64UrlListMethod = clazz.getDeclaredMethod("base64UrlMap", Callback.class);
    HttpResponseMapper mapperBase64List = new HttpResponseMapper(base64UrlListMethod, extractCallbackType(base64UrlListMethod), logger);
    JacksonSerder jacksonSerder = new JacksonSerder();
    final String value0ToEncode = "hello azure android";
    final String value1ToEncode = "hello azure android again";
    Map<String, String> base64EncodedBytesMap = new HashMap<>();
    base64EncodedBytesMap.put("v0", Base64Url.encode(value0ToEncode.getBytes()).toString());
    base64EncodedBytesMap.put("v1", Base64Url.encode(value1ToEncode.getBytes()).toString());
    String wireBase64EncodedList = jacksonSerder.serialize(base64EncodedBytesMap, SerdeEncoding.JSON);
    MockHttpResponse httpResponseBase64DecodedBytesMap = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireBase64EncodedList.getBytes());
    Response<Map<String, byte[]>> restResponseBase64DecodedBytesMap = (Response<Map<String, byte[]>>) mapperBase64List.map(httpResponseBase64DecodedBytesMap, jacksonSerder);
    Map<String, byte[]> decodedBytesList = restResponseBase64DecodedBytesMap.getValue();
    assertNotNull(decodedBytesList);
    assertEquals(2, decodedBytesList.size());
    assertArrayEquals(value0ToEncode.getBytes(), decodedBytesList.get("v0"));
    assertArrayEquals(value1ToEncode.getBytes(), decodedBytesList.get("v1"));
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) HttpMethod(com.azure.android.core.http.HttpMethod) HttpResponse(com.azure.android.core.http.HttpResponse) PagedResponse(com.azure.android.core.rest.util.paging.PagedResponse) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with HttpHeaders

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

the class HttpResponseMapperTests method deserializePojo.

@Test
public void deserializePojo() throws Throwable {
    Class<DeserializePojoMethods> clazz = DeserializePojoMethods.class;
    Method getPersonMethod = clazz.getDeclaredMethod("getPerson", Callback.class);
    HttpResponseMapper mapper = new HttpResponseMapper(getPersonMethod, extractCallbackType(getPersonMethod), logger);
    Person person = new Person("John Doe", 40, OffsetDateTime.parse("1980-01-01T10:00:00Z"));
    JacksonSerder jacksonSerder = new JacksonSerder();
    String wirePerson = jacksonSerder.serialize(person, SerdeEncoding.JSON);
    MockHttpResponse httpResponse = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wirePerson.getBytes());
    Response<Person> restStreamResponse = (Response<Person>) mapper.map(httpResponse, jacksonSerder);
    Person receivedPerson = restStreamResponse.getValue();
    assertEquals(person.getName(), receivedPerson.getName());
    assertEquals(person.getAge(), receivedPerson.getAge());
    assertEquals(person.getDob(), receivedPerson.getDob());
}
Also used : HttpResponse(com.azure.android.core.http.HttpResponse) PagedResponse(com.azure.android.core.rest.util.paging.PagedResponse) HttpHeaders(com.azure.android.core.http.HttpHeaders) 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 13 with HttpHeaders

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

the class HttpResponseMapperTests method head.

@Test
public void head() throws Throwable {
    Class<HeadMethods> clazz = HeadMethods.class;
    Method headMethod = clazz.getDeclaredMethod("resourceExists", Callback.class);
    HttpResponseMapper mapper = new HttpResponseMapper(headMethod, extractCallbackType(headMethod), logger);
    MockHttpResponse httpResponse = new MockHttpResponse(HttpMethod.HEAD, "https://raw.host.com", 200, new HttpHeaders(), new byte[0]);
    Response<Boolean> restResponse = (Response<Boolean>) mapper.map(httpResponse, new JacksonSerder());
    assertTrue(restResponse.getValue());
    assertTrue(httpResponse.isClosed());
    httpResponse = new MockHttpResponse(HttpMethod.HEAD, "https://raw.host.com", 401, new HttpHeaders(), new byte[0]);
    restResponse = (Response<Boolean>) mapper.map(httpResponse, new JacksonSerder());
    assertFalse(restResponse.getValue());
    assertTrue(httpResponse.isClosed());
}
Also used : HttpResponse(com.azure.android.core.http.HttpResponse) PagedResponse(com.azure.android.core.rest.util.paging.PagedResponse) HttpHeaders(com.azure.android.core.http.HttpHeaders) 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 14 with HttpHeaders

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

the class RestProxyTests method getRequestWithNullHeader.

@Test
public void getRequestWithNullHeader() {
    CountDownLatch latch = new CountDownLatch(1);
    CallbackResult<HttpBinJSON> cbResult = new CallbackResult<>();
    createService(Service7.class).getAnything(null, 15, 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, "getRequestWithNullHeader");
    if (cbResult.error != null) {
        Assertions.fail(cbResult.error);
    } else {
        final Response<HttpBinJSON> response = cbResult.response;
        Assertions.assertNotNull(response);
        HttpBinJSON json = response.getValue();
        final HttpHeaders headers = new HttpHeaders(json.headers());
        assertNull(headers.getValue("A"));
        assertArrayEquals(null, headers.getValues("A"));
        assertEquals("15", headers.getValue("B"));
        assertArrayEquals(new String[] { "15" }, headers.getValues("B"));
    }
}
Also used : Response(com.azure.android.core.rest.Response) StreamResponse(com.azure.android.core.rest.StreamResponse) HttpHeaders(com.azure.android.core.http.HttpHeaders) CountDownLatch(java.util.concurrent.CountDownLatch) HttpBinJSON(com.azure.android.core.test.implementation.entities.HttpBinJSON) Test(org.junit.jupiter.api.Test)

Example 15 with HttpHeaders

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

the class MockHttpClient method send.

@Override
public void send(HttpRequest request, CancellationToken cancellationToken, HttpCallback httpCallback) {
    HttpResponse response = null;
    try {
        final URL requestUrl = request.getUrl();
        final String requestHost = requestUrl.getHost();
        final String contentType = request.getHeaders().getValue("Content-Type");
        if ("localhost".equalsIgnoreCase(requestHost)) {
            final String requestPath = requestUrl.getPath();
            final String requestPathLower = requestPath.toLowerCase();
            if (requestPathLower.startsWith("/anything")) {
                if ("HEAD".equals(request.getHttpMethod().name())) {
                    response = new MockHttpResponse(request, 200, new byte[0]);
                } else {
                    final HttpBinJSON json = new HttpBinJSON();
                    json.url(cleanseUrl(requestUrl));
                    json.headers(toMap(request.getHeaders()));
                    response = new MockHttpResponse(request, 200, json);
                }
            } else if (requestPathLower.startsWith("/bytes/")) {
                final String byteCountString = requestPath.substring("/bytes/".length());
                final int byteCount = Integer.parseInt(byteCountString);
                HttpHeaders newHeaders = new HttpHeaders(RESPONSE_HEADERS).put("Content-Type", "application/octet-stream").put("Content-Length", Integer.toString(byteCount));
                response = new MockHttpResponse(request, 200, newHeaders, byteCount == 0 ? null : new byte[byteCount]);
            } else if (requestPathLower.startsWith("/base64urlbytes/")) {
                final String byteCountString = requestPath.substring("/base64urlbytes/".length());
                final int byteCount = Integer.parseInt(byteCountString);
                final byte[] bytes = new byte[byteCount];
                for (int i = 0; i < byteCount; ++i) {
                    bytes[i] = (byte) i;
                }
                final Base64Url base64EncodedBytes = bytes.length == 0 ? null : Base64Url.encode(bytes);
                response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, base64EncodedBytes);
            } else if (requestPathLower.equals("/base64urllistofbytes")) {
                final List<String> base64EncodedBytesList = new ArrayList<>();
                for (int i = 0; i < 3; ++i) {
                    final int byteCount = (i + 1) * 10;
                    final byte[] bytes = new byte[byteCount];
                    for (int j = 0; j < byteCount; ++j) {
                        bytes[j] = (byte) j;
                    }
                    base64EncodedBytesList.add(Base64Url.encode(bytes).toString());
                }
                response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, base64EncodedBytesList);
            } else if (requestPathLower.equals("/base64urllistoflistofbytes")) {
                final List<List<String>> result = new ArrayList<>();
                for (int i = 0; i < 2; ++i) {
                    final List<String> innerList = new ArrayList<>();
                    for (int j = 0; j < (i + 1) * 2; ++j) {
                        final int byteCount = (j + 1) * 5;
                        final byte[] bytes = new byte[byteCount];
                        for (int k = 0; k < byteCount; ++k) {
                            bytes[k] = (byte) k;
                        }
                        innerList.add(Base64Url.encode(bytes).toString());
                    }
                    result.add(innerList);
                }
                response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, result);
            } else if (requestPathLower.equals("/base64urlmapofbytes")) {
                final Map<String, String> result = new HashMap<>();
                for (int i = 0; i < 2; ++i) {
                    final String key = Integer.toString(i);
                    final int byteCount = (i + 1) * 10;
                    final byte[] bytes = new byte[byteCount];
                    for (int j = 0; j < byteCount; ++j) {
                        bytes[j] = (byte) j;
                    }
                    result.put(key, Base64Url.encode(bytes).toString());
                }
                response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, result);
            } else if (requestPathLower.equals("/datetimerfc1123")) {
                final DateTimeRfc1123 now = new DateTimeRfc1123(OffsetDateTime.ofInstant(Instant.ofEpochSecond(0), ZoneOffset.UTC));
                response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, now.toString());
            } else if (requestPathLower.equals("/unixtime")) {
                response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, 0);
            } else if (requestPathLower.equals("/delete")) {
                final HttpBinJSON json = new HttpBinJSON();
                json.url(cleanseUrl(requestUrl));
                json.data(createHttpBinResponseDataForRequest(request));
                response = new MockHttpResponse(request, 200, json);
            } else if (requestPathLower.equals("/get")) {
                final HttpBinJSON json = new HttpBinJSON();
                json.url(cleanseUrl(requestUrl));
                json.headers(toMap(request.getHeaders()));
                response = new MockHttpResponse(request, 200, json);
            } else if (requestPathLower.equals("/patch")) {
                final HttpBinJSON json = new HttpBinJSON();
                json.url(cleanseUrl(requestUrl));
                json.data(createHttpBinResponseDataForRequest(request));
                response = new MockHttpResponse(request, 200, json);
            } else if (requestPathLower.equals("/post")) {
                if (contentType != null && contentType.contains("x-www-form-urlencoded")) {
                    Map<String, String> parsed = bodyToMap(request);
                    final HttpBinFormDataJSON json = new HttpBinFormDataJSON();
                    HttpBinFormDataJSON.Form form = new HttpBinFormDataJSON.Form();
                    form.customerName(parsed.get("custname"));
                    form.customerEmail(parsed.get("custemail"));
                    form.customerTelephone(parsed.get("custtel"));
                    form.pizzaSize(HttpBinFormDataJSON.PizzaSize.valueOf(parsed.get("size")));
                    form.toppings(Arrays.asList(parsed.get("toppings").split(",")));
                    json.form(form);
                    response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, json);
                } else {
                    final HttpBinJSON json = new HttpBinJSON();
                    json.url(cleanseUrl(requestUrl));
                    json.data(createHttpBinResponseDataForRequest(request));
                    json.headers(toMap(request.getHeaders()));
                    response = new MockHttpResponse(request, 200, json);
                }
            } else if (requestPathLower.equals("/put")) {
                final HttpBinJSON json = new HttpBinJSON();
                json.url(cleanseUrl(requestUrl));
                json.data(createHttpBinResponseDataForRequest(request));
                json.headers(toMap(request.getHeaders()));
                response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, json);
            } else if (requestPathLower.startsWith("/status/")) {
                final String statusCodeString = requestPathLower.substring("/status/".length());
                final int statusCode = Integer.parseInt(statusCodeString);
                response = new MockHttpResponse(request, statusCode);
            }
        } else if ("echo.org".equalsIgnoreCase(requestHost)) {
            response = new MockHttpResponse(request, 200, new HttpHeaders(request.getHeaders()), request.getBody());
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    if (response == null) {
        response = new MockHttpResponse(request, 500);
    }
    httpCallback.onSuccess(response);
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) HttpBinFormDataJSON(com.azure.android.core.test.implementation.entities.HttpBinFormDataJSON) ArrayList(java.util.ArrayList) HttpResponse(com.azure.android.core.http.HttpResponse) URL(java.net.URL) Base64Url(com.azure.android.core.util.Base64Url) DateTimeRfc1123(com.azure.android.core.util.DateTimeRfc1123) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HttpBinJSON(com.azure.android.core.test.implementation.entities.HttpBinJSON)

Aggregations

HttpHeaders (com.azure.android.core.http.HttpHeaders)37 Test (org.junit.jupiter.api.Test)23 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)23 HttpResponse (com.azure.android.core.http.HttpResponse)20 JacksonSerder (com.azure.android.core.serde.jackson.JacksonSerder)17 HttpMethod (com.azure.android.core.http.HttpMethod)16 Method (java.lang.reflect.Method)16 PagedResponse (com.azure.android.core.rest.util.paging.PagedResponse)13 CountDownLatch (java.util.concurrent.CountDownLatch)9 HashMap (java.util.HashMap)7 HttpRequest (com.azure.android.core.http.HttpRequest)6 HttpBinJSON (com.azure.android.core.test.implementation.entities.HttpBinJSON)6 Map (java.util.Map)6 OffsetDateTime (org.threeten.bp.OffsetDateTime)6 Response (com.azure.android.core.rest.Response)5 StreamResponse (com.azure.android.core.rest.StreamResponse)5 List (java.util.List)5 HttpCallback (com.azure.android.core.http.HttpCallback)4 HttpHeader (com.azure.android.core.http.HttpHeader)4 HttpPipeline (com.azure.android.core.http.HttpPipeline)4