Search in sources :

Example 26 with HttpHeaders

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

the class HttpResponseMapper method instantiateResponse.

private Response<?> instantiateResponse(Constructor<? extends Response<?>> responseCtr, int responseCtrParamCount, HttpRequest httpRequest, HttpResponse httpResponse, Object headerAsObject, Object bodyAsObject) {
    final int responseStatusCode = httpResponse.getStatusCode();
    final HttpHeaders responseHeaders = httpResponse.getHeaders();
    switch(responseCtrParamCount) {
        case 3:
            try {
                return responseCtr.newInstance(httpRequest, responseStatusCode, responseHeaders);
            } catch (IllegalAccessException e) {
                throw logger.logExceptionAsError(new RuntimeException("Failed to deserialize 3-parameter response. ", e));
            } catch (InvocationTargetException e) {
                throw logger.logExceptionAsError(new RuntimeException("Failed to deserialize 3-parameter response. ", e));
            } catch (InstantiationException e) {
                throw logger.logExceptionAsError(new RuntimeException("Failed to deserialize 3-parameter response. ", e));
            }
        case 4:
            try {
                return responseCtr.newInstance(httpRequest, responseStatusCode, responseHeaders, bodyAsObject);
            } catch (IllegalAccessException e) {
                throw logger.logExceptionAsError(new RuntimeException("Failed to deserialize 4-parameter response. ", e));
            } catch (InvocationTargetException e) {
                throw logger.logExceptionAsError(new RuntimeException("Failed to deserialize 4-parameter response. ", e));
            } catch (InstantiationException e) {
                throw logger.logExceptionAsError(new RuntimeException("Failed to deserialize 4-parameter response. ", e));
            }
        case 5:
            try {
                return responseCtr.newInstance(httpRequest, responseStatusCode, responseHeaders, bodyAsObject, headerAsObject);
            } catch (IllegalAccessException e) {
                String message = String.format("Failed to deserialize 5-parameter response %s decoded headers. ", headerAsObject != null ? "with" : "without");
                throw logger.logExceptionAsError(new RuntimeException(message, e));
            } catch (InvocationTargetException e) {
                String message = String.format("Failed to deserialize 5-parameter response %s decoded headers. ", headerAsObject != null ? "with" : "without");
                throw logger.logExceptionAsError(new RuntimeException(message, e));
            } catch (InstantiationException e) {
                String message = String.format("Failed to deserialize 5-parameter response %s decoded headers. ", headerAsObject != null ? "with" : "without");
                throw logger.logExceptionAsError(new RuntimeException(message, e));
            }
        default:
            throw logger.logExceptionAsError(new IllegalStateException("Response constructor with expected parameters not found."));
    }
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 27 with HttpHeaders

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

the class HttpResponseMapperTests method base64EncodedContentList.

@Test
public void base64EncodedContentList() throws Throwable {
    Class<Base64EncodedContentMethods> clazz = Base64EncodedContentMethods.class;
    Method base64UrlListMethod = clazz.getDeclaredMethod("base64UrlList", 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";
    List<String> base64EncodedBytesList = new ArrayList<>();
    base64EncodedBytesList.add(Base64Url.encode(value0ToEncode.getBytes()).toString());
    base64EncodedBytesList.add(Base64Url.encode(value1ToEncode.getBytes()).toString());
    String wireBase64EncodedList = jacksonSerder.serialize(base64EncodedBytesList, SerdeEncoding.JSON);
    MockHttpResponse httpResponseBase64EncodedBytesList = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireBase64EncodedList.getBytes());
    Response<List<byte[]>> restResponseBase64DecodedBytesList = (Response<List<byte[]>>) mapperBase64List.map(httpResponseBase64EncodedBytesList, jacksonSerder);
    List<byte[]> decodedBytesList = restResponseBase64DecodedBytesList.getValue();
    assertNotNull(decodedBytesList);
    assertEquals(2, decodedBytesList.size());
    assertArrayEquals(value0ToEncode.getBytes(), decodedBytesList.get(0));
    assertArrayEquals(value1ToEncode.getBytes(), decodedBytesList.get(1));
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) ArrayList(java.util.ArrayList) 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) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 28 with HttpHeaders

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

the class HttpResponseMapperTests method dateTimeRfc1123EncodedContentList.

@Test
public void dateTimeRfc1123EncodedContentList() throws Throwable {
    Class<DateTimeRfc1123EncodedContentMethods> clazz = DateTimeRfc1123EncodedContentMethods.class;
    Method dateTimeRfc1123ListMethod = clazz.getDeclaredMethod("dateTimeRfc1123List", Callback.class);
    HttpResponseMapper mapperDateTimeRfc1123 = new HttpResponseMapper(dateTimeRfc1123ListMethod, extractCallbackType(dateTimeRfc1123ListMethod), logger);
    JacksonSerder jacksonSerder = new JacksonSerder();
    OffsetDateTime offsetDateTime0 = OffsetDateTime.parse("1980-01-01T10:00:00Z");
    OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("1981-01-01T10:00:00Z");
    List<String> wireDateTimeRfc1123List = new ArrayList<>();
    wireDateTimeRfc1123List.add(new DateTimeRfc1123(offsetDateTime0).toString());
    wireDateTimeRfc1123List.add(new DateTimeRfc1123(offsetDateTime1).toString());
    String wireDateTimeRfc1123JsonList = jacksonSerder.serialize(wireDateTimeRfc1123List, SerdeEncoding.JSON);
    MockHttpResponse httpResponseDateTimeRfc1123 = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireDateTimeRfc1123JsonList.getBytes());
    Response<List<OffsetDateTime>> httpResponseOffsetDateTimeList = (Response<List<OffsetDateTime>>) mapperDateTimeRfc1123.map(httpResponseDateTimeRfc1123, jacksonSerder);
    List<OffsetDateTime> dateTimeListReceived = httpResponseOffsetDateTimeList.getValue();
    assertNotNull(dateTimeListReceived);
    assertEquals(2, dateTimeListReceived.size());
    assertEquals(0, dateTimeListReceived.get(0).compareTo(offsetDateTime0));
    assertEquals(0, dateTimeListReceived.get(1).compareTo(offsetDateTime1));
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) ArrayList(java.util.ArrayList) 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) OffsetDateTime(org.threeten.bp.OffsetDateTime) DateTimeRfc1123(com.azure.android.core.util.DateTimeRfc1123) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 29 with HttpHeaders

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

the class HttpRequestMapperTests method headers.

@ParameterizedTest
@MethodSource("headersSupplier")
public void headers(Method method, HttpHeaders expectedHeaders) {
    HttpRequestMapper mapper = new HttpRequestMapper("https://raw.host.com", method, new JacksonSerder());
    HttpHeaders actual = new HttpHeaders();
    mapper.applyHeaderMappings(null, actual);
    for (HttpHeader header : actual) {
        assertEquals(expectedHeaders.getValue(header.getName()), header.getValue());
    }
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) HttpHeader(com.azure.android.core.http.HttpHeader) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 30 with HttpHeaders

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

the class SerdeEncodingTests method recognizeXml.

@ParameterizedTest
@ValueSource(strings = { "application/xml", "application/atom+xml", "text/xml", "application/foo+XML", "TEXT/XML", "application/xml;charset=utf-8", "application/atom+xml; charset=utf-32" })
void recognizeXml(String mimeType) {
    // Arrange
    HttpHeaders headers = new HttpHeaders(Collections.singletonMap(CONTENT_TYPE, mimeType));
    // Act & Assert
    Assertions.assertEquals(SerdeEncoding.XML, SerdeEncoding.fromHeaders(headers.toMap()));
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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