use of com.azure.android.core.http.HttpRequest 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.HttpRequest 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.HttpRequest 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);
}
use of com.azure.android.core.http.HttpRequest in project azure-sdk-for-android by Azure.
the class UserAgentPolicyTests method testAppIdNoSdkInfoInUserAgentString.
@Test
void testAppIdNoSdkInfoInUserAgentString() {
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("myappId", null, null)).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, "testAppIdNoSdkInfoInUserAgentString");
if (uAgentOrError.error != null) {
Assertions.fail(uAgentOrError.error);
}
Assertions.assertEquals("myappId azsdk-android-null/null (null; null)", uAgentOrError.userAgent);
}
use of com.azure.android.core.http.HttpRequest in project azure-sdk-for-android by Azure.
the class AddHeadersPolicyTest method clientProvidedMultipleHeader.
@Test
public void clientProvidedMultipleHeader() throws Exception {
String customRequestId = "request-id-value";
final HttpHeaders headers = new HttpHeaders();
headers.put("x-ms-client-request-id", customRequestId);
headers.put("my-header1", "my-header1-value");
headers.put("my-header2", "my-header2-value");
final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new NoOpHttpClient() {
@Override
public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
Assertions.assertEquals(httpRequest.getHeaders().getValue("x-ms-client-request-id"), customRequestId);
Assertions.assertEquals(httpRequest.getHeaders().getValue("my-header1"), "my-header1-value");
Assertions.assertEquals(httpRequest.getHeaders().getValue("my-header2"), "my-header2-value");
httpCallback.onSuccess(mockResponse);
}
}).policies(new AddHeadersPolicy(headers)).policies(new RequestIdPolicy()).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 {
throw new RuntimeException(error);
} finally {
latch.countDown();
}
}
});
awaitOnLatch(latch, "clientProvidedMultipleHeader");
}
Aggregations