use of com.azure.android.core.http.exception.HttpResponseException 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"));
}
use of com.azure.android.core.http.exception.HttpResponseException in project azure-sdk-for-android by Azure.
the class RestProxyTests method putRequestWithUnexpectedResponseAndNoFallthroughExceptionType.
@Test
public void putRequestWithUnexpectedResponseAndNoFallthroughExceptionType() {
CountDownLatch latch = new CountDownLatch(1);
CallbackResult<HttpBinJSON> cbResult = new CallbackResult<>();
createService(Service9.class).putWithUnexpectedResponseAndNoFallthroughExceptionType("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, "putRequestWithUnexpectedResponseAndNoFallthroughExceptionType");
if (cbResult.response != null) {
fail("Expected HttpResponseException would be thrown.");
} else {
Assertions.assertNotNull(cbResult.error);
Assertions.assertTrue(cbResult.error instanceof HttpResponseException, "Expected HttpResponseException would be thrown. Instead got " + cbResult.error.getClass().getSimpleName());
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 RestProxyTests method service18GetStatus500.
@Test
public void service18GetStatus500() {
CountDownLatch latch = new CountDownLatch(1);
CallbackResult<Void> cbResult = new CallbackResult<>();
createService(Service18.class).getStatus500(new Callback<Response<Void>>() {
@Override
public void onSuccess(Response<Void> response) {
cbResult.response = response;
latch.countDown();
}
@Override
public void onFailure(Throwable error) {
cbResult.error = error;
latch.countDown();
}
});
awaitOnLatch(latch, "service18GetStatus400");
if (cbResult.response != null) {
fail("Expected HttpResponseException would be thrown.");
} else {
Assertions.assertNotNull(cbResult.error);
Assertions.assertTrue(cbResult.error instanceof HttpResponseException, "Expected HttpResponseException would be thrown. Instead got " + cbResult.error.getClass().getSimpleName());
HttpResponseException e = (HttpResponseException) cbResult.error;
assertNull(e.getValue());
Assertions.assertEquals(500, e.getResponse().getStatusCode());
}
}
use of com.azure.android.core.http.exception.HttpResponseException in project azure-sdk-for-android by Azure.
the class RestProxyTests method service18GetStatus400.
@Test
public void service18GetStatus400() {
CountDownLatch latch = new CountDownLatch(1);
CallbackResult<Void> cbResult = new CallbackResult<>();
createService(Service18.class).getStatus400(new Callback<Response<Void>>() {
@Override
public void onSuccess(Response<Void> response) {
cbResult.response = response;
latch.countDown();
}
@Override
public void onFailure(Throwable error) {
cbResult.error = error;
latch.countDown();
}
});
awaitOnLatch(latch, "service18GetStatus400");
if (cbResult.response != null) {
fail("Expected HttpResponseException would be thrown.");
} else {
Assertions.assertNotNull(cbResult.error);
Assertions.assertTrue(cbResult.error instanceof HttpResponseException, "Expected HttpResponseException would be thrown. Instead got " + cbResult.error.getClass().getSimpleName());
HttpResponseException e = (HttpResponseException) cbResult.error;
assertNull(e.getValue());
Assertions.assertEquals(400, e.getResponse().getStatusCode());
}
}
use of com.azure.android.core.http.exception.HttpResponseException in project azure-sdk-for-android by Azure.
the class RestProxy method invoke.
@Override
public Object invoke(final Object restProxy, final Method swaggerMethod, final Object[] swaggerMethodArgs) {
final SwaggerMethodParser methodParser = this.interfaceParser.getMethodParser(swaggerMethod, this.logger);
final Callback<Response<?>> restCallback;
restCallback = (Callback<Response<?>>) swaggerMethodArgs[methodParser.callbackArgIndex];
Objects.requireNonNull(restCallback);
final CancellationToken cancellationToken;
if (methodParser.cancellationTokenArgIndex == -1) {
cancellationToken = CancellationToken.NONE;
} else {
cancellationToken = (CancellationToken) swaggerMethodArgs[methodParser.cancellationTokenArgIndex];
}
final HttpRequest httpRequest;
try {
httpRequest = methodParser.mapToHttpRequest(swaggerMethodArgs);
} catch (IOException e) {
restCallback.onFailure(e);
return null;
} catch (HttpResponseException e) {
restCallback.onFailure(e);
return null;
}
this.httpPipeline.send(httpRequest, RequestContext.NONE, cancellationToken, new HttpPipelineCallback(methodParser, restCallback));
return null;
}
Aggregations