< Summary

Class:Azure.Core.TestFramework.RecordedTestSanitizer
Assembly:Azure.Core.TestFramework
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\RecordedTestSanitizer.cs
Covered lines:40
Uncovered lines:1
Coverable lines:41
Total lines:129
Line coverage:97.5% (40 of 41)
Covered branches:21
Total branches:22
Branch coverage:95.4% (21 of 22)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_JsonPathSanitizers()-100%100%
.cctor()-100%100%
get_SanitizedHeaders()-100%100%
SanitizeUri(...)-100%100%
SanitizeHeaders(...)-100%100%
SanitizeTextBody(...)-100%100%
SanitizeBody(...)-100%100%
SanitizeVariable(...)-0%100%
SanitizeBody(...)-100%83.33%
Sanitize(...)-100%100%
Sanitize(...)-100%100%
UpdateSanitizedContentLength(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\RecordedTestSanitizer.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System.Collections.Generic;
 5using System.Globalization;
 6using System.Linq;
 7using System.Text;
 8using Newtonsoft.Json;
 9using Newtonsoft.Json.Linq;
 10
 11namespace Azure.Core.TestFramework
 12{
 13    public class RecordedTestSanitizer
 14    {
 15        public const string SanitizeValue = "Sanitized";
 1166616        public List<string> JsonPathSanitizers { get; } = new List<string>();
 17
 5918        private static readonly string[] s_sanitizeValueArray = { SanitizeValue };
 19
 65421420        public List<string> SanitizedHeaders { get; } = new List<string> { "Authorization" };
 21
 22        public virtual string SanitizeUri(string uri)
 23        {
 32218624            return uri;
 25        }
 26
 27        public virtual void SanitizeHeaders(IDictionary<string, string[]> headers)
 28        {
 261406429            foreach (var header in SanitizedHeaders)
 30            {
 65427631                if (headers.ContainsKey(header))
 32                {
 32160633                    headers[header] = s_sanitizeValueArray;
 34                }
 35            }
 65275636        }
 37
 38        public virtual string SanitizeTextBody(string contentType, string body)
 39        {
 1064840            if (JsonPathSanitizers.Count == 0)
 1041441                return body;
 42            try
 43            {
 23444                var jsonO = JObject.Parse(body);
 137245                foreach (string jsonPath in JsonPathSanitizers)
 46                {
 138047                    foreach (JToken token in jsonO.SelectTokens(jsonPath))
 48                    {
 23649                        token.Replace(JToken.FromObject(SanitizeValue));
 50                    }
 51                }
 23252                return JsonConvert.SerializeObject(jsonO);
 53            }
 254            catch
 55            {
 256                return body;
 57            }
 23458        }
 59
 60        public virtual byte[] SanitizeBody(string contentType, byte[] body)
 61        {
 710462            return body;
 63        }
 64
 065        public virtual string SanitizeVariable(string variableName, string environmentVariableValue) => environmentVaria
 66
 67        public virtual void SanitizeBody(RecordEntryMessage message)
 68        {
 65275669            if (message.Body != null)
 70            {
 1916271                message.TryGetContentType(out string contentType);
 72
 1916273                if (message.TryGetBodyAsText(out string text))
 74                {
 1205875                    message.Body = Encoding.UTF8.GetBytes(SanitizeTextBody(contentType, text));
 76                }
 77                else
 78                {
 710479                    message.Body = SanitizeBody(contentType, message.Body);
 80                }
 81
 1916282                UpdateSanitizedContentLength(message.Headers, message.Body?.Length ?? 0);
 83            }
 65275684        }
 85
 86        public virtual void Sanitize(RecordEntry entry)
 87        {
 32637888            entry.RequestUri = SanitizeUri(entry.RequestUri);
 89
 32637890            SanitizeHeaders(entry.Request.Headers);
 91
 32637892            SanitizeBody(entry.Request);
 93
 32637894            SanitizeHeaders(entry.Response.Headers);
 95
 32637896            SanitizeBody(entry.Response);
 32637897        }
 98
 99        public virtual void Sanitize(RecordSession session)
 100        {
 16101            foreach (RecordEntry entry in session.Entries)
 102            {
 2103                Sanitize(entry);
 104            }
 105
 28106            foreach (KeyValuePair<string, string> variable in session.Variables.ToArray())
 107            {
 8108                session.Variables[variable.Key] = SanitizeVariable(variable.Key, variable.Value);
 109            }
 6110        }
 111
 112        /// <summary>
 113        /// Optionally update the Content-Length header if we've sanitized it
 114        /// and the new value is a different length from the original
 115        /// Content-Length header.  We don't add a Content-Length header if it
 116        /// wasn't already present.
 117        /// </summary>
 118        /// <param name="headers">The Request or Response headers</param>
 119        /// <param name="sanitizedLength">The sanitized Content-Length</param>
 120        protected static void UpdateSanitizedContentLength(IDictionary<string, string[]> headers, int sanitizedLength)
 121        {
 122            // Only update Content-Length if already present.
 19274123            if (headers.ContainsKey("Content-Length"))
 124            {
 19274125                headers["Content-Length"] = new string[] { sanitizedLength.ToString(CultureInfo.InvariantCulture) };
 126            }
 19274127        }
 128    }
 129}