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());
}
}
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));
}
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));
}
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);
}
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"));
}
Aggregations