| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.IO; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Text; |
| | 9 | | using System.Text.Encodings.Web; |
| | 10 | | using System.Text.Json; |
| | 11 | | using Azure.Core.Pipeline; |
| | 12 | |
|
| | 13 | | namespace Azure.Core.TestFramework |
| | 14 | | { |
| | 15 | | public class RecordEntry |
| | 16 | | { |
| 66 | 17 | | private static readonly JsonWriterOptions RequestWriterOptions = new JsonWriterOptions(); |
| | 18 | | // Responses are usually formatted using Newtonsoft.Json that has more relaxed encoding rules |
| | 19 | | // To enable us to store more responses as JSON instead of string in Recording files use |
| | 20 | | // relaxed settings for roundrip |
| 66 | 21 | | private static readonly JsonWriterOptions ResponseWriterOptions = new JsonWriterOptions() |
| 66 | 22 | | { |
| 66 | 23 | | Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping |
| 66 | 24 | | }; |
| | 25 | |
|
| 5566502 | 26 | | public RecordEntryMessage Request { get; } = new RecordEntryMessage(); |
| | 27 | |
|
| 3353130 | 28 | | public RecordEntryMessage Response { get; } = new RecordEntryMessage(); |
| | 29 | |
|
| 1986068 | 30 | | public string RequestUri { get; set; } |
| | 31 | |
|
| 0 | 32 | | public bool IsTrack1Recording { get; set; } |
| | 33 | |
|
| 1333122 | 34 | | public RequestMethod RequestMethod { get; set; } |
| | 35 | |
|
| 675108 | 36 | | public int StatusCode { get; set; } |
| | 37 | |
|
| | 38 | | public static RecordEntry Deserialize(JsonElement element) |
| | 39 | | { |
| 348692 | 40 | | var record = new RecordEntry(); |
| | 41 | |
|
| 348692 | 42 | | if (element.TryGetProperty(nameof(RequestMethod), out JsonElement property)) |
| | 43 | | { |
| 348692 | 44 | | record.RequestMethod = RequestMethod.Parse(property.GetString()); |
| | 45 | | } |
| | 46 | |
|
| 348692 | 47 | | if (element.TryGetProperty(nameof(RequestUri), out property)) |
| | 48 | | { |
| 348692 | 49 | | record.RequestUri = property.GetString(); |
| | 50 | | } |
| | 51 | |
|
| 348692 | 52 | | if (element.TryGetProperty("EncodedRequestUri", out property)) |
| | 53 | | { |
| 0 | 54 | | record.IsTrack1Recording = true; |
| | 55 | | } |
| | 56 | |
|
| 348692 | 57 | | if (element.TryGetProperty("RequestHeaders", out property)) |
| | 58 | | { |
| 348692 | 59 | | DeserializeHeaders(record.Request.Headers, property); |
| | 60 | | } |
| | 61 | |
|
| 348692 | 62 | | if (element.TryGetProperty("RequestBody", out property)) |
| | 63 | | { |
| 348692 | 64 | | record.Request.Body = DeserializeBody(record.Request.Headers, property, RequestWriterOptions); |
| | 65 | | } |
| | 66 | |
|
| 348692 | 67 | | if (element.TryGetProperty(nameof(StatusCode), out property) && |
| 348692 | 68 | | property.TryGetInt32(out var statusCode)) |
| | 69 | | { |
| 348692 | 70 | | record.StatusCode = statusCode; |
| | 71 | | } |
| | 72 | |
|
| 348692 | 73 | | if (element.TryGetProperty("ResponseHeaders", out property)) |
| | 74 | | { |
| 348692 | 75 | | DeserializeHeaders(record.Response.Headers, property); |
| | 76 | | } |
| | 77 | |
|
| 348692 | 78 | | if (element.TryGetProperty("ResponseBody", out property)) |
| | 79 | | { |
| 348692 | 80 | | record.Response.Body = DeserializeBody(record.Response.Headers, property, ResponseWriterOptions); |
| | 81 | | } |
| | 82 | |
|
| 348692 | 83 | | return record; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | private static byte[] DeserializeBody(IDictionary<string, string[]> headers, in JsonElement property, JsonWriter |
| | 87 | | { |
| 697384 | 88 | | if (property.ValueKind == JsonValueKind.Null) |
| | 89 | | { |
| 329598 | 90 | | return null; |
| | 91 | | } |
| | 92 | |
|
| 367786 | 93 | | if (IsTextContentType(headers, out Encoding encoding)) |
| | 94 | | { |
| 300541 | 95 | | if (property.ValueKind == JsonValueKind.Object) |
| | 96 | | { |
| 49042 | 97 | | using var memoryStream = new MemoryStream(); |
| 49042 | 98 | | using var writer = new Utf8JsonWriter(memoryStream, writerOptions); |
| 49042 | 99 | | property.WriteTo(writer); |
| 49042 | 100 | | writer.Flush(); |
| 49042 | 101 | | return memoryStream.ToArray(); |
| | 102 | | } |
| 251499 | 103 | | else if (property.ValueKind == JsonValueKind.Array) |
| | 104 | | { |
| 241266 | 105 | | StringBuilder stringBuilder = new StringBuilder(); |
| | 106 | |
|
| 3563652 | 107 | | foreach (JsonElement item in property.EnumerateArray()) |
| | 108 | | { |
| 1547302 | 109 | | stringBuilder.Append(item.GetString()); |
| | 110 | | } |
| | 111 | |
|
| 241266 | 112 | | return encoding.GetBytes(stringBuilder.ToString()); |
| | 113 | | } |
| | 114 | | else |
| | 115 | | { |
| 10233 | 116 | | return encoding.GetBytes(property.GetString()); |
| | 117 | | } |
| | 118 | | } |
| | 119 | |
|
| 67245 | 120 | | if (property.ValueKind == JsonValueKind.Array) |
| | 121 | | { |
| 58661 | 122 | | return Array.Empty<byte>(); |
| | 123 | | } |
| | 124 | |
|
| 8584 | 125 | | return Convert.FromBase64String(property.GetString()); |
| 49042 | 126 | | } |
| | 127 | |
|
| | 128 | | private static void DeserializeHeaders(IDictionary<string, string[]> headers, in JsonElement property) |
| | 129 | | { |
| 14475280 | 130 | | foreach (JsonProperty item in property.EnumerateObject()) |
| | 131 | | { |
| 6540256 | 132 | | if (item.Value.ValueKind == JsonValueKind.Array) |
| | 133 | | { |
| 621338 | 134 | | var values = new List<string>(); |
| 3730188 | 135 | | foreach (JsonElement headerValue in item.Value.EnumerateArray()) |
| | 136 | | { |
| 1243756 | 137 | | values.Add(headerValue.GetString()); |
| | 138 | | } |
| | 139 | |
|
| 621338 | 140 | | headers[item.Name] = values.ToArray(); |
| | 141 | | } |
| | 142 | | else |
| | 143 | | { |
| 5918918 | 144 | | headers[item.Name] = new[] { item.Value.GetString() }; |
| | 145 | | } |
| | 146 | | } |
| 697384 | 147 | | } |
| | 148 | |
|
| | 149 | | public void Serialize(Utf8JsonWriter jsonWriter) |
| | 150 | | { |
| 28 | 151 | | jsonWriter.WriteStartObject(); |
| | 152 | |
|
| 28 | 153 | | jsonWriter.WriteString(nameof(RequestUri), RequestUri); |
| 28 | 154 | | jsonWriter.WriteString(nameof(RequestMethod), RequestMethod.Method); |
| 28 | 155 | | jsonWriter.WriteStartObject("RequestHeaders"); |
| 28 | 156 | | SerializeHeaders(jsonWriter, Request.Headers); |
| 28 | 157 | | jsonWriter.WriteEndObject(); |
| | 158 | |
|
| 28 | 159 | | SerializeBody(jsonWriter, "RequestBody", Request.Body, Request.Headers, RequestWriterOptions); |
| | 160 | |
|
| 28 | 161 | | jsonWriter.WriteNumber(nameof(StatusCode), StatusCode); |
| | 162 | |
|
| 28 | 163 | | jsonWriter.WriteStartObject("ResponseHeaders"); |
| 28 | 164 | | SerializeHeaders(jsonWriter, Response.Headers); |
| 28 | 165 | | jsonWriter.WriteEndObject(); |
| | 166 | |
|
| 28 | 167 | | SerializeBody(jsonWriter, "ResponseBody", Response.Body, Response.Headers, ResponseWriterOptions); |
| 28 | 168 | | jsonWriter.WriteEndObject(); |
| 28 | 169 | | } |
| | 170 | |
|
| | 171 | | private void SerializeBody(Utf8JsonWriter jsonWriter, string name, byte[] requestBody, IDictionary<string, strin |
| | 172 | | { |
| 56 | 173 | | if (requestBody == null) |
| | 174 | | { |
| 4 | 175 | | jsonWriter.WriteNull(name); |
| | 176 | | } |
| 52 | 177 | | else if (requestBody.Length == 0) |
| | 178 | | { |
| 4 | 179 | | jsonWriter.WriteStartArray(name); |
| 4 | 180 | | jsonWriter.WriteEndArray(); |
| | 181 | | } |
| 48 | 182 | | else if (IsTextContentType(headers, out Encoding encoding)) |
| | 183 | | { |
| | 184 | | // Try parse response as JSON and write it directly if possible |
| | 185 | | try |
| | 186 | | { |
| 44 | 187 | | using JsonDocument document = JsonDocument.Parse(requestBody); |
| | 188 | |
|
| | 189 | | // We use array as a wrapper for string based serialization |
| | 190 | | // so if the root is an array we can't write it directly |
| | 191 | | // fallback to generic string writing |
| 28 | 192 | | if (document.RootElement.ValueKind != JsonValueKind.Array) |
| | 193 | | { |
| | 194 | | // Make sure we can replay JSON is exactly the same as the source |
| | 195 | | // for the case where service response was pre-formatted |
| | 196 | | // fallback to generic string writing |
| 20 | 197 | | using var memoryStream = new MemoryStream(); |
| | 198 | | // Settings of this writer should be in sync with the one used in deserialiation |
| 20 | 199 | | using (var reformattedWriter = new Utf8JsonWriter(memoryStream, writerOptions)) |
| | 200 | | { |
| 20 | 201 | | document.RootElement.WriteTo(reformattedWriter); |
| 20 | 202 | | } |
| | 203 | |
|
| 20 | 204 | | if (memoryStream.ToArray().SequenceEqual(requestBody)) |
| | 205 | | { |
| 10 | 206 | | jsonWriter.WritePropertyName(name.AsSpan()); |
| 10 | 207 | | document.RootElement.WriteTo(jsonWriter); |
| 10 | 208 | | return; |
| | 209 | | } |
| | 210 | | } |
| 18 | 211 | | } |
| 16 | 212 | | catch (Exception) |
| | 213 | | { |
| | 214 | | // ignore |
| 16 | 215 | | } |
| | 216 | |
|
| 34 | 217 | | ReadOnlySpan<char> text = encoding.GetString(requestBody).AsMemory().Span; |
| | 218 | |
|
| 34 | 219 | | var indexOfNewline = IndexOfNewline(text); |
| 34 | 220 | | if (indexOfNewline == -1) |
| | 221 | | { |
| 18 | 222 | | jsonWriter.WriteString(name, text); |
| | 223 | | } |
| | 224 | | else |
| | 225 | | { |
| 16 | 226 | | jsonWriter.WriteStartArray(name); |
| | 227 | | do |
| | 228 | | { |
| 20 | 229 | | jsonWriter.WriteStringValue(text.Slice(0, indexOfNewline + 1)); |
| 20 | 230 | | text = text.Slice(indexOfNewline + 1); |
| 20 | 231 | | indexOfNewline = IndexOfNewline(text); |
| 20 | 232 | | } while (indexOfNewline != -1); |
| | 233 | |
|
| 16 | 234 | | if (!text.IsEmpty) |
| | 235 | | { |
| 12 | 236 | | jsonWriter.WriteStringValue(text); |
| | 237 | | } |
| | 238 | |
|
| 16 | 239 | | jsonWriter.WriteEndArray(); |
| | 240 | | } |
| | 241 | | } |
| | 242 | | else |
| | 243 | | { |
| 4 | 244 | | jsonWriter.WriteString(name, Convert.ToBase64String(requestBody)); |
| | 245 | | } |
| 14 | 246 | | } |
| | 247 | |
|
| | 248 | | private int IndexOfNewline(ReadOnlySpan<char> span) |
| | 249 | | { |
| 54 | 250 | | int indexOfNewline = span.IndexOfAny('\r', '\n'); |
| | 251 | |
|
| 54 | 252 | | if (indexOfNewline == -1) |
| | 253 | | { |
| 34 | 254 | | return -1; |
| | 255 | | } |
| | 256 | |
|
| 20 | 257 | | if (span.Length > indexOfNewline + 1 && |
| 20 | 258 | | (span[indexOfNewline + 1] == '\r' || |
| 20 | 259 | | span[indexOfNewline + 1] == '\n')) |
| | 260 | | { |
| 8 | 261 | | indexOfNewline++; |
| | 262 | | } |
| | 263 | |
|
| 20 | 264 | | return indexOfNewline; |
| | 265 | | } |
| | 266 | |
|
| | 267 | | private void SerializeHeaders(Utf8JsonWriter jsonWriter, IDictionary<string, string[]> header) |
| | 268 | | { |
| 328 | 269 | | foreach (KeyValuePair<string, string[]> requestHeader in header) |
| | 270 | | { |
| 108 | 271 | | if (requestHeader.Value.Length == 1) |
| | 272 | | { |
| 56 | 273 | | jsonWriter.WriteString(requestHeader.Key, requestHeader.Value[0]); |
| | 274 | | } |
| | 275 | | else |
| | 276 | | { |
| 52 | 277 | | jsonWriter.WriteStartArray(requestHeader.Key); |
| 312 | 278 | | foreach (var value in requestHeader.Value) |
| | 279 | | { |
| 104 | 280 | | jsonWriter.WriteStringValue(value); |
| | 281 | | } |
| | 282 | |
|
| 52 | 283 | | jsonWriter.WriteEndArray(); |
| | 284 | | } |
| | 285 | | } |
| 56 | 286 | | } |
| | 287 | |
|
| | 288 | | public static bool TryGetContentType(IDictionary<string, string[]> requestHeaders, out string contentType) |
| | 289 | | { |
| 367834 | 290 | | contentType = null; |
| 367834 | 291 | | if (requestHeaders.TryGetValue("Content-Type", out var contentTypes) && |
| 367834 | 292 | | contentTypes.Length == 1) |
| | 293 | | { |
| 303697 | 294 | | contentType = contentTypes[0]; |
| 303697 | 295 | | return true; |
| | 296 | | } |
| 64137 | 297 | | return false; |
| | 298 | | } |
| | 299 | |
|
| | 300 | | public static bool IsTextContentType(IDictionary<string, string[]> requestHeaders, out Encoding encoding) |
| | 301 | | { |
| 367834 | 302 | | encoding = null; |
| 367834 | 303 | | return TryGetContentType(requestHeaders, out string contentType) && |
| 367834 | 304 | | ContentTypeUtilities.TryGetTextEncoding(contentType, out encoding); |
| | 305 | | } |
| | 306 | | } |
| | 307 | | } |