Search in sources :

Example 1 with StreamResponse

use of com.azure.android.core.rest.StreamResponse 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 2 with StreamResponse

use of com.azure.android.core.rest.StreamResponse in project azure-sdk-for-android by Azure.

the class RestProxyTests method simpleDownloadStreamResponseTest.

@Test
public void simpleDownloadStreamResponseTest() {
    CountDownLatch latch = new CountDownLatch(1);
    CallbackResult<InputStream> cbResult = new CallbackResult<>();
    createService(DownloadService.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, "simpleDownloadStreamResponseTest");
    if (cbResult.error != null) {
        Assertions.fail(cbResult.error);
    } else {
        final Response<InputStream> response = cbResult.response;
        assertNotNull(response);
        assertEquals(200, response.getStatusCode());
        InputStream stream = response.getValue();
        assertNotNull(stream);
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        try {
            int nRead;
            byte[] data = new byte[1024];
            while ((nRead = stream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, nRead);
            }
            buffer.flush();
        } catch (IOException e) {
            fail(e);
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                fail(e);
            }
        }
        byte[] byteArray = buffer.toByteArray();
        assertEquals(30720, byteArray.length);
    }
}
Also used : InputStream(java.io.InputStream) StreamResponse(com.azure.android.core.rest.StreamResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

StreamResponse (com.azure.android.core.rest.StreamResponse)2 InputStream (java.io.InputStream)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.jupiter.api.Test)2 HttpResponseException (com.azure.android.core.http.exception.HttpResponseException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1