< Summary

Class:Azure.Core.HttpMessage
Assembly:Azure.Core
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\HttpMessage.cs
Covered lines:32
Uncovered lines:11
Coverable lines:43
Total lines:179
Line coverage:74.4% (32 of 43)
Covered branches:13
Total branches:16
Branch coverage:81.2% (13 of 16)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
get_Request()-100%100%
get_Response()-66.67%50%
set_Response(...)-100%100%
get_HasResponse()-100%100%
get_CancellationToken()-100%100%
get_ResponseClassifier()-100%100%
get_BufferResponse()-100%100%
TryGetProperty(...)-100%100%
SetProperty(...)-100%100%
ExtractResponseContent()-100%83.33%
Dispose()-100%75%
get_Original()-100%100%
.ctor(...)-100%100%
CreateException()-100%100%
Flush()-0%100%
Read(...)-100%100%
Seek(...)-0%100%
SetLength(...)-0%100%
Write(...)-0%100%
get_CanRead()-0%100%
get_CanSeek()-0%100%
get_CanWrite()-0%100%
get_Length()-0%100%
get_Position()-0%100%
set_Position(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\HttpMessage.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;
 8using Azure.Core.Pipeline;
 9
 10namespace Azure.Core
 11{
 12    /// <summary>
 13    /// Represents a context flowing through the <see cref="HttpPipeline"/>.
 14    /// </summary>
 15    public sealed class HttpMessage: IDisposable
 16    {
 17        private Dictionary<string, object>? _properties;
 18
 19        private Response? _response;
 20
 21        /// <summary>
 22        /// Creates a new instance of <see cref="HttpMessage"/>.
 23        /// </summary>
 24        /// <param name="request">The request.</param>
 25        /// <param name="responseClassifier">The response classifier.</param>
 369226        public HttpMessage(Request request, ResponseClassifier responseClassifier)
 27        {
 369228            Request = request;
 369229            ResponseClassifier = responseClassifier;
 369230            BufferResponse = true;
 369231        }
 32
 33        /// <summary>
 34        /// Gets the <see cref="Request"/> associated with this message.
 35        /// </summary>
 2299836        public Request Request { get; }
 37
 38        /// <summary>
 39        /// Gets the <see cref="Response"/> associated with this message. Throws an exception if it wasn't set yet.
 40        /// To avoid the exception use <see cref="HasResponse"/> property to check.
 41        /// </summary>
 42        public Response Response
 43        {
 44            get
 45            {
 2189046                if (_response == null)
 47                {
 48#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
 049                    throw new InvalidOperationException("Response was not set, make sure SendAsync was called");
 50#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
 51                }
 2189052                return _response;
 53            }
 419454            set => _response = value;
 55        }
 56
 57        /// <summary>
 58        /// Gets the value indicating if the response is set on this message.
 59        /// </summary>
 64060        public bool HasResponse => _response != null;
 61
 62        /// <summary>
 63        /// The <see cref="System.Threading.CancellationToken"/> to be used during the <see cref="HttpMessage"/> process
 64        /// </summary>
 1884665        public CancellationToken CancellationToken { get; internal set; }
 66
 67        /// <summary>
 68        /// The <see cref="ResponseClassifier"/> instance to use for response classification during pipeline invocation.
 69        /// </summary>
 521870        public ResponseClassifier ResponseClassifier { get; }
 71
 72        /// <summary>
 73        /// Gets or sets the value indicating if response would be buffered as part of the pipeline. Defaults to true.
 74        /// </summary>
 820875        public bool BufferResponse { get; set; }
 76
 77        /// <summary>
 78        /// Gets a property that modifies the pipeline behavior. Please refer to individual policies documentation on wh
 79        /// </summary>
 80        /// <param name="name">The property name.</param>
 81        /// <param name="value">The property value.</param>
 82        /// <returns><c>true</c> if property exists, otherwise. <c>false</c>.</returns>
 83        public bool TryGetProperty(string name, out object? value)
 84        {
 685            value = null;
 686            return _properties?.TryGetValue(name, out value) == true;
 87        }
 88
 89        /// <summary>
 90        /// Sets a property that modifies the pipeline behavior. Please refer to individual policies documentation on wh
 91        /// </summary>
 92        /// <param name="name">The property name.</param>
 93        /// <param name="value">The property value.</param>
 94        public void SetProperty(string name, object value)
 95        {
 496            _properties ??= new Dictionary<string, object>();
 97
 498            _properties[name] = value;
 499        }
 100
 101        /// <summary>
 102        /// Returns the response content stream and releases it ownership to the caller. After calling this methods usin
 103        /// </summary>
 104        /// <returns>The content stream or null if response didn't have any.</returns>
 105        public Stream? ExtractResponseContent()
 106        {
 808107            switch (_response?.ContentStream)
 108            {
 109                case ResponseShouldNotBeUsedStream responseContent:
 2110                    return responseContent.Original;
 111                case Stream stream :
 804112                    _response.ContentStream = new ResponseShouldNotBeUsedStream(_response.ContentStream);
 804113                    return stream;
 114                default:
 2115                    return null;
 116            }
 117        }
 118
 119        /// <summary>
 120        /// Disposes the request and response.
 121        /// </summary>
 122        public void Dispose()
 123        {
 1224124            Request?.Dispose();
 1224125            _response?.Dispose();
 1218126        }
 127
 128        private class ResponseShouldNotBeUsedStream: Stream
 129        {
 2130            public Stream Original { get; }
 131
 804132            public ResponseShouldNotBeUsedStream(Stream original)
 133            {
 804134                Original = original;
 804135            }
 136
 137            private static Exception CreateException()
 138            {
 2139                return new InvalidOperationException("The operation has called ExtractResponseContent and will provide t
 140            }
 141
 142            public override void Flush()
 143            {
 0144                throw CreateException();
 145            }
 146
 147            public override int Read(byte[] buffer, int offset, int count)
 148            {
 2149                throw CreateException();
 150            }
 151
 152            public override long Seek(long offset, SeekOrigin origin)
 153            {
 0154                throw CreateException();
 155            }
 156
 157            public override void SetLength(long value)
 158            {
 0159                throw CreateException();
 160            }
 161
 162            public override void Write(byte[] buffer, int offset, int count)
 163            {
 0164                throw CreateException();
 165            }
 166
 0167            public override bool CanRead => throw CreateException();
 0168            public override bool CanSeek => throw CreateException();
 0169            public override bool CanWrite => throw CreateException();
 0170            public override long Length => throw CreateException();
 171
 172            public override long Position
 173            {
 0174                get => throw CreateException();
 0175                set => throw CreateException();
 176            }
 177        }
 178    }
 179}