< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Description.UseRequestHeadersForMetadataAddressBehavior
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Description/UseRequestHeadersForMetadataAddressBehavior.cs
Line coverage
91%
Covered lines: 21
Uncovered lines: 2
Coverable lines: 23
Total lines: 76
Line coverage: 91.3%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
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/Description/UseRequestHeadersForMetadataAddressBehavior.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.Collections.Generic;
 6using System.Collections.ObjectModel;
 7using CoreWCF.Channels;
 8using Microsoft.AspNetCore.Http;
 9
 10namespace CoreWCF.Description
 11{
 12    public class UseRequestHeadersForMetadataAddressBehavior : IServiceBehavior
 13    {
 14        internal class UseHostHeaderMetadataEndpointAddressProvider : IMetadataEndpointAddressProvider
 15        {
 16            public Uri GetEndpointAddress(HttpRequest httpRequest)
 17            {
 318                Uri listenUri = httpRequest.HttpContext.Items["CoreWCF.Description.ServiceMetadataExtension.HttpGetImpl.
 19
 320                string host = null;
 321                int port = 0;
 22
 23                // Get the host header
 324                HostString hostString = httpRequest.Host;
 325                if (!hostString.HasValue)
 26                {
 027                    return null;
 28                }
 29
 330                host = hostString.Host;
 331                if (hostString.Port.HasValue)
 32                {
 233                    port = hostString.Port.Value;
 34                }
 35                else
 36                {
 137                    string hostUriString = string.Concat(listenUri.Scheme, "://", host);
 138                    if (!Uri.TryCreate(hostUriString, UriKind.Absolute, out Uri hostUri))
 39                    {
 040                        return null;
 41                    }
 42
 143                    port = hostUri.Port;
 44                }
 45
 346                return new UriBuilder(listenUri.Scheme, host, port).Uri;
 47            }
 48        }
 49
 50        private Dictionary<string, int> _defaultPortsByScheme;
 51
 52        public IDictionary<string, int> DefaultPortsByScheme
 53        {
 54            get
 55            {
 356                if (_defaultPortsByScheme == null)
 57                {
 358                    _defaultPortsByScheme = new Dictionary<string, int>();
 59                }
 60
 361                return _defaultPortsByScheme;
 62            }
 63        }
 64
 365        void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { }
 666        void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBas
 67
 68        void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription,
 69            ServiceHostBase serviceHostBase)
 70        {
 371            var mex = ServiceMetadataExtension.EnsureServiceMetadataExtension(serviceHostBase);
 372            mex.DynamicMetadataEndpointAddressProvider = new UseHostHeaderMetadataEndpointAddressProvider();
 373            mex.UpdatePortsByScheme = new ReadOnlyDictionary<string, int>(DefaultPortsByScheme);
 374        }
 75    }
 76}