| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Globalization; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Text; |
| | 8 | | using Newtonsoft.Json; |
| | 9 | | using Newtonsoft.Json.Linq; |
| | 10 | |
|
| | 11 | | namespace Azure.Core.TestFramework |
| | 12 | | { |
| | 13 | | public class RecordedTestSanitizer |
| | 14 | | { |
| | 15 | | public const string SanitizeValue = "Sanitized"; |
| 11666 | 16 | | public List<string> JsonPathSanitizers { get; } = new List<string>(); |
| | 17 | |
|
| 59 | 18 | | private static readonly string[] s_sanitizeValueArray = { SanitizeValue }; |
| | 19 | |
|
| 654214 | 20 | | public List<string> SanitizedHeaders { get; } = new List<string> { "Authorization" }; |
| | 21 | |
|
| | 22 | | public virtual string SanitizeUri(string uri) |
| | 23 | | { |
| 322186 | 24 | | return uri; |
| | 25 | | } |
| | 26 | |
|
| | 27 | | public virtual void SanitizeHeaders(IDictionary<string, string[]> headers) |
| | 28 | | { |
| 2614064 | 29 | | foreach (var header in SanitizedHeaders) |
| | 30 | | { |
| 654276 | 31 | | if (headers.ContainsKey(header)) |
| | 32 | | { |
| 321606 | 33 | | headers[header] = s_sanitizeValueArray; |
| | 34 | | } |
| | 35 | | } |
| 652756 | 36 | | } |
| | 37 | |
|
| | 38 | | public virtual string SanitizeTextBody(string contentType, string body) |
| | 39 | | { |
| 10648 | 40 | | if (JsonPathSanitizers.Count == 0) |
| 10414 | 41 | | return body; |
| | 42 | | try |
| | 43 | | { |
| 234 | 44 | | var jsonO = JObject.Parse(body); |
| 1372 | 45 | | foreach (string jsonPath in JsonPathSanitizers) |
| | 46 | | { |
| 1380 | 47 | | foreach (JToken token in jsonO.SelectTokens(jsonPath)) |
| | 48 | | { |
| 236 | 49 | | token.Replace(JToken.FromObject(SanitizeValue)); |
| | 50 | | } |
| | 51 | | } |
| 232 | 52 | | return JsonConvert.SerializeObject(jsonO); |
| | 53 | | } |
| 2 | 54 | | catch |
| | 55 | | { |
| 2 | 56 | | return body; |
| | 57 | | } |
| 234 | 58 | | } |
| | 59 | |
|
| | 60 | | public virtual byte[] SanitizeBody(string contentType, byte[] body) |
| | 61 | | { |
| 7104 | 62 | | return body; |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | public virtual string SanitizeVariable(string variableName, string environmentVariableValue) => environmentVaria |
| | 66 | |
|
| | 67 | | public virtual void SanitizeBody(RecordEntryMessage message) |
| | 68 | | { |
| 652756 | 69 | | if (message.Body != null) |
| | 70 | | { |
| 19162 | 71 | | message.TryGetContentType(out string contentType); |
| | 72 | |
|
| 19162 | 73 | | if (message.TryGetBodyAsText(out string text)) |
| | 74 | | { |
| 12058 | 75 | | message.Body = Encoding.UTF8.GetBytes(SanitizeTextBody(contentType, text)); |
| | 76 | | } |
| | 77 | | else |
| | 78 | | { |
| 7104 | 79 | | message.Body = SanitizeBody(contentType, message.Body); |
| | 80 | | } |
| | 81 | |
|
| 19162 | 82 | | UpdateSanitizedContentLength(message.Headers, message.Body?.Length ?? 0); |
| | 83 | | } |
| 652756 | 84 | | } |
| | 85 | |
|
| | 86 | | public virtual void Sanitize(RecordEntry entry) |
| | 87 | | { |
| 326378 | 88 | | entry.RequestUri = SanitizeUri(entry.RequestUri); |
| | 89 | |
|
| 326378 | 90 | | SanitizeHeaders(entry.Request.Headers); |
| | 91 | |
|
| 326378 | 92 | | SanitizeBody(entry.Request); |
| | 93 | |
|
| 326378 | 94 | | SanitizeHeaders(entry.Response.Headers); |
| | 95 | |
|
| 326378 | 96 | | SanitizeBody(entry.Response); |
| 326378 | 97 | | } |
| | 98 | |
|
| | 99 | | public virtual void Sanitize(RecordSession session) |
| | 100 | | { |
| 16 | 101 | | foreach (RecordEntry entry in session.Entries) |
| | 102 | | { |
| 2 | 103 | | Sanitize(entry); |
| | 104 | | } |
| | 105 | |
|
| 28 | 106 | | foreach (KeyValuePair<string, string> variable in session.Variables.ToArray()) |
| | 107 | | { |
| 8 | 108 | | session.Variables[variable.Key] = SanitizeVariable(variable.Key, variable.Value); |
| | 109 | | } |
| 6 | 110 | | } |
| | 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. |
| 19274 | 123 | | if (headers.ContainsKey("Content-Length")) |
| | 124 | | { |
| 19274 | 125 | | headers["Content-Length"] = new string[] { sanitizedLength.ToString(CultureInfo.InvariantCulture) }; |
| | 126 | | } |
| 19274 | 127 | | } |
| | 128 | | } |
| | 129 | | } |