< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.HttpStreamMessage
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Channels/HttpStreamMessage.cs
Line coverage
31%
Covered lines: 23
Uncovered lines: 50
Coverable lines: 73
Total lines: 203
Line coverage: 31.5%
Branch coverage
25%
Covered branches: 7
Total branches: 28
Branch coverage: 25%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)50%22100%
.ctor(...)0%220%
OnBodyToString(...)0%220%
OnClose()30%101045%
OnCreateBufferedCopy(...)0%220%
OnWriteBodyContents(...)100%11100%
CreateDisposedException()100%110%
.ctor(...)100%110%
Close()0%220%
CreateMessage()0%220%
CreateDisposedException()100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Channels/HttpStreamMessage.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;
 5using System.Xml;
 6using CoreWCF.Runtime;
 7
 8namespace CoreWCF.Channels
 9{
 10    internal class HttpStreamMessage : Message
 11    {
 12        internal const string StreamElementName = "Binary";
 13        private BodyWriter _bodyWriter;
 14        private readonly MessageHeaders _headers;
 15        private readonly MessageProperties _properties;
 16
 417        public HttpStreamMessage(BodyWriter writer)
 18        {
 419            _bodyWriter = writer ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer));
 420            _headers = new MessageHeaders(MessageVersion.None, 1);
 421            _properties = new MessageProperties();
 422        }
 23
 024        public HttpStreamMessage(MessageHeaders headers, MessageProperties properties, BodyWriter bodyWriter)
 25        {
 026            _headers = new MessageHeaders(headers);
 027            _properties = new MessageProperties(properties);
 028            _bodyWriter = bodyWriter ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(bodyWrit
 029        }
 30
 31        public override MessageHeaders Headers
 32        {
 33            get
 34            {
 2035                if (IsDisposed)
 36                {
 037                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateDisposedException());
 38                }
 39
 2040                return _headers;
 41            }
 42        }
 43
 044        public override bool IsEmpty => false;
 45
 446        public override bool IsFault => false;
 47
 48        public override MessageProperties Properties
 49        {
 50            get
 51            {
 2852                if (IsDisposed)
 53                {
 054                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateDisposedException());
 55                }
 56
 2857                return _properties;
 58            }
 59        }
 60
 61        public override MessageVersion Version
 62        {
 63            get
 64            {
 2465                if (IsDisposed)
 66                {
 067                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateDisposedException());
 68                }
 69
 2470                return MessageVersion.None;
 71            }
 72        }
 73
 74        protected override void OnBodyToString(XmlDictionaryWriter writer)
 75        {
 076            if (_bodyWriter.IsBuffered)
 77            {
 078                _bodyWriter.WriteBodyContents(writer);
 79            }
 80            else
 81            {
 082                writer.WriteString(SR.Format(SR.MessageBodyIsStream));
 83            }
 084        }
 85
 86        protected override void OnClose()
 87        {
 488            Exception ex = null;
 89            try
 90            {
 491                base.OnClose();
 492            }
 93            catch (Exception e)
 94            {
 095                if (Fx.IsFatal(e))
 96                {
 097                    throw;
 98                }
 099                ex = e;
 0100            }
 101
 102            try
 103            {
 4104                if (_properties != null)
 105                {
 4106                    _properties.Dispose();
 107                }
 4108            }
 0109            catch (Exception e)
 110            {
 0111                if (Fx.IsFatal(e))
 112                {
 0113                    throw;
 114                }
 0115                if (ex == null)
 116                {
 0117                    ex = e;
 118                }
 0119            }
 120
 4121            if (ex != null)
 122            {
 0123                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex);
 124            }
 125
 4126            _bodyWriter = null;
 4127        }
 128
 129        protected override MessageBuffer OnCreateBufferedCopy(int maxBufferSize)
 130        {
 131            BodyWriter bufferedBodyWriter;
 0132            if (_bodyWriter.IsBuffered)
 133            {
 0134                bufferedBodyWriter = _bodyWriter;
 135            }
 136            else
 137            {
 0138                bufferedBodyWriter = _bodyWriter.CreateBufferedCopy(maxBufferSize);
 139            }
 140
 0141            return new HttpStreamMessageBuffer(Headers, new MessageProperties(Properties), bufferedBodyWriter);
 142        }
 143
 144        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
 145        {
 4146            _bodyWriter.WriteBodyContents(writer);
 4147        }
 148
 0149        private Exception CreateDisposedException() => new ObjectDisposedException("", SR.Format(SR.MessageClosed));
 150
 151        internal class HttpStreamMessageBuffer : MessageBuffer
 152        {
 153            private BodyWriter _bodyWriter;
 154            private bool _closed;
 155            private MessageHeaders _headers;
 156            private MessageProperties _properties;
 157
 158            public HttpStreamMessageBuffer(MessageHeaders headers,
 159                MessageProperties properties, BodyWriter bodyWriter)
 0160                : base()
 161            {
 0162                _bodyWriter = bodyWriter;
 0163                _headers = headers;
 0164                _properties = properties;
 0165            }
 166
 0167            public override int BufferSize => 0;
 168
 0169            private object ThisLock { get; } = new object();
 170
 171            public override void Close()
 172            {
 0173                lock (ThisLock)
 174                {
 0175                    if (!_closed)
 176                    {
 0177                        _closed = true;
 0178                        _bodyWriter = null;
 0179                        _headers = null;
 0180                        _properties = null;
 181                    }
 0182                }
 0183            }
 184
 185            public override Message CreateMessage()
 186            {
 0187                lock (ThisLock)
 188                {
 0189                    if (_closed)
 190                    {
 0191                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateDisposedException());
 192                    }
 0193                    return new HttpStreamMessage(_headers, _properties, _bodyWriter);
 194                }
 0195            }
 196
 197            private Exception CreateDisposedException()
 198            {
 0199                return new ObjectDisposedException("", SR.Format(SR.MessageBufferIsClosed));
 200            }
 201        }
 202    }
 203}