use of com.azure.android.core.test.models.NetworkCallError in project azure-sdk-for-android by Azure.
the class RecordNetworkCallPolicy method process.
@Override
public void process(HttpPipelinePolicyChain chain) {
final NetworkCallRecord networkCallRecord = new NetworkCallRecord();
Map<String, String> headers = new HashMap<>();
captureRequestHeaders(chain.getRequest().getHeaders(), headers, X_MS_CLIENT_REQUEST_ID, CONTENT_TYPE, X_MS_VERSION, USER_AGENT);
networkCallRecord.setHeaders(headers);
networkCallRecord.setMethod(chain.getRequest().getHttpMethod().toString());
// Remove sensitive information such as SAS token signatures from the recording.
UrlBuilder urlBuilder = UrlBuilder.parse(chain.getRequest().getUrl());
redactedAccountName(urlBuilder);
if (urlBuilder.getQuery().containsKey(SIG)) {
urlBuilder.setQueryParameter(SIG, "REDACTED");
}
networkCallRecord.setUri(urlBuilder.toString().replaceAll("\\?$", ""));
chain.processNextPolicy(chain.getRequest(), new NextPolicyCallback() {
@Override
public PolicyCompleter.CompletionState onSuccess(HttpResponse response, PolicyCompleter completer) {
final HttpResponse bufferedResponse = response.buffer();
Map<String, String> responseData = extractResponseData(bufferedResponse);
networkCallRecord.setResponse(responseData);
String body = responseData.get(BODY);
// Remove pre-added header if this is a waiting or redirection
if (body != null && body.contains("<Status>InProgress</Status>") || Integer.parseInt(responseData.get(STATUS_CODE)) == HttpURLConnection.HTTP_MOVED_TEMP) {
logger.info("Waiting for a response or redirection.");
} else {
recordedData.addNetworkCall(networkCallRecord);
}
return completer.completed(bufferedResponse);
}
@Override
public PolicyCompleter.CompletionState onError(Throwable error, PolicyCompleter completer) {
networkCallRecord.setException(new NetworkCallError(error));
recordedData.addNetworkCall(networkCallRecord);
return completer.completedError(error);
}
});
}
Aggregations