use of com.azure.android.core.http.HttpCallDispatcher in project azure-sdk-for-android by Azure.
the class HttpLoggingPolicyTests method validateLoggingDoesNotChangeResponse.
/**
* Tests that logging the response body doesn't consume the stream before it is returned from the service call.
*/
@ParameterizedTest(name = "[{index}] {displayName}")
@MethodSource("validateLoggingDoesNotConsumeSupplier")
@ResourceLock("SYSTEM_OUT")
public void validateLoggingDoesNotChangeResponse(byte[] content, byte[] data, int contentLength) {
HttpRequest request = new HttpRequest(HttpMethod.GET, "https://test.com");
HttpHeaders responseHeaders = new HttpHeaders().put("Content-Type", "application/json").put("Content-Length", Integer.toString(contentLength));
HttpPipeline pipeline = new HttpPipelineBuilder().policies(new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY))).httpClient(new HttpClient() {
@Override
public HttpCallDispatcher getHttpCallDispatcher() {
return new HttpCallDispatcher();
}
@Override
public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
httpCallback.onSuccess(new MockHttpResponse(httpRequest, 200, responseHeaders, content));
}
}).build();
CountDownLatch latch = new CountDownLatch(1);
// pipeline.send(request, CONTEXT)
pipeline.send(request, RequestContext.NONE, CancellationToken.NONE, new HttpCallback() {
@Override
public void onSuccess(HttpResponse response) {
try {
assertArrayEquals(data, response.getBodyAsByteArray());
} finally {
latch.countDown();
}
}
@Override
public void onError(Throwable error) {
try {
assertTrue(false, "unexpected call to pipeline::send onError" + error.getMessage());
} finally {
latch.countDown();
}
}
});
awaitOnLatch(latch, "validateLoggingDoesNotChangeResponse");
String logString = convertOutputStreamToString(logCaptureStream);
assertTrue(logString.contains(new String(data, StandardCharsets.UTF_8)));
}
use of com.azure.android.core.http.HttpCallDispatcher in project azure-sdk-for-android by Azure.
the class HttpLoggingPolicyTests method validateLoggingDoesNotChangeRequest.
/**
* Tests that logging the request body doesn't consume the stream before it is sent over the network.
*/
@ParameterizedTest(name = "[{index}] {displayName}")
@MethodSource("validateLoggingDoesNotConsumeSupplier")
@ResourceLock("SYSTEM_OUT")
public void validateLoggingDoesNotChangeRequest(byte[] content, byte[] data, int contentLength) {
final String requestUrl = "https://test.com";
HttpHeaders requestHeaders = new HttpHeaders().put("Content-Type", "application/json").put("Content-Length", Integer.toString(contentLength));
HttpPipeline pipeline = new HttpPipelineBuilder().policies(new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY))).httpClient(new HttpClient() {
@Override
public HttpCallDispatcher getHttpCallDispatcher() {
return new HttpCallDispatcher();
}
@Override
public void send(HttpRequest httpRequest, CancellationToken cancellationToken, HttpCallback httpCallback) {
assertArrayEquals(data, httpRequest.getBody());
httpCallback.onSuccess(new MockHttpResponse(httpRequest, 200));
}
}).build();
CountDownLatch latch = new CountDownLatch(1);
// pipeline.send(new HttpRequest(HttpMethod.POST, requestUrl, requestHeaders, content), CONTEXT)
pipeline.send(new HttpRequest(HttpMethod.POST, requestUrl, requestHeaders, content), 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, "validateLoggingDoesNotChangeRequest");
String logString = convertOutputStreamToString(logCaptureStream);
assertTrue(logString.contains(new String(data, StandardCharsets.UTF_8)));
}
use of com.azure.android.core.http.HttpCallDispatcher in project azure-sdk-for-android by Azure.
the class OkHttpAsyncHttpClientBuilder method build.
/**
* Creates a new OkHttp-backed {@link com.azure.android.core.http.HttpClient} instance on every call, using the
* configuration set in the builder at the time of the build method call.
*
* @return A new OkHttp-backed {@link com.azure.android.core.http.HttpClient} instance.
*/
public HttpClient build() {
OkHttpClient.Builder httpClientBuilder = this.okHttpClient == null ? new OkHttpClient.Builder() : this.okHttpClient.newBuilder();
final OkHttpClient okHttpClient = httpClientBuilder.build();
final HttpCallDispatcher httpCallDispatcher = new HttpCallDispatcher(okHttpClient.dispatcher().executorService());
httpCallDispatcher.setMaxRunningCalls(okHttpClient.dispatcher().getMaxRequests());
return new OkHttpAsyncHttpClient(okHttpClient, httpCallDispatcher);
}
Aggregations