-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathRecordEntry.cs
327 lines (282 loc) · 12.1 KB
/
RecordEntry.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Azure.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
namespace Azure.Sdk.Tools.TestProxy.Common
{
public class RecordEntry
{
// Requests and responses are usually formatted using Newtonsoft.Json that has more relaxed encoding rules
// To enable us to store more responses as JSON instead of string in Recording files use
// relaxed settings for roundtrip
private static readonly JsonWriterOptions WriterOptions = new JsonWriterOptions()
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
public RequestOrResponse Request { get; set; } = new RequestOrResponse();
public RequestOrResponse Response { get; set; } = new RequestOrResponse();
public string RequestUri { get; set; }
public bool IsTrack1Recording { get; set; }
public RequestMethod RequestMethod { get; set; }
public int StatusCode { get; set; }
public static RecordEntry Deserialize(JsonElement element)
{
var record = new RecordEntry();
if (element.TryGetProperty(nameof(RequestMethod), out JsonElement property))
{
record.RequestMethod = RequestMethod.Parse(property.GetString());
}
if (element.TryGetProperty(nameof(RequestUri), out property))
{
record.RequestUri = property.GetString();
}
if (element.TryGetProperty("EncodedRequestUri", out property))
{
record.IsTrack1Recording = true;
}
if (element.TryGetProperty("RequestHeaders", out property))
{
DeserializeHeaders(record.Request.Headers, property);
}
if (element.TryGetProperty("RequestBody", out property))
{
DeserializeBody(record.Request, property);
}
if (element.TryGetProperty(nameof(StatusCode), out property) &&
property.TryGetInt32(out var statusCode))
{
record.StatusCode = statusCode;
}
if (element.TryGetProperty("ResponseHeaders", out property))
{
DeserializeHeaders(record.Response.Headers, property);
}
if (element.TryGetProperty("ResponseBody", out property))
{
DeserializeBody(record.Response, property);
}
return record;
}
private static void DeserializeBody(RequestOrResponse requestOrResponse, in JsonElement property)
{
if (property.ValueKind == JsonValueKind.Null)
{
requestOrResponse.Body = null;
}
else if (IsTextContentType(requestOrResponse.Headers, out Encoding encoding))
{
if (property.ValueKind == JsonValueKind.Array)
{
{
StringBuilder sb = new StringBuilder();
foreach (JsonElement item in property.EnumerateArray())
{
sb.Append(item.GetString());
}
requestOrResponse.Body = encoding.GetBytes(sb.ToString());
}
}
else if (property.ValueKind == JsonValueKind.String)
{
requestOrResponse.Body = encoding.GetBytes(property.GetString());
}
else
{
requestOrResponse.Body = encoding.GetBytes(property.GetRawText());
}
// TODO consider versioning RecordSession so that we can stop doing the below for newly created recordings
NormalizeJsonBody(requestOrResponse);
}
else if (property.ValueKind == JsonValueKind.Array)
{
requestOrResponse.Body = Array.Empty<byte>();
}
else
{
requestOrResponse.Body = Convert.FromBase64String(property.GetString());
}
}
public static void NormalizeJsonBody(RequestOrResponse requestOrResponse)
{
if (requestOrResponse.TryGetContentType(out string contentType) && contentType.Contains("json"))
{
try
{
// in case the bytes are actually a pre-encoded JSON object, try to parse it
using var memoryStream = new MemoryStream();
using var writer = new Utf8JsonWriter(memoryStream, WriterOptions);
using var document = JsonDocument.Parse(requestOrResponse.Body);
document.RootElement.WriteTo(writer);
writer.Flush();
requestOrResponse.Body = memoryStream.ToArray();
RecordedTestSanitizer.UpdateSanitizedContentLength(requestOrResponse);
}
catch (JsonException)
{
}
}
}
private static void DeserializeHeaders(IDictionary<string, string[]> headers, in JsonElement property)
{
foreach (JsonProperty item in property.EnumerateObject())
{
if (item.Value.ValueKind == JsonValueKind.Array)
{
var values = new List<string>();
foreach (JsonElement headerValue in item.Value.EnumerateArray())
{
values.Add(headerValue.GetString());
}
headers[item.Name] = values.ToArray();
}
else
{
headers[item.Name] = new[] { item.Value.GetString() };
}
}
}
public void Serialize(Utf8JsonWriter jsonWriter)
{
jsonWriter.WriteStartObject();
jsonWriter.WriteString(nameof(RequestUri), RequestUri);
jsonWriter.WriteString(nameof(RequestMethod), RequestMethod.Method);
jsonWriter.WriteStartObject("RequestHeaders");
SerializeHeaders(jsonWriter, Request.Headers);
jsonWriter.WriteEndObject();
SerializeBody(jsonWriter, "RequestBody", Request.Body, Request.Headers);
jsonWriter.WriteNumber(nameof(StatusCode), StatusCode);
jsonWriter.WriteStartObject("ResponseHeaders");
SerializeHeaders(jsonWriter, Response.Headers);
jsonWriter.WriteEndObject();
SerializeBody(jsonWriter, "ResponseBody", Response.Body, Response.Headers);
jsonWriter.WriteEndObject();
}
private void SerializeBody(Utf8JsonWriter jsonWriter, string name, byte[] requestBody, IDictionary<string, string[]> headers)
{
if (requestBody == null)
{
jsonWriter.WriteNull(name);
}
else if (requestBody.Length == 0)
{
jsonWriter.WriteStartArray(name);
jsonWriter.WriteEndArray();
}
else if (IsTextContentType(headers, out Encoding encoding))
{
// Try parse response as JSON and write it directly if possible
try
{
using JsonDocument document = JsonDocument.Parse(requestBody);
// We use array as a wrapper for string based serialization
// so if the root is an array we can't write it directly
// fallback to generic string writing. Also, if the root is a string
// we don't want to write it directly, as this would make matching
// not work in libraries that allow passing JSON as a string.
// Finally, if the root is a JSON null, but requestBody was not null, then that means the body actually contained
// bytes for "null" and we should instead write it as a string.
if (document.RootElement.ValueKind != JsonValueKind.Array &&
document.RootElement.ValueKind != JsonValueKind.String &&
document.RootElement.ValueKind != JsonValueKind.Null)
{
using var memoryStream = new MemoryStream();
// Settings of this writer should be in sync with the one used in deserialization
using (var reformattedWriter = new Utf8JsonWriter(memoryStream, WriterOptions))
{
document.RootElement.WriteTo(reformattedWriter);
}
jsonWriter.WritePropertyName(name.AsSpan());
document.RootElement.WriteTo(jsonWriter);
return;
}
}
catch (Exception)
{
// ignore
}
ReadOnlySpan<char> text = encoding.GetString(requestBody).AsMemory().Span;
var indexOfNewline = IndexOfNewline(text);
if (indexOfNewline == -1)
{
jsonWriter.WriteString(name, text);
}
else
{
jsonWriter.WriteStartArray(name);
do
{
jsonWriter.WriteStringValue(text.Slice(0, indexOfNewline + 1));
text = text.Slice(indexOfNewline + 1);
indexOfNewline = IndexOfNewline(text);
} while (indexOfNewline != -1);
if (!text.IsEmpty)
{
jsonWriter.WriteStringValue(text);
}
jsonWriter.WriteEndArray();
}
}
else
{
jsonWriter.WriteString(name, Convert.ToBase64String(requestBody));
}
}
private int IndexOfNewline(ReadOnlySpan<char> span)
{
int indexOfNewline = span.IndexOfAny('\r', '\n');
if (indexOfNewline == -1)
{
return -1;
}
if (span.Length > indexOfNewline + 1 &&
(span[indexOfNewline + 1] == '\r' ||
span[indexOfNewline + 1] == '\n'))
{
indexOfNewline++;
}
return indexOfNewline;
}
private void SerializeHeaders(Utf8JsonWriter jsonWriter, IDictionary<string, string[]> header)
{
foreach (KeyValuePair<string, string[]> requestHeader in header)
{
if (requestHeader.Value.Length == 1)
{
jsonWriter.WriteString(requestHeader.Key, requestHeader.Value[0]);
}
else
{
jsonWriter.WriteStartArray(requestHeader.Key);
foreach (var value in requestHeader.Value)
{
jsonWriter.WriteStringValue(value);
}
jsonWriter.WriteEndArray();
}
}
}
public static bool TryGetContentType(IDictionary<string, string[]> requestHeaders, out string contentType)
{
contentType = null;
if (requestHeaders.TryGetValue("Content-Type", out var contentTypes) &&
contentTypes.Length == 1)
{
contentType = contentTypes[0];
return true;
}
return false;
}
public static bool IsTextContentType(IDictionary<string, string[]> requestHeaders, out Encoding encoding)
{
encoding = null;
return TryGetContentType(requestHeaders, out string contentType) &&
ContentTypeUtilities.TryGetTextEncoding(contentType, out encoding);
}
}
}