| | 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.Threading.Tasks; |
| | 8 | |
|
| | 9 | | namespace Azure.Core.TestFramework |
| | 10 | | { |
| | 11 | | public class PlaybackTransport : MockTransport |
| | 12 | | { |
| | 13 | | private readonly RecordSession _session; |
| | 14 | |
|
| | 15 | | private readonly RecordMatcher _matcher; |
| | 16 | |
|
| | 17 | | private readonly RecordedTestSanitizer _sanitizer; |
| | 18 | |
|
| | 19 | | private readonly Random _random; |
| | 20 | |
|
| | 21 | | private readonly Func<RecordEntry, bool> _skipRequestBodyFilter; |
| | 22 | |
|
| 14941 | 23 | | public PlaybackTransport(RecordSession session, RecordMatcher matcher, RecordedTestSanitizer sanitizer, Random r |
| | 24 | | { |
| 14941 | 25 | | _session = session; |
| 14941 | 26 | | _matcher = matcher; |
| 14941 | 27 | | _random = random; |
| 14941 | 28 | | _sanitizer = sanitizer; |
| 14941 | 29 | | _skipRequestBodyFilter = skipRequestBodyFilter; |
| 14941 | 30 | | } |
| | 31 | |
|
| | 32 | | public override void Process(HttpMessage message) |
| | 33 | | { |
| | 34 | | // Some tests will check if the Request Content is being read (to |
| | 35 | | // verify their Progress handling) so we'll just copy it to a |
| | 36 | | // MemoryStream |
| 37460 | 37 | | if (message.Request.Content != null && |
| 37460 | 38 | | message.Request.Content.TryComputeLength(out long length) && |
| 37460 | 39 | | length > 0) |
| | 40 | | { |
| 8281 | 41 | | using (MemoryStream stream = new MemoryStream((int)length)) |
| | 42 | | { |
| 8281 | 43 | | message.Request.Content.WriteTo(stream, message.CancellationToken); |
| 8272 | 44 | | } |
| | 45 | | } |
| | 46 | |
|
| 37451 | 47 | | var requestEntry = RecordTransport.CreateEntry(message.Request, null); |
| | 48 | |
|
| 37451 | 49 | | if (_skipRequestBodyFilter(requestEntry)) |
| | 50 | | { |
| 48 | 51 | | requestEntry.Request.Body = null; |
| | 52 | | } |
| | 53 | |
|
| 37451 | 54 | | message.Response = GetResponse(_session.Lookup(requestEntry, _matcher, _sanitizer)); |
| | 55 | |
|
| | 56 | | // Copy the ClientRequestId like the HTTP transport. |
| 37437 | 57 | | message.Response.ClientRequestId = message.Request.ClientRequestId; |
| 37437 | 58 | | } |
| | 59 | |
|
| | 60 | | public override async ValueTask ProcessAsync(HttpMessage message) |
| | 61 | | { |
| | 62 | | // Some tests will check if the Request Content is being read (to |
| | 63 | | // verify their Progress handling) so we'll just copy it to a |
| | 64 | | // MemoryStream asynchronously |
| 288934 | 65 | | if (message.Request.Content != null && |
| 288934 | 66 | | message.Request.Content.TryComputeLength(out long length) && |
| 288934 | 67 | | length > 0) |
| | 68 | | { |
| 10981 | 69 | | using (MemoryStream stream = new MemoryStream((int)length)) |
| | 70 | | { |
| 10981 | 71 | | await message.Request.Content.WriteToAsync(stream, message.CancellationToken).ConfigureAwait(false); |
| 10972 | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| 288925 | 75 | | var requestEntry = RecordTransport.CreateEntry(message.Request, null); |
| | 76 | |
|
| 288925 | 77 | | if (_skipRequestBodyFilter(requestEntry)) |
| | 78 | | { |
| 50 | 79 | | requestEntry.Request.Body = null; |
| | 80 | | } |
| | 81 | |
|
| 288925 | 82 | | message.Response = GetResponse(_session.Lookup(requestEntry, _matcher, _sanitizer)); |
| | 83 | |
|
| | 84 | | // Copy the ClientRequestId like the HTTP transport. |
| 288889 | 85 | | message.Response.ClientRequestId = message.Request.ClientRequestId; |
| 288889 | 86 | | } |
| | 87 | |
|
| | 88 | | public override Request CreateRequest() |
| | 89 | | { |
| 331778 | 90 | | lock (_random) |
| | 91 | | { |
| | 92 | | // Force a call to random.NewGuid so we keep the random seed |
| | 93 | | // unified between record/playback |
| 331778 | 94 | | _random.NewGuid(); |
| | 95 | |
|
| | 96 | | // TODO: Pavel will think about ways to unify this |
| 331778 | 97 | | } |
| 331778 | 98 | | return base.CreateRequest(); |
| | 99 | | } |
| | 100 | |
|
| | 101 | | public Response GetResponse(RecordEntry recordEntry) |
| | 102 | | { |
| 326326 | 103 | | var response = new MockResponse(recordEntry.StatusCode); |
| | 104 | | // TODO: Use non-seekable stream |
| 326326 | 105 | | if (recordEntry.Response.Body != null) |
| | 106 | | { |
| 326124 | 107 | | response.ContentStream = new MemoryStream(recordEntry.Response.Body); |
| | 108 | | } |
| | 109 | |
|
| 9864516 | 110 | | foreach (KeyValuePair<string, string[]> responseHeader in recordEntry.Response.Headers) |
| | 111 | | { |
| 18970796 | 112 | | foreach (string value in responseHeader.Value) |
| | 113 | | { |
| 4879464 | 114 | | response.AddHeader(new HttpHeader(responseHeader.Key, value)); |
| | 115 | | } |
| | 116 | | } |
| | 117 | |
|
| 326326 | 118 | | return response; |
| | 119 | | } |
| | 120 | | } |
| | 121 | | } |