< Summary

Class:Azure.Core.TestFramework.MockTransport
Assembly:Azure.Core.TestFramework
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\MockTransport.cs
Covered lines:41
Uncovered lines:3
Coverable lines:44
Total lines:106
Line coverage:93.1% (41 of 44)
Covered branches:9
Total branches:12
Branch coverage:75% (9 of 12)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor()-100%100%
get_RequestGate()-100%100%
get_Requests()-100%100%
get_ExpectSyncPipeline()-100%100%
.ctor(...)-100%100%
.ctor(...)-100%100%
CreateRequest()-100%100%
Process(...)-75%50%
ProcessAsync()-75%50%
ProcessCore()-91.67%87.5%
get_SingleRequest()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\MockTransport.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.Linq;
 7using System.Threading.Tasks;
 8using Azure.Core.Pipeline;
 9
 10namespace Azure.Core.TestFramework
 11{
 12    public class MockTransport : HttpPipelineTransport
 13    {
 170014        private readonly object _syncObj = new object();
 15        private readonly Func<MockRequest, MockResponse> _responseFactory;
 16
 283617        public AsyncGate<MockRequest, MockResponse> RequestGate { get; }
 18
 363619        public List<MockRequest> Requests { get; } = new List<MockRequest>();
 20
 207021        public bool? ExpectSyncPipeline { get; set; }
 22
 1512923        public MockTransport()
 24        {
 1512925            RequestGate = new AsyncGate<MockRequest, MockResponse>();
 1512926        }
 27
 90628        public MockTransport(params MockResponse[] responses)
 29        {
 90630            var requestIndex = 0;
 90631            _responseFactory = req =>
 90632            {
 195233                lock (_syncObj)
 90634                {
 195235                    return responses[requestIndex++];
 90636                }
 194437            };
 90638        }
 39
 8440        public MockTransport(Func<MockRequest, MockResponse> responseFactory)
 41        {
 8442            _responseFactory = responseFactory;
 8443        }
 44
 45        public override Request CreateRequest()
 33413846            => new MockRequest();
 47
 48        public override void Process(HttpMessage message)
 49        {
 90750            if (ExpectSyncPipeline == false)
 51            {
 052                throw new InvalidOperationException("Sync pipeline invocation not expected");
 53            }
 54
 90755            ProcessCore(message).GetAwaiter().GetResult();
 86956        }
 57
 58        public override async ValueTask ProcessAsync(HttpMessage message)
 59        {
 117360            if (ExpectSyncPipeline == true)
 61            {
 062                throw new InvalidOperationException("Async pipeline invocation not expected");
 63            }
 64
 117365            await ProcessCore(message);
 113166        }
 67
 68        private async Task ProcessCore(HttpMessage message)
 69        {
 208070            if (!(message.Request is MockRequest request))
 071                throw new InvalidOperationException("the request is not compatible with the transport");
 72
 208073            lock (_syncObj)
 74            {
 208075                Requests.Add(request);
 208076            }
 77
 208078            if (RequestGate != null)
 79            {
 37880                message.Response = await RequestGate.WaitForRelease(request);
 81            }
 82            else
 83            {
 170284                message.Response = _responseFactory(request);
 85            }
 86
 200087            message.Response.ClientRequestId = request.ClientRequestId;
 88
 200089            if (message.Response.ContentStream != null && ExpectSyncPipeline != null)
 90            {
 14091                message.Response.ContentStream = new AsyncValidatingStream(!ExpectSyncPipeline.Value, message.Response.C
 92            }
 200093        }
 94
 95        public MockRequest SingleRequest
 96        {
 97            get
 98            {
 14099                lock (_syncObj)
 100                {
 140101                    return Requests.Single();
 102                }
 140103            }
 104        }
 105    }
 106}