Search in sources :

Example 1 with HttpHeaders

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

the class HttpRequestMapperTests method headerSubstitution.

@ParameterizedTest
@MethodSource("headerSubstitutionSupplier")
public void headerSubstitution(Method method, Object[] arguments, Map<String, String> expectedHeaders) {
    HttpRequestMapper mapper = new HttpRequestMapper("https://raw.host.com", method, new JacksonSerder());
    HttpHeaders actual = new HttpHeaders();
    mapper.applyHeaderMappings(arguments, actual);
    for (HttpHeader header : actual) {
        assertEquals(expectedHeaders.get(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 2 with HttpHeaders

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

the class HttpRequestMapperTests method headerSubstitutionSupplier.

private static Stream<Arguments> headerSubstitutionSupplier() throws NoSuchMethodException {
    Class<HeaderSubstitutionMethods> clazz = HeaderSubstitutionMethods.class;
    Method addHeaders = clazz.getDeclaredMethod("addHeaders", String.class, boolean.class);
    Method overrideHeaders = clazz.getDeclaredMethod("overrideHeaders", String.class, boolean.class);
    Method headerMap = clazz.getDeclaredMethod("headerMap", Map.class);
    Map<String, String> simpleHeaderMap = Collections.singletonMap("key", "value");
    Map<String, String> expectedSimpleHeadersMap = Collections.singletonMap("x-ms-meta-key", "value");
    Map<String, String> complexHeaderMap = new HttpHeaders().put("key1", null).put("key2", "value2").toMap();
    Map<String, String> expectedComplexHeaderMap = Collections.singletonMap("x-ms-meta-key2", "value2");
    return Stream.of(Arguments.of(addHeaders, null, null), Arguments.of(addHeaders, toObjectArray("header", true), createExpectedParameters("header", true)), Arguments.of(addHeaders, toObjectArray(null, true), createExpectedParameters(null, true)), Arguments.of(addHeaders, toObjectArray("{sub1}", false), createExpectedParameters("{sub1}", false)), Arguments.of(overrideHeaders, null, createExpectedParameters("sub1", false)), Arguments.of(overrideHeaders, toObjectArray(null, true), createExpectedParameters("sub1", true)), Arguments.of(overrideHeaders, toObjectArray("header", false), createExpectedParameters("header", false)), Arguments.of(overrideHeaders, toObjectArray("{sub1}", true), createExpectedParameters("{sub1}", true)), Arguments.of(headerMap, null, null), Arguments.of(headerMap, toObjectArray(simpleHeaderMap), expectedSimpleHeadersMap), Arguments.of(headerMap, toObjectArray(complexHeaderMap), expectedComplexHeaderMap));
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) Method(java.lang.reflect.Method) HttpMethod(com.azure.android.core.http.HttpMethod)

Example 3 with HttpHeaders

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

the class HttpResponseMapperTests method dateTimeRfc1123EncodedContentMap.

@Test
public void dateTimeRfc1123EncodedContentMap() throws Throwable {
    Class<DateTimeRfc1123EncodedContentMethods> clazz = DateTimeRfc1123EncodedContentMethods.class;
    Method dateTimeRfc1123MapMethod = clazz.getDeclaredMethod("dateTimeRfc1123Map", Callback.class);
    HttpResponseMapper mapperDateTimeRfc1123 = new HttpResponseMapper(dateTimeRfc1123MapMethod, extractCallbackType(dateTimeRfc1123MapMethod), logger);
    JacksonSerder jacksonSerder = new JacksonSerder();
    OffsetDateTime offsetDateTime0 = OffsetDateTime.parse("1980-01-01T10:00:00Z");
    OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("1981-01-01T10:00:00Z");
    Map<String, String> wireDateTimeRfc1123Map = new HashMap<>();
    wireDateTimeRfc1123Map.put("v0", new DateTimeRfc1123(offsetDateTime0).toString());
    wireDateTimeRfc1123Map.put("v1", new DateTimeRfc1123(offsetDateTime1).toString());
    String wireDateTimeRfc1123JsonMap = jacksonSerder.serialize(wireDateTimeRfc1123Map, SerdeEncoding.JSON);
    MockHttpResponse httpResponseOffsetDateTimeMap = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireDateTimeRfc1123JsonMap.getBytes());
    Response<Map<String, OffsetDateTime>> restResponseBase64OffsetDataTimeMap = (Response<Map<String, OffsetDateTime>>) mapperDateTimeRfc1123.map(httpResponseOffsetDateTimeMap, jacksonSerder);
    Map<String, OffsetDateTime> dateTimeMapReceived = restResponseBase64OffsetDataTimeMap.getValue();
    assertNotNull(dateTimeMapReceived);
    assertEquals(2, dateTimeMapReceived.size());
    assertEquals(0, dateTimeMapReceived.get("v0").compareTo(offsetDateTime0));
    assertEquals(0, dateTimeMapReceived.get("v1").compareTo(offsetDateTime1));
}
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) OffsetDateTime(org.threeten.bp.OffsetDateTime) DateTimeRfc1123(com.azure.android.core.util.DateTimeRfc1123) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with HttpHeaders

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

the class HttpResponseMapperTests method base64EncodedContent.

@Test
public void base64EncodedContent() throws Throwable {
    Class<Base64EncodedContentMethods> clazz = Base64EncodedContentMethods.class;
    Method base64UrlMethod = clazz.getDeclaredMethod("base64Url", Callback.class);
    HttpResponseMapper mapperBase64 = new HttpResponseMapper(base64UrlMethod, extractCallbackType(base64UrlMethod), logger);
    JacksonSerder jacksonSerder = new JacksonSerder();
    final String valueToEncode = "hello azure android";
    // aGVsbG8gYXp1cmUgYW5kcm9pZA==  (plain bytes).
    final byte[] base64EncodedBytes = Base64Url.encode(valueToEncode.getBytes()).encodedBytes();
    MockHttpResponse httpResponseBase64EncodedBytes0 = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), base64EncodedBytes);
    Response<byte[]> restResponseBase64DecodedBytes0 = (Response<byte[]>) mapperBase64.map(httpResponseBase64EncodedBytes0, jacksonSerder);
    byte[] decodedBytes0 = restResponseBase64DecodedBytes0.getValue();
    assertNotNull(decodedBytes0);
    assertArrayEquals(valueToEncode.getBytes(), decodedBytes0);
    // "aGVsbG8gYXp1cmUgYW5kcm9pZA==" (bytes wrapped in a string, so a valid json string).
    byte[] base64EncodedBytesAsJsonString = new byte[base64EncodedBytes.length + 2];
    System.arraycopy(base64EncodedBytes, 0, base64EncodedBytesAsJsonString, 1, base64EncodedBytes.length);
    base64EncodedBytesAsJsonString[0] = '"';
    base64EncodedBytesAsJsonString[base64EncodedBytesAsJsonString.length - 1] = '"';
    MockHttpResponse httpResponseBase64EncodedBytes1 = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), base64EncodedBytesAsJsonString);
    Response<byte[]> restResponseBase64DecodedBytes1 = (Response<byte[]>) mapperBase64.map(httpResponseBase64EncodedBytes1, jacksonSerder);
    byte[] decodedBytes1 = restResponseBase64DecodedBytes1.getValue();
    assertNotNull(decodedBytes1);
    assertArrayEquals(valueToEncode.getBytes(), decodedBytes1);
}
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 5 with HttpHeaders

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

the class HttpResponseMapperTests method anyError.

@Test
public void anyError() throws Throwable {
    Class<AnyErrorMethods> clazz = AnyErrorMethods.class;
    Method getAnyErrorMethod = clazz.getDeclaredMethod("getAnyError", Callback.class);
    HttpResponseMapper mapper = new HttpResponseMapper(getAnyErrorMethod, extractCallbackType(getAnyErrorMethod), logger);
    MockHttpResponse httpResponse = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 409, new HttpHeaders(), "Unknown error".getBytes());
    HttpResponseException ex = null;
    try {
        mapper.map(httpResponse, new JacksonSerder());
    } catch (HttpResponseException e) {
        ex = e;
    }
    assertNotNull(ex);
    assertNotNull(ex.getMessage());
    assertTrue(ex.getMessage().contains("Unknown error"));
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) HttpResponseException(com.azure.android.core.http.exception.HttpResponseException) Method(java.lang.reflect.Method) HttpMethod(com.azure.android.core.http.HttpMethod) Test(org.junit.jupiter.api.Test) 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