< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.MtomMessageEncodingBindingElement
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/MtomMessageEncodingBindingElement.cs
Line coverage
48%
Covered lines: 51
Uncovered lines: 54
Coverable lines: 105
Total lines: 246
Line coverage: 48.5%
Branch coverage
22%
Covered branches: 11
Total branches: 50
Branch coverage: 22%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/MtomMessageEncodingBindingElement.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.ComponentModel;
 6using System.Text;
 7using System.Xml;
 8using CoreWCF.Description;
 9
 10namespace CoreWCF.Channels
 11{
 12    public sealed class MtomMessageEncodingBindingElement : MessageEncodingBindingElement, IWsdlExportExtension, IPolicy
 13    {
 14        private int _maxReadPoolSize;
 15        private int _maxWritePoolSize;
 16        private XmlDictionaryReaderQuotas _readerQuotas;
 17        private int _maxBufferSize;
 18        private Encoding _writeEncoding;
 19        private MessageVersion _messageVersion;
 20
 77021        public MtomMessageEncodingBindingElement() : this(MessageVersion.Default, TextEncoderDefaults.Encoding) { }
 22
 38523        public MtomMessageEncodingBindingElement(MessageVersion messageVersion, Encoding writeEncoding)
 24        {
 38525            if (messageVersion == null)
 026                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(messageVersion));
 27
 38528            if (messageVersion == MessageVersion.None)
 029                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.MtomEncoder
 30
 38531            if (writeEncoding == null)
 032                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writeEncoding));
 33
 38534            TextEncoderDefaults.ValidateEncoding(writeEncoding);
 38535            _maxReadPoolSize = EncoderDefaults.MaxReadPoolSize;
 38536            _maxWritePoolSize = EncoderDefaults.MaxWritePoolSize;
 38537            _readerQuotas = new XmlDictionaryReaderQuotas();
 38538            EncoderDefaults.ReaderQuotas.CopyTo(_readerQuotas);
 38539            _maxBufferSize = MtomEncoderDefaults.MaxBufferSize;
 38540            _messageVersion = messageVersion;
 38541            _writeEncoding = writeEncoding;
 38542        }
 43
 44        private MtomMessageEncodingBindingElement(MtomMessageEncodingBindingElement elementToBeCloned)
 25545            : base(elementToBeCloned)
 46        {
 25547            _maxReadPoolSize = elementToBeCloned._maxReadPoolSize;
 25548            _maxWritePoolSize = elementToBeCloned._maxWritePoolSize;
 25549            _readerQuotas = new XmlDictionaryReaderQuotas();
 25550            elementToBeCloned._readerQuotas.CopyTo(_readerQuotas);
 25551            _maxBufferSize = elementToBeCloned._maxBufferSize;
 25552            _writeEncoding = elementToBeCloned._writeEncoding;
 25553            _messageVersion = elementToBeCloned._messageVersion;
 25554        }
 55
 56        [DefaultValue(EncoderDefaults.MaxReadPoolSize)]
 57        public int MaxReadPoolSize
 58        {
 59            get
 60            {
 1561                return _maxReadPoolSize;
 62            }
 63            set
 64            {
 265                if (value <= 0)
 66                {
 067                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 068                                                    SRCommon.ValueMustBePositive));
 69                }
 270                _maxReadPoolSize = value;
 271            }
 72        }
 73
 74        [DefaultValue(EncoderDefaults.MaxWritePoolSize)]
 75        public int MaxWritePoolSize
 76        {
 77            get
 78            {
 1579                return _maxWritePoolSize;
 80            }
 81            set
 82            {
 283                if (value <= 0)
 84                {
 085                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 086                                                    SRCommon.ValueMustBePositive));
 87                }
 288                _maxWritePoolSize = value;
 289            }
 90        }
 91
 92        public XmlDictionaryReaderQuotas ReaderQuotas
 93        {
 94            get
 95            {
 387896                return _readerQuotas;
 97            }
 98        }
 99
 100        [DefaultValue(MtomEncoderDefaults.MaxBufferSize)]
 101        public int MaxBufferSize
 102        {
 103            get
 104            {
 13105                return _maxBufferSize;
 106            }
 107            set
 108            {
 18109                if (value <= 0)
 110                {
 0111                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 0112                                                    SRCommon.ValueMustBePositive));
 113                }
 18114                _maxBufferSize = value;
 18115            }
 116        }
 117
 118        public Encoding WriteEncoding
 119        {
 120            get
 121            {
 15122                return _writeEncoding;
 123            }
 124            set
 125            {
 3862126                if (value == null)
 0127                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
 128
 3862129                TextEncoderDefaults.ValidateEncoding(value);
 3862130                _writeEncoding = value;
 3862131            }
 132        }
 133
 134        public override MessageVersion MessageVersion
 135        {
 136            get
 137            {
 84138                return _messageVersion;
 139            }
 140            set
 141            {
 410142                if (value == null)
 143                {
 0144                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
 145                }
 410146                if (value == MessageVersion.None)
 147                {
 0148                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.MtomEnc
 149                }
 150
 410151                _messageVersion = value;
 410152            }
 153        }
 154
 155        public override BindingElement Clone()
 156        {
 255157            return new MtomMessageEncodingBindingElement(this);
 158        }
 159
 160        public override MessageEncoderFactory CreateMessageEncoderFactory()
 161        {
 13162            return new MtomMessageEncoderFactory(MessageVersion, WriteEncoding, MaxReadPoolSize, MaxWritePoolSize, MaxBu
 163        }
 164
 165        public override T GetProperty<T>(BindingContext context)
 166        {
 108167            if (context == null)
 168            {
 0169                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context));
 170            }
 108171            if (typeof(T) == typeof(XmlDictionaryReaderQuotas))
 172            {
 0173                return (T)(object)_readerQuotas;
 174            }
 175            else
 176            {
 108177                return base.GetProperty<T>(context);
 178            }
 179        }
 180
 181        void IPolicyExportExtension.ExportPolicy(MetadataExporter exporter, PolicyConversionContext policyContext)
 182        {
 0183            if (policyContext == null)
 184            {
 0185                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(policyContext));
 186            }
 0187            XmlDocument document = new XmlDocument();
 188
 0189            policyContext.GetBindingAssertions().Add(document.CreateElement(
 0190                MessageEncodingPolicyConstants.OptimizedMimeSerializationPrefix,
 0191                MessageEncodingPolicyConstants.MtomEncodingName,
 0192                MessageEncodingPolicyConstants.OptimizedMimeSerializationNamespace));
 0193        }
 194
 0195        void IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext context) { }
 196        void IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
 197        {
 0198            if (context == null)
 199            {
 0200                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context));
 201            }
 202
 0203            SoapHelper.SetSoapVersion(context, exporter, MessageVersion.Envelope);
 0204        }
 205
 206        protected override bool CheckEncodingVersion(EnvelopeVersion version)
 207        {
 0208            return _messageVersion.Envelope == version;
 209        }
 210
 211        protected override bool IsMatch(BindingElement b)
 212        {
 0213            if (!base.IsMatch(b))
 0214                return false;
 0215            MtomMessageEncodingBindingElement mtom = b as MtomMessageEncodingBindingElement;
 0216            if (mtom == null)
 0217                return false;
 0218            if (_maxReadPoolSize != mtom.MaxReadPoolSize)
 0219                return false;
 0220            if (_maxWritePoolSize != mtom.MaxWritePoolSize)
 0221                return false;
 222
 223            // compare XmlDictionaryReaderQuotas
 0224            if (_readerQuotas.MaxStringContentLength != mtom.ReaderQuotas.MaxStringContentLength)
 0225                return false;
 0226            if (_readerQuotas.MaxArrayLength != mtom.ReaderQuotas.MaxArrayLength)
 0227                return false;
 0228            if (_readerQuotas.MaxBytesPerRead != mtom.ReaderQuotas.MaxBytesPerRead)
 0229                return false;
 0230            if (_readerQuotas.MaxDepth != mtom.ReaderQuotas.MaxDepth)
 0231                return false;
 0232            if (_readerQuotas.MaxNameTableCharCount != mtom.ReaderQuotas.MaxNameTableCharCount)
 0233                return false;
 234
 0235            if (_maxBufferSize != mtom.MaxBufferSize)
 0236                return false;
 237
 0238            if (WriteEncoding.EncodingName != mtom.WriteEncoding.EncodingName)
 0239                return false;
 0240            if (!MessageVersion.IsMatch(mtom.MessageVersion))
 0241                return false;
 242
 0243            return true;
 244        }
 245    }
 246}