Summary

Class:WireMock.Util.PortUtils
Assembly:WireMock.Net
File(s):C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Util\PortUtils.cs
Covered lines:23
Uncovered lines:0
Coverable lines:23
Total lines:55
Line coverage:100%
Branch coverage:100%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
FindFreeTcpPort()0011
TryExtract(...)0011
.cctor()0010

File(s)

C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Util\PortUtils.cs

#LineLine coverage
 1using System.Net;
 2using System.Net.Sockets;
 3using System.Text.RegularExpressions;
 4
 5namespace WireMock.Util
 6{
 7    /// <summary>
 8    /// Port Utility class
 9    /// </summary>
 10    public static class PortUtils
 11    {
 112        private static readonly Regex UrlDetailsRegex = new Regex(@"^((?<proto>\w+)://)(?<host>[^/]+?):(?<port>\d+)\/?$"
 13
 14        /// <summary>
 15        /// Finds a free TCP port.
 16        /// </summary>
 17        /// <remarks>see http://stackoverflow.com/questions/138043/find-the-next-tcp-port-in-net.</remarks>
 18        public static int FindFreeTcpPort()
 5419        {
 5420            TcpListener tcpListener = null;
 21            try
 5422            {
 5423                tcpListener = new TcpListener(IPAddress.Loopback, 0);
 5424                tcpListener.Start();
 25
 5426                return ((IPEndPoint)tcpListener.LocalEndpoint).Port;
 27            }
 28            finally
 5429            {
 5430                tcpListener?.Stop();
 5431            }
 5432        }
 33
 34        /// <summary>
 35        /// Extract the protocol, host and port from a URL.
 36        /// </summary>
 37        public static bool TryExtract(string url, out string protocol, out string host, out int port)
 11438        {
 11439            protocol = null;
 11440            host = null;
 11441            port = default(int);
 42
 11443            Match m = UrlDetailsRegex.Match(url);
 11444            if (m.Success)
 11245            {
 11246                protocol = m.Groups["proto"].Value;
 11247                host = m.Groups["host"].Value;
 48
 11249                return int.TryParse(m.Groups["port"].Value, out port);
 50            }
 51
 252            return false;
 11453        }
 54    }
 55}