use of com.azure.android.core.http.HttpCallback in project azure-sdk-for-android by Azure.
the class RequestIdPolicyTests method newRequestIdForEachCall.
@Test
public void newRequestIdForEachCall() {
HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {
String firstRequestId = null;
@Override
public void send(HttpRequest request, CancellationToken cancellationToken, HttpCallback httpCallback) {
if (firstRequestId != null) {
String newRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
Assertions.assertNotNull(newRequestId, "newRequestId should not be null");
Assertions.assertNotEquals(newRequestId, firstRequestId);
}
firstRequestId = request.getHeaders().getValue(REQUEST_ID_HEADER);
if (firstRequestId == null) {
Assertions.fail("The firstRequestId should not be null.");
}
httpCallback.onSuccess(mockResponse);
}
}).policies(new RequestIdPolicy()).build();
CountDownLatch latch1 = new CountDownLatch(1);
pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {
@Override
public void onSuccess(HttpResponse response) {
latch1.countDown();
}
@Override
public void onError(Throwable error) {
try {
throw new RuntimeException(error);
} finally {
latch1.countDown();
}
}
});
awaitOnLatch(latch1, "newRequestIdForEachCall");
CountDownLatch latch2 = new CountDownLatch(1);
pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {
@Override
public void onSuccess(HttpResponse response) {
latch2.countDown();
}
@Override
public void onError(Throwable error) {
try {
throw new RuntimeException(error);
} finally {
latch2.countDown();
}
}
});
awaitOnLatch(latch2, "newRequestIdForEachCall");
}
use of com.azure.android.core.http.HttpCallback in project azure-sdk-for-android by Azure.
the class RetryPolicyTests method retryMax.
@Test
public void retryMax() {
final int maxRetries = 5;
final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {
int count = -1;
@Override
public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
Assertions.assertTrue(count++ < maxRetries);
httpCallback.onSuccess(new MockHttpResponse(httpRequest, 500));
}
}).policies(new RetryPolicy(new FixedDelay(maxRetries, Duration.of(0, ChronoUnit.MILLIS)))).build();
final HttpResponse[] httpResponse = new HttpResponse[1];
CountDownLatch latch = new CountDownLatch(1);
pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {
@Override
public void onSuccess(HttpResponse response) {
httpResponse[0] = response;
latch.countDown();
}
@Override
public void onError(Throwable error) {
try {
throw new RuntimeException(error);
} finally {
latch.countDown();
}
}
});
awaitOnLatch(latch, "retryMax");
assertNotNull(httpResponse[0]);
assertEquals(500, httpResponse[0].getStatusCode());
}
use of com.azure.android.core.http.HttpCallback in project azure-sdk-for-android by Azure.
the class RetryPolicyTests method retryConsumesBody.
@Test
public void retryConsumesBody() {
final AtomicInteger bodyConsumptionCount = new AtomicInteger();
final InputStream errorBody = new ByteArrayInputStream("Should be consumed".getBytes(StandardCharsets.UTF_8)) {
@Override
public void close() throws IOException {
bodyConsumptionCount.incrementAndGet();
super.close();
}
};
final HttpPipeline pipeline = new HttpPipelineBuilder().policies(new RetryPolicy(new FixedDelay(2, Duration.ofMillis(1)))).httpClient(new NoOpHttpClient() {
@Override
public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
httpCallback.onSuccess(new HttpResponse(httpRequest) {
@Override
public int getStatusCode() {
return 503;
}
@Override
public String getHeaderValue(String name) {
return getHeaders().getValue(name);
}
@Override
public HttpHeaders getHeaders() {
return new HttpHeaders();
}
@Override
public InputStream getBody() {
return errorBody;
}
@Override
public byte[] getBodyAsByteArray() {
return collectBytesInInputStream(getBody());
}
@Override
public String getBodyAsString() {
return getBodyAsString(StandardCharsets.UTF_8);
}
@Override
public String getBodyAsString(Charset charset) {
return new String(getBodyAsByteArray(), charset);
}
@Override
public void close() {
try {
errorBody.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
}).build();
CountDownLatch latch = new CountDownLatch(1);
pipeline.send(new HttpRequest(HttpMethod.GET, "https://example.com"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {
@Override
public void onSuccess(HttpResponse response) {
latch.countDown();
}
@Override
public void onError(Throwable error) {
latch.countDown();
}
});
awaitOnLatch(latch, "retryConsumesBody");
assertEquals(2, bodyConsumptionCount.get());
}
use of com.azure.android.core.http.HttpCallback in project azure-sdk-for-android by Azure.
the class UserAgentPolicyTests method testDefaultUserAgentString.
@Test
void testDefaultUserAgentString() {
UAgentOrError uAgentOrError = new UAgentOrError();
final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {
@Override
public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
uAgentOrError.userAgent = httpRequest.getHeaders().getValue("User-Agent");
httpCallback.onSuccess(new MockHttpResponse(httpRequest, 200));
}
}).policies(new UserAgentPolicy()).build();
CountDownLatch latch = new CountDownLatch(1);
pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {
@Override
public void onSuccess(HttpResponse response) {
latch.countDown();
}
@Override
public void onError(Throwable error) {
try {
uAgentOrError.error = error;
} finally {
latch.countDown();
}
}
});
awaitOnLatch(latch, "testDefaultUserAgentString");
if (uAgentOrError.error != null) {
Assertions.fail(uAgentOrError.error);
}
Assertions.assertEquals("azsdk-android", uAgentOrError.userAgent);
}
use of com.azure.android.core.http.HttpCallback in project azure-sdk-for-android by Azure.
the class UserAgentPolicyTests method testNoAppIdInUserAgentString.
@Test
void testNoAppIdInUserAgentString() {
UAgentOrError uAgentOrError = new UAgentOrError();
final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {
@Override
public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
uAgentOrError.userAgent = httpRequest.getHeaders().getValue("User-Agent");
httpCallback.onSuccess(new MockHttpResponse(httpRequest, 200));
}
}).policies(new UserAgentPolicy(null, "azure-storage-blob", "12.0.0")).build();
CountDownLatch latch = new CountDownLatch(1);
pipeline.send(new HttpRequest(HttpMethod.GET, "http://localhost/"), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {
@Override
public void onSuccess(HttpResponse response) {
latch.countDown();
}
@Override
public void onError(Throwable error) {
try {
uAgentOrError.error = error;
} finally {
latch.countDown();
}
}
});
awaitOnLatch(latch, "testNoAppIdInUserAgentString");
if (uAgentOrError.error != null) {
Assertions.fail(uAgentOrError.error);
}
Assertions.assertEquals("azsdk-android-azure-storage-blob/12.0.0 (null; null)", uAgentOrError.userAgent);
}
Aggregations