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