| | 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.Diagnostics; |
| | 7 | | using System.Globalization; |
| | 8 | | using System.IO; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Threading; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | | using Azure.Core.Pipeline; |
| | 13 | |
|
| | 14 | | namespace Azure.Core.TestFramework |
| | 15 | | { |
| | 16 | | public class RecordTransport : HttpPipelineTransport |
| | 17 | | { |
| | 18 | | private readonly HttpPipelineTransport _innerTransport; |
| | 19 | |
|
| | 20 | | private readonly Func<RecordEntry, EntryRecordModel> _filter; |
| | 21 | |
|
| | 22 | | private readonly Random _random; |
| | 23 | |
|
| | 24 | | private readonly RecordSession _session; |
| | 25 | |
|
| 4 | 26 | | public RecordTransport(RecordSession session, HttpPipelineTransport innerTransport, Func<RecordEntry, EntryRecor |
| | 27 | | { |
| 4 | 28 | | _innerTransport = innerTransport; |
| 4 | 29 | | _filter = filter; |
| 4 | 30 | | _random = random; |
| 4 | 31 | | _session = session; |
| 4 | 32 | | } |
| | 33 | |
|
| | 34 | | public override void Process(HttpMessage message) |
| | 35 | | { |
| 4 | 36 | | _innerTransport.Process(message); |
| 4 | 37 | | Record(message); |
| 4 | 38 | | } |
| | 39 | |
|
| | 40 | | public override async ValueTask ProcessAsync(HttpMessage message) |
| | 41 | | { |
| 0 | 42 | | await _innerTransport.ProcessAsync(message); |
| 0 | 43 | | Record(message); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | private void Record(HttpMessage message) |
| | 47 | | { |
| 4 | 48 | | RecordEntry recordEntry = CreateEntry(message.Request, message.Response); |
| | 49 | |
|
| 4 | 50 | | switch (_filter(recordEntry)) |
| | 51 | | { |
| | 52 | | case EntryRecordModel.Record: |
| 0 | 53 | | _session.Record(recordEntry); |
| 0 | 54 | | break; |
| | 55 | | case EntryRecordModel.RecordWithoutRequestBody: |
| 4 | 56 | | recordEntry.Request.Body = null; |
| 4 | 57 | | _session.Record(recordEntry); |
| | 58 | | break; |
| | 59 | | default: |
| | 60 | | break; |
| | 61 | | } |
| 4 | 62 | | } |
| | 63 | |
|
| | 64 | | public override Request CreateRequest() |
| | 65 | | { |
| 0 | 66 | | Request request = _innerTransport.CreateRequest(); |
| | 67 | |
|
| 0 | 68 | | lock (_random) |
| | 69 | | { |
| | 70 | | // Override ClientRequestId to avoid excessive diffs |
| 0 | 71 | | request.ClientRequestId = _random.NewGuid().ToString("N"); |
| 0 | 72 | | } |
| | 73 | |
|
| 0 | 74 | | return request; |
| | 75 | | } |
| | 76 | |
|
| | 77 | | public static RecordEntry CreateEntry(Request request, Response response) |
| | 78 | | { |
| 326392 | 79 | | var entry = new RecordEntry |
| 326392 | 80 | | { |
| 326392 | 81 | | RequestUri = request.Uri.ToString(), |
| 326392 | 82 | | RequestMethod = request.Method, |
| 326392 | 83 | | Request = |
| 326392 | 84 | | { |
| 326392 | 85 | | Body = ReadToEnd(request.Content), |
| 326392 | 86 | | }, |
| 326392 | 87 | | }; |
| | 88 | |
|
| 3752284 | 89 | | foreach (HttpHeader requestHeader in request.Headers) |
| | 90 | | { |
| 1549750 | 91 | | var gotHeader = request.Headers.TryGetValues(requestHeader.Name, out IEnumerable<string> headerValues); |
| | 92 | | Debug.Assert(gotHeader); |
| 1549750 | 93 | | entry.Request.Headers.Add(requestHeader.Name, headerValues.ToArray()); |
| | 94 | | } |
| | 95 | |
|
| | 96 | | // Make sure we record Content-Length even if it's not set explicitly |
| 326392 | 97 | | if (!request.Headers.TryGetValue("Content-Length", out _) && request.Content != null && request.Content.TryC |
| | 98 | | { |
| 0 | 99 | | entry.Request.Headers.Add("Content-Length", new[] { computedLength.ToString(CultureInfo.InvariantCulture |
| | 100 | | } |
| | 101 | |
|
| 326392 | 102 | | if (response != null) |
| | 103 | | { |
| 10 | 104 | | entry.Response.Body = ReadToEnd(response); |
| 10 | 105 | | entry.StatusCode = response.Status; |
| 0 | 106 | | foreach (HttpHeader responseHeader in response.Headers) |
| | 107 | | { |
| 0 | 108 | | var gotHeader = response.Headers.TryGetValues(responseHeader.Name, out IEnumerable<string> headerVal |
| | 109 | | Debug.Assert(gotHeader); |
| 0 | 110 | | entry.Response.Headers.Add(responseHeader.Name, headerValues.ToArray()); |
| | 111 | | } |
| | 112 | | } |
| | 113 | |
|
| 326392 | 114 | | return entry; |
| | 115 | | } |
| | 116 | |
|
| | 117 | | private static byte[] ReadToEnd(Response response) |
| | 118 | | { |
| 10 | 119 | | Stream responseContentStream = response.ContentStream; |
| 10 | 120 | | if (responseContentStream == null) |
| | 121 | | { |
| 10 | 122 | | return null; |
| | 123 | | } |
| | 124 | |
|
| 0 | 125 | | var memoryStream = new NonSeekableMemoryStream(); |
| 0 | 126 | | responseContentStream.CopyTo(memoryStream); |
| 0 | 127 | | responseContentStream.Dispose(); |
| | 128 | |
|
| 0 | 129 | | memoryStream.Reset(); |
| 0 | 130 | | response.ContentStream = memoryStream; |
| 0 | 131 | | return memoryStream.ToArray(); |
| | 132 | | } |
| | 133 | |
|
| | 134 | | private static byte[] ReadToEnd(RequestContent requestContent) |
| | 135 | | { |
| 326392 | 136 | | if (requestContent == null) |
| | 137 | | { |
| 307128 | 138 | | return null; |
| | 139 | | } |
| | 140 | |
|
| 19264 | 141 | | using var memoryStream = new MemoryStream(); |
| 19264 | 142 | | requestContent.WriteTo(memoryStream, CancellationToken.None); |
| 19264 | 143 | | return memoryStream.ToArray(); |
| 19264 | 144 | | } |
| | 145 | | } |
| | 146 | | } |