Search in sources :

Example 6 with HttpResponseException

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());
    }
}
Also used : InputStream(java.io.InputStream) StreamResponse(com.azure.android.core.rest.StreamResponse) HttpResponseException(com.azure.android.core.http.exception.HttpResponseException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 7 with HttpResponseException

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);
            }
        }
    }
}
Also used : UnexpectedResponseExceptionType(com.azure.android.core.rest.annotation.UnexpectedResponseExceptionType) ReturnValueWireType(com.azure.android.core.rest.annotation.ReturnValueWireType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ItemPage(com.azure.android.core.rest.implementation.ItemPage) HttpResponseExceptionInfo(com.azure.android.core.rest.implementation.HttpResponseExceptionInfo) HttpResponseException(com.azure.android.core.http.exception.HttpResponseException) IOException(java.io.IOException) Base64Url(com.azure.android.core.util.Base64Url)

Example 8 with HttpResponseException

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"));
    }
}
Also used : HttpResponseException(com.azure.android.core.http.exception.HttpResponseException) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedHashMap(java.util.LinkedHashMap) Response(com.azure.android.core.rest.Response) StreamResponse(com.azure.android.core.rest.StreamResponse) HttpBinJSON(com.azure.android.core.test.implementation.entities.HttpBinJSON) Test(org.junit.jupiter.api.Test)

Example 9 with HttpResponseException

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());
}
Also used : Response(com.azure.android.core.rest.Response) PagedResponse(com.azure.android.core.rest.util.paging.PagedResponse) HttpResponseException(com.azure.android.core.http.exception.HttpResponseException) ExecutionException(java.util.concurrent.ExecutionException) CommunicationErrorResponseException(com.azure.android.communication.chat.implementation.models.CommunicationErrorResponseException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 10 with HttpResponseException

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());
}
Also used : ChatThreadProperties(com.azure.android.communication.chat.implementation.models.ChatThreadProperties) HttpResponseException(com.azure.android.core.http.exception.HttpResponseException) ExecutionException(java.util.concurrent.ExecutionException) CommunicationErrorResponseException(com.azure.android.communication.chat.implementation.models.CommunicationErrorResponseException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

HttpResponseException (com.azure.android.core.http.exception.HttpResponseException)10 Test (org.junit.jupiter.api.Test)6 Response (com.azure.android.core.rest.Response)5 StreamResponse (com.azure.android.core.rest.StreamResponse)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 CommunicationErrorResponseException (com.azure.android.communication.chat.implementation.models.CommunicationErrorResponseException)2 HttpBinJSON (com.azure.android.core.test.implementation.entities.HttpBinJSON)2 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 ChatThreadProperties (com.azure.android.communication.chat.implementation.models.ChatThreadProperties)1 HttpHeaders (com.azure.android.core.http.HttpHeaders)1 HttpMethod (com.azure.android.core.http.HttpMethod)1 HttpRequest (com.azure.android.core.http.HttpRequest)1 HttpResponse (com.azure.android.core.http.HttpResponse)1 ReturnValueWireType (com.azure.android.core.rest.annotation.ReturnValueWireType)1 UnexpectedResponseExceptionType (com.azure.android.core.rest.annotation.UnexpectedResponseExceptionType)1 HttpResponseExceptionInfo (com.azure.android.core.rest.implementation.HttpResponseExceptionInfo)1