< Summary

Information
Class: System.Net.Http.HttpResponseMessage
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\HttpResponseMessage.cs
Line coverage
9%
Covered lines: 12
Uncovered lines: 111
Coverable lines: 123
Total lines: 234
Line coverage: 9.7%
Branch coverage
4%
Covered branches: 2
Total branches: 42
Branch coverage: 4.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
SetVersionWithoutValidation(...)100%110%
SetStatusCodeWithoutValidation(...)100%110%
SetReasonPhraseWithoutValidation(...)100%110%
StoreReceivedTrailingHeaders(...)0%220%
.ctor()100%11100%
.ctor(...)100%11100%
EnsureSuccessStatusCode()0%440%
ToString()0%880%
Dispose(...)0%660%
Dispose()100%110%
CheckDisposed()100%110%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\HttpResponseMessage.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Net.Http.Headers;
 7using System.Text;
 8
 9namespace System.Net.Http
 10{
 11    public class HttpResponseMessage : IDisposable
 12    {
 13        private const HttpStatusCode DefaultStatusCode = HttpStatusCode.OK;
 114        private static Version DefaultResponseVersion => HttpVersion.Version11;
 15
 16        private HttpStatusCode _statusCode;
 17        private HttpResponseHeaders? _headers;
 18        private HttpResponseHeaders? _trailingHeaders;
 19        private string? _reasonPhrase;
 20        private HttpRequestMessage? _requestMessage;
 21        private Version _version;
 22        private HttpContent? _content;
 23        private bool _disposed;
 24
 25        public Version Version
 26        {
 027            get { return _version; }
 28            set
 029            {
 30#if !PHONE
 031                ArgumentNullException.ThrowIfNull(value);
 32#endif
 033                CheckDisposed();
 34
 035                _version = value;
 036            }
 37        }
 38
 039        internal void SetVersionWithoutValidation(Version value) => _version = value;
 40
 41        [AllowNull]
 42        public HttpContent Content
 43        {
 044            get { return _content ??= new EmptyContent(); }
 45            set
 046            {
 047                CheckDisposed();
 48
 049                if (NetEventSource.Log.IsEnabled())
 050                {
 051                    if (value == null)
 052                    {
 053                        NetEventSource.ContentNull(this);
 054                    }
 55                    else
 056                    {
 057                        NetEventSource.Associate(this, value);
 058                    }
 059                }
 60
 061                _content = value;
 062            }
 63        }
 64
 65        public HttpStatusCode StatusCode
 66        {
 067            get { return _statusCode; }
 68            set
 069            {
 070                ArgumentOutOfRangeException.ThrowIfNegative((int)value, nameof(value));
 071                ArgumentOutOfRangeException.ThrowIfGreaterThan((int)value, 999, nameof(value));
 072                CheckDisposed();
 73
 074                _statusCode = value;
 075            }
 76        }
 77
 078        internal void SetStatusCodeWithoutValidation(HttpStatusCode value) => _statusCode = value;
 79
 80        public string? ReasonPhrase
 81        {
 82            get
 083            {
 084                if (_reasonPhrase != null)
 085                {
 086                    return _reasonPhrase;
 87                }
 88                // Provide a default if one was not set.
 089                return HttpStatusDescription.Get(StatusCode);
 090            }
 91            set
 092            {
 093                if ((value != null) && HttpRuleParser.ContainsNewLineOrNull(value))
 094                {
 095                    throw new FormatException(SR.net_http_reasonphrase_format_error);
 96                }
 097                CheckDisposed();
 98
 099                _reasonPhrase = value; // It's OK to have a 'null' reason phrase.
 0100            }
 101        }
 102
 0103        internal void SetReasonPhraseWithoutValidation(string value) => _reasonPhrase = value;
 104
 1105        public HttpResponseHeaders Headers => _headers ??= new HttpResponseHeaders();
 106
 0107        public HttpResponseHeaders TrailingHeaders => _trailingHeaders ??= new HttpResponseHeaders(containsTrailingHeade
 108
 109        /// <summary>Stores the supplied trailing headers into this instance.</summary>
 110        /// <remarks>
 111        /// In the common/desired case where response.TrailingHeaders isn't accessed until after the whole payload has b
 112        /// received, <see cref="_trailingHeaders" /> will still be null, and we can simply store the supplied instance 
 113        /// <see cref="_trailingHeaders" /> and assume ownership of the instance.  In the uncommon case where it was acc
 114        /// we add all of the headers to the existing instance.
 115        /// </remarks>
 116        internal void StoreReceivedTrailingHeaders(HttpResponseHeaders headers)
 0117        {
 0118            Debug.Assert(headers.ContainsTrailingHeaders);
 119
 0120            if (_trailingHeaders is null)
 0121            {
 0122                _trailingHeaders = headers;
 0123            }
 124            else
 0125            {
 0126                _trailingHeaders.AddHeaders(headers);
 0127            }
 0128        }
 129
 130        public HttpRequestMessage? RequestMessage
 131        {
 0132            get { return _requestMessage; }
 133            set
 0134            {
 0135                CheckDisposed();
 0136                if (value is not null && NetEventSource.Log.IsEnabled())
 0137                    NetEventSource.Associate(this, value);
 0138                _requestMessage = value;
 0139            }
 140        }
 141
 142        public bool IsSuccessStatusCode
 143        {
 0144            get { return ((int)_statusCode >= 200) && ((int)_statusCode <= 299); }
 145        }
 146
 147        public HttpResponseMessage()
 1148            : this(DefaultStatusCode)
 1149        {
 1150        }
 151
 1152        public HttpResponseMessage(HttpStatusCode statusCode)
 1153        {
 1154            ArgumentOutOfRangeException.ThrowIfNegative((int)statusCode, nameof(statusCode));
 1155            ArgumentOutOfRangeException.ThrowIfGreaterThan((int)statusCode, 999, nameof(statusCode));
 156
 1157            _statusCode = statusCode;
 1158            _version = DefaultResponseVersion;
 1159        }
 160
 161        public HttpResponseMessage EnsureSuccessStatusCode()
 0162        {
 0163            if (!IsSuccessStatusCode)
 0164            {
 0165                throw new HttpRequestException(
 0166                    SR.Format(
 0167                        System.Globalization.CultureInfo.InvariantCulture,
 0168                        string.IsNullOrWhiteSpace(ReasonPhrase) ? SR.net_http_message_not_success_statuscode : SR.net_ht
 0169                        (int)_statusCode,
 0170                        ReasonPhrase),
 0171                    inner: null,
 0172                    _statusCode);
 173            }
 174
 0175            return this;
 0176        }
 177
 178        public override string ToString()
 0179        {
 0180            ValueStringBuilder sb = new ValueStringBuilder(stackalloc char[512]);
 181
 0182            sb.Append("StatusCode: ");
 0183            sb.AppendSpanFormattable((int)_statusCode);
 184
 0185            sb.Append(", ReasonPhrase: '");
 0186            sb.Append(ReasonPhrase ?? "<null>");
 187
 0188            sb.Append("', Version: ");
 0189            sb.AppendSpanFormattable(_version);
 190
 0191            sb.Append(", Content: ");
 0192            sb.Append(_content == null ? "<null>" : _content.GetType().ToString());
 193
 0194            sb.Append(", Headers:");
 0195            sb.Append(Environment.NewLine);
 0196            HeaderUtilities.DumpHeaders(ref sb, _headers, _content?.Headers);
 197
 0198            if (_trailingHeaders != null)
 0199            {
 0200                sb.Append(", Trailing Headers:");
 0201                sb.Append(Environment.NewLine);
 0202                HeaderUtilities.DumpHeaders(ref sb, _trailingHeaders);
 0203            }
 204
 0205            return sb.ToString();
 0206        }
 207
 208        #region IDisposable Members
 209
 210        protected virtual void Dispose(bool disposing)
 0211        {
 212            // The reason for this type to implement IDisposable is that it contains instances of types that implement
 213            // IDisposable (content).
 0214            if (disposing && !_disposed)
 0215            {
 0216                _disposed = true;
 0217                _content?.Dispose();
 0218            }
 0219        }
 220
 221        public void Dispose()
 0222        {
 0223            Dispose(true);
 0224            GC.SuppressFinalize(this);
 0225        }
 226
 227        #endregion
 228
 229        private void CheckDisposed()
 0230        {
 0231            ObjectDisposedException.ThrowIf(_disposed, this);
 0232        }
 233    }
 234}