Search in sources :

Example 1 with NetworkCallRecord

use of com.azure.android.core.test.models.NetworkCallRecord in project azure-sdk-for-android by Azure.

the class PlaybackClient method playbackHttpResponse.

private HttpResponse playbackHttpResponse(final HttpRequest request) {
    final String incomingUrl = applyReplacementRule(request.getUrl().toString());
    final String incomingMethod = request.getHttpMethod().toString();
    final String matchingUrl = removeHost(incomingUrl);
    NetworkCallRecord networkCallRecord = recordedData.findFirstAndRemoveNetworkCall(record -> record.getMethod().equalsIgnoreCase(incomingMethod) && removeHost(record.getUri()).equalsIgnoreCase(matchingUrl));
    count.incrementAndGet();
    if (networkCallRecord == null) {
        logger.warning("NOT FOUND - Method: {} URL: {}", incomingMethod, incomingUrl);
        logger.warning("Records requested: {}.", count);
        throw new IllegalStateException("==> Unexpected request: " + incomingMethod + " " + incomingUrl);
    }
    if (networkCallRecord.getException() != null) {
        throw logger.logExceptionAsWarning(new RuntimeException(networkCallRecord.getException().get()));
    }
    // Overwrite the request header if any.
    if (networkCallRecord.getHeaders().containsKey(X_MS_CLIENT_REQUEST_ID)) {
        request.setHeader(X_MS_CLIENT_REQUEST_ID, networkCallRecord.getHeaders().get(X_MS_CLIENT_REQUEST_ID));
    }
    if (request.getHeaders().getValue(X_MS_ENCRYPTION_KEY_SHA256) != null) {
        networkCallRecord.getResponse().put(X_MS_ENCRYPTION_KEY_SHA256, request.getHeaders().getValue(X_MS_ENCRYPTION_KEY_SHA256));
    }
    int recordStatusCode = Integer.parseInt(networkCallRecord.getResponse().get("StatusCode"));
    HttpHeaders headers = new HttpHeaders();
    for (Map.Entry<String, String> pair : networkCallRecord.getResponse().entrySet()) {
        if (!pair.getKey().equals("StatusCode") && !pair.getKey().equals("Body")) {
            String rawHeader = pair.getValue();
            for (Map.Entry<String, String> rule : textReplacementRules.entrySet()) {
                if (rule.getValue() != null) {
                    rawHeader = rawHeader.replaceAll(rule.getKey(), rule.getValue());
                }
            }
            headers.put(pair.getKey(), rawHeader);
        }
    }
    String rawBody = networkCallRecord.getResponse().get("Body");
    byte[] bytes = null;
    if (rawBody != null) {
        for (Map.Entry<String, String> rule : textReplacementRules.entrySet()) {
            if (rule.getValue() != null) {
                rawBody = rawBody.replaceAll(rule.getKey(), rule.getValue());
            }
        }
        String contentType = networkCallRecord.getResponse().get("Content-Type");
        /*
             * The Body Content-Type is application/octet-stream or avro/binary, those use a custom format to be written
             * to disk. In older versions of azure-core-test this used Arrays.toString(), bodies saved using this format
             * will begin with '[' and end with ']'. The new format for persisting these Content-Types is Base64
             * encoding. Base64 encoding is more compact as Arrays.toString() will separate each byte with ', ', adding
             * (2 * byte[].length) - 1 additional characters, additionally each byte on average takes 2-3 characters to
             * be written to disk [-128,127). Base64 encoding only takes about 4 characters per 3 bytes, this results
             * in a drastically smaller size on disk. In addition to a smaller size on disk, loading the body when it
             * is Base64 encoded is much faster as it doesn't require string splitting.
             */
        if (contentType != null && (contentType.equalsIgnoreCase("application/octet-stream") || contentType.equalsIgnoreCase("avro/binary"))) {
            if (rawBody.startsWith("[") && rawBody.endsWith("]")) {
                /*
                     * Body is encoded using the old Arrays.toString() format. Remove the leading '[' and trailing ']'
                     * and split the string into individual bytes using ', '.
                     */
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                for (String piece : rawBody.substring(1, rawBody.length() - 1).split(", ")) {
                    outputStream.write(Byte.parseByte(piece));
                }
                bytes = outputStream.toByteArray();
            } else {
                /*
                     * Body is encoded using the Base64 encoded format, simply Base64 decode it.
                     */
                bytes = Base64.getDecoder().decode(rawBody);
            }
        } else {
            bytes = rawBody.getBytes(StandardCharsets.UTF_8);
        }
        if (bytes.length > 0) {
            headers.put("Content-Length", String.valueOf(bytes.length));
        }
    }
    return new MockHttpResponse(request, recordStatusCode, headers, bytes);
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) NetworkCallRecord(com.azure.android.core.test.models.NetworkCallRecord) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with NetworkCallRecord

use of com.azure.android.core.test.models.NetworkCallRecord 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);
        }
    });
}
Also used : HashMap(java.util.HashMap) HttpResponse(com.azure.android.core.http.HttpResponse) NetworkCallRecord(com.azure.android.core.test.models.NetworkCallRecord) UrlBuilder(com.azure.android.core.http.util.UrlBuilder) NextPolicyCallback(com.azure.android.core.http.NextPolicyCallback) HashMap(java.util.HashMap) Map(java.util.Map) PolicyCompleter(com.azure.android.core.http.PolicyCompleter) NetworkCallError(com.azure.android.core.test.models.NetworkCallError)

Aggregations

NetworkCallRecord (com.azure.android.core.test.models.NetworkCallRecord)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 HttpHeaders (com.azure.android.core.http.HttpHeaders)1 HttpResponse (com.azure.android.core.http.HttpResponse)1 NextPolicyCallback (com.azure.android.core.http.NextPolicyCallback)1 PolicyCompleter (com.azure.android.core.http.PolicyCompleter)1 UrlBuilder (com.azure.android.core.http.util.UrlBuilder)1 NetworkCallError (com.azure.android.core.test.models.NetworkCallError)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1