use of com.azure.android.core.http.exception.HttpResponseException in project azure-sdk-for-android by Azure.
the class RestProxyTests method unexpectedHTTPOK.
@Test
public void unexpectedHTTPOK() {
CountDownLatch latch = new CountDownLatch(1);
CallbackResult<InputStream> cbResult = new CallbackResult<>();
createService(UnexpectedOKService.class).getBytes(new Callback<StreamResponse>() {
@Override
public void onSuccess(StreamResponse response) {
cbResult.response = response;
latch.countDown();
}
@Override
public void onFailure(Throwable error) {
cbResult.error = error;
latch.countDown();
}
});
awaitOnLatch(latch, "unexpectedHTTPOK");
if (cbResult.error == null) {
Assertions.fail();
} else {
assertNotNull(cbResult.error);
assertTrue(cbResult.error instanceof HttpResponseException);
assertEquals("Status code 200, (1024-byte body)", cbResult.error.getMessage());
}
}
use of com.azure.android.core.http.exception.HttpResponseException in project azure-sdk-for-android by Azure.
the class HttpResponseMapper method map.
Response<?> map(HttpResponse httpResponse, JacksonSerder jacksonSerder) throws Throwable {
if (!isExpectedStatusCode(httpResponse.getStatusCode())) {
final HttpResponseExceptionInfo exceptionInfo = getExceptionInfo(httpResponse.getStatusCode());
throw logger.logThrowableAsError((exceptionInfo.instantiateException(jacksonSerder, httpResponse, logger)));
} else {
Object headerObject = null;
if (this.headerDecodeType != null) {
try {
headerObject = jacksonSerder.deserialize(httpResponse.getHeaders().toMap(), headerDecodeType);
} catch (IOException ioe) {
throw logger.logExceptionAsError(new HttpResponseException("HTTP response has malformed headers", httpResponse, ioe));
}
}
if (isBooleanResponseForHead(httpResponse)) {
final boolean isSuccess = (httpResponse.getStatusCode() / 100) == 2;
httpResponse.close();
return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, isSuccess);
} else if (TypeUtil.isTypeOrSubTypeOf(this.contentDecodeType, Void.class)) {
httpResponse.close();
return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, null);
} else if (TypeUtil.isTypeOrSubTypeOf(this.contentDecodeType, InputStream.class)) {
return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, httpResponse.getBody());
} else if (TypeUtil.isTypeOrSubTypeOf(this.contentDecodeType, byte[].class)) {
if (this.contentEncodedType == Base64Url.class) {
final byte[] encodedContent = httpResponse.getBodyAsByteArray();
final byte[] decodedContent = new Base64Url(encodedContent).decodedBytes();
return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, decodedContent);
} else {
return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, httpResponse.getBodyAsByteArray());
}
} else if (this.contentEncodedType == null) {
final Object decodedContent = deserializeHttpBody(jacksonSerder, httpResponse, this.contentDecodeType);
return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, decodedContent);
} else {
Objects.requireNonNull(this.contentEncodedType);
if (TypeUtil.isTypeOrSubTypeOf(this.contentEncodedType, Page.class)) {
final Type pageType = (this.contentEncodedType == Page.class) ? TypeUtil.createParameterizedType(ItemPage.class, this.contentDecodeType) : this.contentEncodedType;
final Object decodedContent = deserializeHttpBody(jacksonSerder, httpResponse, pageType);
return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, decodedContent);
} else {
Objects.requireNonNull(this.expandedContentEncodedType);
final Object encodedContent = deserializeHttpBody(jacksonSerder, httpResponse, this.expandedContentEncodedType);
final Object decodedContent = decodeContent(encodedContent, this.contentEncodedType, this.contentDecodeType);
return instantiateResponse(this.responseCtr, this.responseCtrParamCount, httpResponse.getRequest(), httpResponse, headerObject, decodedContent);
}
}
}
}
use of com.azure.android.core.http.exception.HttpResponseException in project azure-sdk-for-android by Azure.
the class RestProxyTests method putRequestWithUnexpectedResponse.
// @Test
// public void putRequestWithBodyLessThanContentLength() {
// CountDownLatch latch = new CountDownLatch(1);
// CallbackResult<HttpBinJSON> cbResult = new CallbackResult<>();
//
// final byte[] body = "test".getBytes(StandardCharsets.UTF_8);
// createService(Service9.class).putBodyAndContentLength(body, 5L,
// 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, "putRequestWithBodyLessThanContentLength");
//
// if (cbResult.error == null) {
// Assertions.fail();
// } else {
// final Throwable error = cbResult.error;
// Assertions.assertNotNull(error);
// Assertions.assertTrue(error instanceof UnexpectedLengthException);
// assertTrue(((UnexpectedLengthException) error).getMessage().contains("less than"));
// }
// }
//
// @Test
// public void putRequestWithBodyMoreThanContentLength() {
// CountDownLatch latch = new CountDownLatch(1);
// CallbackResult<HttpBinJSON> cbResult = new CallbackResult<>();
//
// final byte[] body = "test".getBytes(StandardCharsets.UTF_8);
// createService(Service9.class).putBodyAndContentLength(body, 3L,
// 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, "putRequestWithBodyLessThanContentLength");
//
// if (cbResult.error == null) {
// Assertions.fail();
// } else {
// final Throwable error = cbResult.error;
// Assertions.assertNotNull(error);
// Assertions.assertTrue(error instanceof UnexpectedLengthException);
// assertTrue(((UnexpectedLengthException) error).getMessage().contains("more than"));
// }
// }
@Test
public void putRequestWithUnexpectedResponse() {
CountDownLatch latch = new CountDownLatch(1);
CallbackResult<HttpBinJSON> cbResult = new CallbackResult<>();
createService(Service9.class).putWithUnexpectedResponse("I'm the body!", 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, "putRequestWithUnexpectedResponse");
if (cbResult.response != null) {
fail("Expected HttpResponseException would be thrown.");
} else {
Assertions.assertNotNull(cbResult.error);
Assertions.assertTrue(cbResult.error instanceof HttpResponseException);
HttpResponseException e = (HttpResponseException) cbResult.error;
assertNotNull(e.getValue());
assertTrue(e.getValue() instanceof LinkedHashMap);
@SuppressWarnings("unchecked") final LinkedHashMap<String, String> expectedBody = (LinkedHashMap<String, String>) e.getValue();
assertEquals("I'm the body!", expectedBody.get("data"));
}
}
use of com.azure.android.core.http.exception.HttpResponseException in project azure-sdk-for-android by Azure.
the class ChatAsyncImplClientTest method getNotFoundOnNonExistingChatThreadWithResponse.
@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void getNotFoundOnNonExistingChatThreadWithResponse(HttpClient httpClient) {
setupTest(httpClient);
ExecutionException executionException = assertThrows(ExecutionException.class, () -> {
CompletableFuture<Response<ChatThreadProperties>> completableFuture = client.getChatThreadClient().getChatThreadPropertiesWithResponseAsync("19:00000000000000000000000000000000@thread.v2", null);
completableFuture.get();
});
Throwable cause = executionException.getCause();
assertNotNull(cause);
assertTrue(cause instanceof CommunicationErrorResponseException);
HttpResponseException exception = (HttpResponseException) cause;
assertNotNull(exception.getResponse());
assertEquals(404, exception.getResponse().getStatusCode());
}
use of com.azure.android.core.http.exception.HttpResponseException in project azure-sdk-for-android by Azure.
the class ChatAsyncImplClientTest method getNotFoundOnNonExistingChatThread.
@ParameterizedTest
@MethodSource("com.azure.android.core.test.TestBase#getHttpClients")
public void getNotFoundOnNonExistingChatThread(HttpClient httpClient) {
setupTest(httpClient);
ExecutionException executionException = assertThrows(ExecutionException.class, () -> {
CompletableFuture<ChatThreadProperties> completableFuture = client.getChatThreadClient().getChatThreadPropertiesAsync("19:00000000000000000000000000000000@thread.v2");
completableFuture.get();
});
Throwable cause = executionException.getCause();
assertNotNull(cause);
assertTrue(cause instanceof CommunicationErrorResponseException);
HttpResponseException exception = (HttpResponseException) cause;
assertNotNull(exception.getResponse());
assertEquals(404, exception.getResponse().getStatusCode());
}
Aggregations