< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.ServiceModel.Channels.RabbitMqConnectionSettings
Assembly: CoreWCF.RabbitMQ.Client
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.RabbitMQ.Client/src/CoreWCF/ServiceModel/Channels/RabbitMqConnectionSettings.cs
Line coverage
85%
Covered lines: 51
Uncovered lines: 9
Coverable lines: 60
Total lines: 136
Line coverage: 85%
Branch coverage
55%
Covered branches: 11
Total branches: 20
Branch coverage: 55%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
FromUri(...)50%2294.73%
GetConnectionFactory(...)100%44100%
GetQueueNameFromUri(...)50%4466.66%
GetExchangeFromUri(...)50%4466.66%
GetRoutingKeyFromUri(...)50%2266.66%
ConfigureSslOption(...)25%4440%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.RabbitMQ.Client/src/CoreWCF/ServiceModel/Channels/RabbitMqConnectionSettings.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.Linq;
 6using RabbitMQ.Client;
 7
 8namespace CoreWCF.ServiceModel.Channels
 9{
 10    public class RabbitMqConnectionSettings
 11    {
 12        private const string TempQueuePrefix = "corewcf-temp-";
 13        private const string AMQPScheme = "amqp";
 14        private const string SecureAMQPScheme = "amqps";
 15        private const string DefaultVirtualHost = "/";
 16
 317        public Uri BaseAddress { get; set; }
 618        public string Host { get; set; }
 619        public int Port { get; set; }
 920        public string Exchange { get; set; } = string.Empty;
 321        public string QueueName { get; set; }
 922        public string VirtualHost { get; set; } = DefaultVirtualHost;
 623        public virtual string RoutingKey { get; set; }
 924        public string UserName { get; set; } = ConnectionFactory.DefaultUser;
 925        public string Password { get; set; } = ConnectionFactory.DefaultPass;
 626        public SslOption SslOption { get; set; }
 327        public bool AutomaticRecoveryEnabled => true;
 28
 29        public static RabbitMqConnectionSettings FromUri(
 30            Uri uri,
 31            string userName,
 32            string password,
 33            SslOption sslOption = null,
 34            string virtualHost = DefaultVirtualHost)
 35        {
 336            if (uri == null)
 37            {
 038                throw new ArgumentException($"Parameter {nameof(uri)} cannot be null.");
 39            }
 40
 341            sslOption = ConfigureSslOption(sslOption, uri);
 342            var queueName = GetQueueNameFromUri(uri);
 343            var exchange = GetExchangeFromUri(uri);
 344            var routingKey = GetRoutingKeyFromUri(uri, queueName);
 45
 346            return new RabbitMqConnectionSettings
 347            {
 348                BaseAddress = uri,
 349                Host = uri.Host,
 350                Port = uri.Port,
 351                Exchange = exchange,
 352                RoutingKey = routingKey,
 353                QueueName = queueName,
 354                VirtualHost = virtualHost,
 355                UserName = userName,
 356                Password = password,
 357                SslOption = sslOption
 358            };
 59        }
 60
 61        public ConnectionFactory GetConnectionFactory(string userName = null, string password = null)
 62        {
 363            return new ConnectionFactory
 364            {
 365                HostName = Host,
 366                Port = Port,
 367                VirtualHost = VirtualHost,
 368                UserName = userName ?? UserName,
 369                Password = password ?? Password,
 370                Ssl = SslOption,
 371                AutomaticRecoveryEnabled = AutomaticRecoveryEnabled
 372            };
 73        }
 74
 75        private static string GetQueueNameFromUri(Uri uri)
 76        {
 377            if (uri.Segments.Length < 2)
 78            {
 079                throw new ArgumentException(SR.Format(SR.InvalidRabbitMqUri, uri));
 80            }
 81
 382            var lastSegment = uri.Segments.LastOrDefault();
 383            if (lastSegment.EndsWith("/"))
 84            {
 85                // Exchange found but no queueName, so create unique queueName
 086                return $"{TempQueuePrefix}{new Guid()}";
 87            }
 88
 389            return lastSegment.Replace("/", string.Empty);
 90        }
 91
 92        private static string GetExchangeFromUri(Uri uri)
 93        {
 394            if (uri.Segments.Length < 2)
 95            {
 096                throw new ArgumentException(SR.Format(SR.InvalidRabbitMqUri, uri));
 97            }
 98
 399            var secondSegment = uri.Segments[1];
 3100            if (secondSegment.EndsWith("/"))
 101            {
 102                // Exchange name was found
 3103                return secondSegment.Replace("/", string.Empty);
 104            }
 105
 106            // Exchange name was not found
 0107            return string.Empty;
 108        }
 109
 110        private static string GetRoutingKeyFromUri(Uri uri, string queueName)
 111        {
 3112            if (string.IsNullOrEmpty(uri.Fragment))
 113            {
 0114                return queueName;
 115            }
 116
 3117            return uri.Fragment.Replace("#", string.Empty);
 118        }
 119
 120
 121        private static SslOption ConfigureSslOption(SslOption sslOption, Uri uri)
 122        {
 3123            if (sslOption != null)
 124            {
 3125                return sslOption;
 126            }
 127
 0128            if (uri.Scheme.Contains(SecureAMQPScheme))
 129            {
 0130                return new SslOption { ServerName = uri.Host, Enabled = true };
 131            }
 132
 0133            return new SslOption();
 134        }
 135    }
 136}