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