use of com.azure.android.core.http.HttpPipelineBuilder in project azure-sdk-for-android by Azure.
the class RetryPolicyTests method exponentialDelayRetry.
@Test
public void exponentialDelayRetry() {
final int maxRetries = 5;
final long baseDelayMillis = 100;
final long maxDelayMillis = 1000;
ExponentialBackoff exponentialBackoff = new ExponentialBackoff(maxRetries, Duration.ofMillis(baseDelayMillis), Duration.ofMillis(maxDelayMillis));
final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {
int count = -1;
long previousAttemptMadeAt = -1;
@Override
public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
if (count > 0) {
long requestMadeAt = System.currentTimeMillis();
long expectedToBeMadeAt = previousAttemptMadeAt + ((1 << (count - 1)) * (long) (baseDelayMillis * 0.95));
Assertions.assertTrue(requestMadeAt >= expectedToBeMadeAt);
}
Assertions.assertTrue(count++ < maxRetries);
previousAttemptMadeAt = System.currentTimeMillis();
httpCallback.onSuccess(new MockHttpResponse(httpRequest, 503));
}
}).policies(new RetryPolicy(exponentialBackoff)).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) {
latch.countDown();
}
});
awaitOnLatch(latch, "exponentialDelayRetry");
}
use of com.azure.android.core.http.HttpPipelineBuilder in project azure-sdk-for-android by Azure.
the class RetryPolicyTests method retryEndOn501.
@Test
public void retryEndOn501() {
final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {
// Send 408, 500, 502, all retried, with a 501 ending
private final int[] codes = new int[] { 408, 500, 502, 501 };
private int count = 0;
@Override
public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
httpCallback.onSuccess(new MockHttpResponse(httpRequest, codes[count++]));
}
}).policies(new RetryPolicy(new FixedDelay(3, 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, "retryEndOn501");
assertNotNull(httpResponse[0]);
assertEquals(501, httpResponse[0].getStatusCode());
}
use of com.azure.android.core.http.HttpPipelineBuilder in project azure-sdk-for-android by Azure.
the class HttpLoggingPolicyTests method redactQueryParameters.
/**
* Tests that a query string will be properly redacted before it is logged.
*/
@ParameterizedTest
@MethodSource("redactQueryParametersSupplier")
@ResourceLock("SYSTEM_OUT")
public void redactQueryParameters(String requestUrl, String expectedQueryString, Set<String> allowedQueryParameters) {
HttpPipeline pipeline = new HttpPipelineBuilder().policies(new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC).setAllowedQueryParamNames(allowedQueryParameters))).httpClient(new NoOpHttpClient()).build();
CountDownLatch latch = new CountDownLatch(1);
// pipeline.send(new HttpRequest(HttpMethod.POST, requestUrl), CONTEXT, new HttpCallback() {..})
pipeline.send(new HttpRequest(HttpMethod.POST, requestUrl), RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {
@Override
public void onSuccess(HttpResponse response) {
latch.countDown();
}
@Override
public void onError(Throwable error) {
try {
assertTrue(false, "unexpected call to pipeline::send onError" + error.getMessage());
} finally {
latch.countDown();
}
}
});
awaitOnLatch(latch, "redactQueryParameters");
assertTrue(convertOutputStreamToString(logCaptureStream).contains(expectedQueryString));
}
use of com.azure.android.core.http.HttpPipelineBuilder in project azure-sdk-for-android by Azure.
the class AzureCommunicationChatServiceImplBuilder method createHttpPipeline.
private HttpPipeline createHttpPipeline() {
if (httpLogOptions == null) {
httpLogOptions = new HttpLogOptions();
}
List<HttpPipelinePolicy> policies = new ArrayList<>();
String clientName = properties.get(SDK_NAME);
if (clientName == null) {
clientName = "UnknownName";
}
String clientVersion = properties.get(SDK_VERSION);
if (clientVersion == null) {
clientVersion = "UnknownVersion";
}
policies.add(new UserAgentPolicy(null, clientName, clientVersion));
policies.add(retryPolicy == null ? RetryPolicy.withExponentialBackoff() : retryPolicy);
policies.add(new CookiePolicy());
policies.addAll(this.pipelinePolicies);
policies.add(new HttpLoggingPolicy(httpLogOptions));
HttpPipeline httpPipeline = new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])).httpClient(httpClient).build();
return httpPipeline;
}
use of com.azure.android.core.http.HttpPipelineBuilder in project azure-sdk-for-android by Azure.
the class ChatClientBuilder method createHttpPipeline.
private HttpPipeline createHttpPipeline(HttpClient httpClient, HttpPipelinePolicy authorizationPolicy, List<HttpPipelinePolicy> additionalPolicies) {
List<HttpPipelinePolicy> policies = new ArrayList<HttpPipelinePolicy>();
applyRequiredPolicies(policies, authorizationPolicy);
if (additionalPolicies != null && additionalPolicies.size() > 0) {
policies.addAll(additionalPolicies);
}
return new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])).httpClient(httpClient).build();
}
Aggregations