< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.MultiplexingFormatMapping
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Dispatcher/MultiplexingFormatMapping.cs
Line coverage
32%
Covered lines: 9
Uncovered lines: 19
Coverable lines: 28
Total lines: 86
Line coverage: 32.1%
Branch coverage
13%
Covered branches: 3
Total branches: 22
Branch coverage: 13.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)50%2285.71%
CanFormatResponse(...)0%18180%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/Dispatcher/MultiplexingFormatMapping.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.Net.Mime;
 6using System.Text;
 7using CoreWCF.Channels;
 8using CoreWCF.Web;
 9
 10namespace CoreWCF.Dispatcher
 11{
 12    internal abstract class MultiplexingFormatMapping
 13    {
 14        protected Encoding writeEncoding;
 15        protected string writeCharset;
 16        protected WebContentTypeMapper contentTypeMapper;
 17
 18        public abstract WebMessageFormat MessageFormat { get; }
 19        public abstract WebContentFormat ContentFormat { get; }
 20        public abstract string DefaultMediaType { get; }
 21        protected abstract MessageEncoder Encoder { get; }
 22
 23        private ContentType _defaultContentType;
 24
 25        public ContentType DefaultContentType
 26        {
 27            get
 28            {
 7429                if (_defaultContentType == null)
 30                {
 7431                    _defaultContentType = new ContentType(DefaultMediaType) { CharSet = writeCharset };
 32                }
 33
 7434                return _defaultContentType;
 35            }
 36        }
 37
 7438        public MultiplexingFormatMapping(Encoding writeEncoding, WebContentTypeMapper contentTypeMapper)
 39        {
 7440            if (writeEncoding == null)
 41            {
 042                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writeEncoding));
 43            }
 44
 7445            this.writeEncoding = writeEncoding;
 7446            writeCharset = TextEncoderDefaults.EncodingToCharSet(writeEncoding);
 7447            this.contentTypeMapper = contentTypeMapper;
 7448        }
 49
 50        public bool CanFormatResponse(ContentType acceptHeaderElement, bool matchCharset, out ContentType contentType)
 51        {
 052            if (acceptHeaderElement == null)
 53            {
 054                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(acceptHeaderElement));
 55            }
 56
 57            // Scrub the content type so that it is only mediaType and the charset
 058            string charset = acceptHeaderElement.CharSet;
 059            contentType = new ContentType(acceptHeaderElement.MediaType);
 060            contentType.CharSet = DefaultContentType.CharSet;
 061            string contentTypeStr = contentType.ToString();
 62
 063            if (matchCharset &&
 064                !string.IsNullOrEmpty(charset) &&
 065                !string.Equals(charset, DefaultContentType.CharSet, StringComparison.OrdinalIgnoreCase))
 66            {
 067                return false;
 68            }
 69
 070            if (contentTypeMapper != null &&
 071                contentTypeMapper.GetMessageFormatForContentType(contentType.MediaType) == ContentFormat)
 72            {
 073                return true;
 74            }
 75
 076            if (Encoder.IsContentTypeSupported(contentTypeStr) &&
 077                (charset == null || contentType.CharSet == DefaultContentType.CharSet))
 78            {
 079                return true;
 80            }
 81
 082            contentType = null;
 083            return false;
 84        }
 85    }
 86}