-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathRecordedTestSanitizer.cs
164 lines (139 loc) · 5.57 KB
/
RecordedTestSanitizer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Azure.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace Azure.Sdk.Tools.TestProxy.Common
{
/// <summary>
/// The default sanitizer that is always applied. Removes the header "Authorization" and replaces it with the value "Sanitized".
/// </summary>
public class RecordedTestSanitizer
{
public const string SanitizeValue = "Sanitized";
public List<string> JsonPathSanitizers { get; } = new List<string>();
public ApplyCondition Condition { get; protected set; } = null;
/// This is just a temporary workaround to avoid breaking tests that need to be re-recorded
// when updating the JsonPathSanitizer logic to avoid changing date formats when deserializing requests.
// this property will be removed in the future.
public bool LegacyConvertJsonDateTokens { get; set; }
private static readonly string[] s_sanitizeValueArray = { SanitizeValue };
public static readonly JsonSerializerSettings SerializerSettings = new JsonSerializerSettings
{
DateParseHandling = DateParseHandling.None
};
public List<string> SanitizedHeaders { get; } = new List<string> { "Authorization" };
public virtual string SanitizeUri(string uri)
{
return uri;
}
public virtual void SanitizeHeaders(IDictionary<string, string[]> headers)
{
foreach (var header in SanitizedHeaders)
{
if (headers.ContainsKey(header))
{
headers[header] = s_sanitizeValueArray;
}
}
}
public virtual string SanitizeTextBody(string contentType, string body)
{
if (JsonPathSanitizers.Count == 0)
return body;
try
{
JToken jsonO;
// Prevent default behavior where JSON.NET will convert DateTimeOffset
// into a DateTime.
if (!LegacyConvertJsonDateTokens)
{
jsonO = JsonConvert.DeserializeObject<JToken>(body, SerializerSettings);
}
else
{
jsonO = JToken.Parse(body);
}
foreach (string jsonPath in JsonPathSanitizers)
{
foreach (JToken token in jsonO.SelectTokens(jsonPath))
{
token.Replace(JToken.FromObject(SanitizeValue));
}
}
return JsonConvert.SerializeObject(jsonO, SerializerSettings);
}
catch
{
return body;
}
}
public virtual byte[] SanitizeBody(string contentType, byte[] body)
{
return body;
}
public virtual string SanitizeVariable(string variableName, string environmentVariableValue) => environmentVariableValue;
public virtual void SanitizeBody(RequestOrResponse message)
{
if (message.Body != null)
{
message.TryGetContentType(out string contentType);
if (message.TryGetBodyAsText(out string text))
{
message.Body = Encoding.UTF8.GetBytes(SanitizeTextBody(contentType, text));
}
else
{
message.Body = SanitizeBody(contentType, message.Body);
}
UpdateSanitizedContentLength(message);
}
}
public virtual void Sanitize(RecordEntry entry)
{
if (Condition == null || Condition.IsApplicable(entry))
{
entry.RequestUri = SanitizeUri(entry.RequestUri);
SanitizeHeaders(entry.Request.Headers);
SanitizeBody(entry.Request);
SanitizeHeaders(entry.Response.Headers);
if (entry.RequestMethod != RequestMethod.Head)
{
SanitizeBody(entry.Response);
}
}
}
public virtual void Sanitize(RecordSession session)
{
foreach (RecordEntry entry in session.Entries)
{
Sanitize(entry);
}
foreach (KeyValuePair<string, string> variable in session.Variables.ToArray())
{
session.Variables[variable.Key] = SanitizeVariable(variable.Key, variable.Value);
}
}
/// <summary>
/// Optionally update the Content-Length header if we've sanitized it
/// and the new value is a different length from the original
/// Content-Length header. We don't add a Content-Length header if it
/// wasn't already present.
/// </summary>
/// <param name="requestOrResponse">The Request or Response message</param>
protected internal static void UpdateSanitizedContentLength(RequestOrResponse requestOrResponse)
{
var headers = requestOrResponse.Headers;
int sanitizedLength = requestOrResponse.Body?.Length ?? 0;
// Only update Content-Length if already present.
if (headers.ContainsKey("Content-Length"))
{
headers["Content-Length"] = new string[] { sanitizedLength.ToString(CultureInfo.InvariantCulture) };
}
}
}
}