< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.RabbitMqConnectionSettings
Assembly: CoreWCF.RabbitMQ
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.RabbitMQ/src/CoreWCF/Channels/RabbitMqConnectionSettings.cs
Line coverage
95%
Covered lines: 61
Uncovered lines: 3
Coverable lines: 64
Total lines: 147
Line coverage: 95.3%
Branch coverage
87%
Covered branches: 21
Total branches: 24
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
FromUri(...)50%2295.23%
GetConnectionFactory()100%11100%
GetQueueNameFromUri(...)75%4483.33%
GetExchangeFromUri(...)75%4483.33%
GetRoutingKeyFromUri(...)100%22100%
SetUserName(...)100%44100%
SetPassword(...)100%44100%
ConfigureSslOption(...)100%44100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.RabbitMQ/src/CoreWCF/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 System.Net;
 7using RabbitMQ.Client;
 8
 9namespace CoreWCF.Channels
 10{
 11    public class RabbitMqConnectionSettings
 12    {
 13        private const string TempQueuePrefix = "corewcf-temp-";
 14        private const string AMQPScheme = "amqp";
 15        private const string SecureAMQPScheme = "amqps";
 16        private const string DefaultVirtualHost = "/";
 17
 818        public Uri BaseAddress { get; set; }
 1319        public string Host { get; set; }
 1220        public int Port { get; set; }
 2121        public string Exchange { get; set; } = string.Empty;
 2022        public string QueueName { get; set; }
 1923        public string VirtualHost { get; set; } = DefaultVirtualHost;
 1324        public virtual string RoutingKey { get; set; }
 1125        public string UserName { get; set; }
 1126        public string Password { get; set; }
 1327        public SslOption SslOption { get; set; }
 328        public bool AutomaticRecoveryEnabled => true;
 29
 30        public static RabbitMqConnectionSettings FromUri(
 31            Uri uri,
 32            ICredentials credentials = null,
 33            SslOption sslOption = null,
 34            string virtualHost = DefaultVirtualHost)
 35        {
 836            if (uri == null)
 37            {
 038                throw new ArgumentException($"Parameter {nameof(uri)} cannot be null.");
 39            }
 40
 841            sslOption = ConfigureSslOption(sslOption, uri);
 842            var queueName = GetQueueNameFromUri(uri);
 843            var exchange = GetExchangeFromUri(uri);
 844            var routingKey = GetRoutingKeyFromUri(uri, queueName);
 845            var userName = SetUserName(uri, credentials);
 846            var password = SetPassword(uri, credentials);
 47
 848            return new RabbitMqConnectionSettings
 849            {
 850                BaseAddress = uri,
 851                Host = uri.Host,
 852                Port = uri.Port,
 853                Exchange = exchange,
 854                RoutingKey = routingKey,
 855                QueueName = queueName,
 856                VirtualHost = virtualHost,
 857                UserName = userName,
 858                Password = password,
 859                SslOption = sslOption
 860            };
 61        }
 62
 63        public ConnectionFactory GetConnectionFactory()
 64        {
 365            return new ConnectionFactory
 366            {
 367                HostName = Host,
 368                Port = Port,
 369                VirtualHost = VirtualHost,
 370                UserName = UserName,
 371                Password = Password,
 372                Ssl = SslOption,
 373                AutomaticRecoveryEnabled = AutomaticRecoveryEnabled
 374            };
 75        }
 76
 77        private static string GetQueueNameFromUri(Uri uri)
 78        {
 879            if (uri.Segments.Length < 2)
 80            {
 081                throw new ArgumentException(SR.Format(SR.InvalidRabbitMqUri, uri));
 82            }
 83
 884            var lastSegment = uri.Segments.LastOrDefault();
 885            if (lastSegment.EndsWith("/"))
 86            {
 87                // Exchange found but no queueName, so create unique queueName
 188                return $"{TempQueuePrefix}{new Guid()}";
 89            }
 90
 791            return lastSegment.Replace("/", string.Empty);
 92        }
 93
 94        private static string GetExchangeFromUri(Uri uri)
 95        {
 896            if (uri.Segments.Length < 2)
 97            {
 098                throw new ArgumentException(SR.Format(SR.InvalidRabbitMqUri, uri));
 99            }
 100
 8101            var secondSegment = uri.Segments[1];
 8102            if (secondSegment.EndsWith("/"))
 103            {
 104                // Exchange name was found
 7105                return secondSegment.Replace("/", string.Empty);
 106            }
 107
 108            // Exchange name was not found
 1109            return string.Empty;
 110        }
 111
 112        private static string GetRoutingKeyFromUri(Uri uri, string queueName)
 113        {
 8114            if (string.IsNullOrEmpty(uri.Fragment))
 115            {
 1116                return queueName;
 117            }
 118
 7119            return uri.Fragment.Replace("#", string.Empty);
 120        }
 121
 122        private static string SetUserName(Uri uri, ICredentials credentials)
 123        {
 8124            return credentials?.GetCredential(uri, string.Empty).UserName ?? ConnectionFactory.DefaultUser;
 125        }
 126
 127        private static string SetPassword(Uri uri, ICredentials credentials)
 128        {
 8129            return credentials?.GetCredential(uri, string.Empty).Password ?? ConnectionFactory.DefaultPass;
 130        }
 131
 132        private static SslOption ConfigureSslOption(SslOption sslOption, Uri uri)
 133        {
 8134            if (sslOption != null)
 135            {
 3136                return sslOption;
 137            }
 138
 5139            if (uri.Scheme.Contains(SecureAMQPScheme))
 140            {
 1141                return new SslOption { ServerName = uri.Host, Enabled = true };
 142            }
 143
 4144            return new SslOption();
 145        }
 146    }
 147}