< Summary

Class:Azure.Core.TestFramework.PlaybackTransport
Assembly:Azure.Core.TestFramework
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\PlaybackTransport.cs
Covered lines:42
Uncovered lines:0
Coverable lines:42
Total lines:121
Line coverage:100% (42 of 42)
Covered branches:22
Total branches:22
Branch coverage:100% (22 of 22)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
Process(...)-100%100%
ProcessAsync()-100%100%
CreateRequest()-100%100%
GetResponse(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\PlaybackTransport.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.IO;
 7using System.Threading.Tasks;
 8
 9namespace 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
 1494123        public PlaybackTransport(RecordSession session, RecordMatcher matcher, RecordedTestSanitizer sanitizer, Random r
 24        {
 1494125            _session = session;
 1494126            _matcher = matcher;
 1494127            _random = random;
 1494128            _sanitizer = sanitizer;
 1494129            _skipRequestBodyFilter = skipRequestBodyFilter;
 1494130        }
 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
 3746037            if (message.Request.Content != null &&
 3746038                message.Request.Content.TryComputeLength(out long length) &&
 3746039                length > 0)
 40            {
 828141                using (MemoryStream stream = new MemoryStream((int)length))
 42                {
 828143                    message.Request.Content.WriteTo(stream, message.CancellationToken);
 827244                }
 45            }
 46
 3745147            var requestEntry = RecordTransport.CreateEntry(message.Request, null);
 48
 3745149            if (_skipRequestBodyFilter(requestEntry))
 50            {
 4851                requestEntry.Request.Body = null;
 52            }
 53
 3745154            message.Response = GetResponse(_session.Lookup(requestEntry, _matcher, _sanitizer));
 55
 56            // Copy the ClientRequestId like the HTTP transport.
 3743757            message.Response.ClientRequestId = message.Request.ClientRequestId;
 3743758        }
 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
 28893465            if (message.Request.Content != null &&
 28893466                message.Request.Content.TryComputeLength(out long length) &&
 28893467                length > 0)
 68            {
 1098169                using (MemoryStream stream = new MemoryStream((int)length))
 70                {
 1098171                    await message.Request.Content.WriteToAsync(stream, message.CancellationToken).ConfigureAwait(false);
 1097272                }
 73            }
 74
 28892575            var requestEntry = RecordTransport.CreateEntry(message.Request, null);
 76
 28892577            if (_skipRequestBodyFilter(requestEntry))
 78            {
 5079                requestEntry.Request.Body = null;
 80            }
 81
 28892582            message.Response = GetResponse(_session.Lookup(requestEntry, _matcher, _sanitizer));
 83
 84            // Copy the ClientRequestId like the HTTP transport.
 28888985            message.Response.ClientRequestId = message.Request.ClientRequestId;
 28888986        }
 87
 88        public override Request CreateRequest()
 89        {
 33177890            lock (_random)
 91            {
 92                // Force a call to random.NewGuid so we keep the random seed
 93                // unified between record/playback
 33177894                _random.NewGuid();
 95
 96                // TODO: Pavel will think about ways to unify this
 33177897            }
 33177898            return base.CreateRequest();
 99        }
 100
 101        public Response GetResponse(RecordEntry recordEntry)
 102        {
 326326103            var response = new MockResponse(recordEntry.StatusCode);
 104            // TODO: Use non-seekable stream
 326326105            if (recordEntry.Response.Body != null)
 106            {
 326124107                response.ContentStream = new MemoryStream(recordEntry.Response.Body);
 108            }
 109
 9864516110            foreach (KeyValuePair<string, string[]> responseHeader in recordEntry.Response.Headers)
 111            {
 18970796112                foreach (string value in responseHeader.Value)
 113                {
 4879464114                    response.AddHeader(new HttpHeader(responseHeader.Key, value));
 115                }
 116            }
 117
 326326118            return response;
 119        }
 120    }
 121}