< Summary

Class:Azure.Core.TestFramework.RecordTransport
Assembly:Azure.Core.TestFramework
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\RecordTransport.cs
Covered lines:40
Uncovered lines:20
Coverable lines:60
Total lines:146
Line coverage:66.6% (40 of 60)
Covered branches:14
Total branches:22
Branch coverage:63.6% (14 of 22)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
Process(...)-100%100%
ProcessAsync()-0%0%
Record(...)-71.43%50%
CreateRequest()-0%100%
CreateEntry(...)-80.95%75%
ReadToEnd(...)-33.33%50%
ReadToEnd(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\RecordTransport.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.Diagnostics;
 7using System.Globalization;
 8using System.IO;
 9using System.Linq;
 10using System.Threading;
 11using System.Threading.Tasks;
 12using Azure.Core.Pipeline;
 13
 14namespace 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
 426        public RecordTransport(RecordSession session, HttpPipelineTransport innerTransport, Func<RecordEntry, EntryRecor
 27        {
 428            _innerTransport = innerTransport;
 429            _filter = filter;
 430            _random = random;
 431            _session = session;
 432        }
 33
 34        public override void Process(HttpMessage message)
 35        {
 436            _innerTransport.Process(message);
 437            Record(message);
 438        }
 39
 40        public override async ValueTask ProcessAsync(HttpMessage message)
 41        {
 042            await _innerTransport.ProcessAsync(message);
 043            Record(message);
 044        }
 45
 46        private void Record(HttpMessage message)
 47        {
 448            RecordEntry recordEntry = CreateEntry(message.Request, message.Response);
 49
 450            switch (_filter(recordEntry))
 51            {
 52                case EntryRecordModel.Record:
 053                    _session.Record(recordEntry);
 054                    break;
 55                case EntryRecordModel.RecordWithoutRequestBody:
 456                    recordEntry.Request.Body = null;
 457                    _session.Record(recordEntry);
 58                    break;
 59                default:
 60                    break;
 61            }
 462        }
 63
 64        public override Request CreateRequest()
 65        {
 066            Request request = _innerTransport.CreateRequest();
 67
 068            lock (_random)
 69            {
 70                // Override ClientRequestId to avoid excessive diffs
 071                request.ClientRequestId = _random.NewGuid().ToString("N");
 072            }
 73
 074            return request;
 75        }
 76
 77        public static RecordEntry CreateEntry(Request request, Response response)
 78        {
 32639279            var entry = new RecordEntry
 32639280            {
 32639281                RequestUri = request.Uri.ToString(),
 32639282                RequestMethod = request.Method,
 32639283                Request =
 32639284                {
 32639285                    Body = ReadToEnd(request.Content),
 32639286                },
 32639287            };
 88
 375228489            foreach (HttpHeader requestHeader in request.Headers)
 90            {
 154975091                var gotHeader = request.Headers.TryGetValues(requestHeader.Name, out IEnumerable<string> headerValues);
 92                Debug.Assert(gotHeader);
 154975093                entry.Request.Headers.Add(requestHeader.Name, headerValues.ToArray());
 94            }
 95
 96            // Make sure we record Content-Length even if it's not set explicitly
 32639297            if (!request.Headers.TryGetValue("Content-Length", out _) && request.Content != null && request.Content.TryC
 98            {
 099                entry.Request.Headers.Add("Content-Length", new[] { computedLength.ToString(CultureInfo.InvariantCulture
 100            }
 101
 326392102            if (response != null)
 103            {
 10104                entry.Response.Body = ReadToEnd(response);
 10105                entry.StatusCode = response.Status;
 0106                foreach (HttpHeader responseHeader in response.Headers)
 107                {
 0108                    var gotHeader = response.Headers.TryGetValues(responseHeader.Name, out IEnumerable<string> headerVal
 109                    Debug.Assert(gotHeader);
 0110                    entry.Response.Headers.Add(responseHeader.Name, headerValues.ToArray());
 111                }
 112            }
 113
 326392114            return entry;
 115        }
 116
 117        private static byte[] ReadToEnd(Response response)
 118        {
 10119            Stream responseContentStream = response.ContentStream;
 10120            if (responseContentStream == null)
 121            {
 10122                return null;
 123            }
 124
 0125            var memoryStream = new NonSeekableMemoryStream();
 0126            responseContentStream.CopyTo(memoryStream);
 0127            responseContentStream.Dispose();
 128
 0129            memoryStream.Reset();
 0130            response.ContentStream = memoryStream;
 0131            return memoryStream.ToArray();
 132        }
 133
 134        private static byte[] ReadToEnd(RequestContent requestContent)
 135        {
 326392136            if (requestContent == null)
 137            {
 307128138                return null;
 139            }
 140
 19264141            using var memoryStream = new MemoryStream();
 19264142            requestContent.WriteTo(memoryStream, CancellationToken.None);
 19264143            return memoryStream.ToArray();
 19264144        }
 145    }
 146}