Summary

Class:WireMock.Util.PortUtils
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\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\StefHeyenrath\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()
 4619        {
 4620            TcpListener tcpListener = null;
 21            try
 4622            {
 4623                tcpListener = new TcpListener(IPAddress.Loopback, 0);
 4624                tcpListener.Start();
 25
 4626                return ((IPEndPoint)tcpListener.LocalEndpoint).Port;
 27            }
 28            finally
 4629            {
 4630                tcpListener?.Stop();
 4631            }
 4632        }
 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)
 9838        {
 9839            protocol = null;
 9840            host = null;
 9841            port = default(int);
 42
 9843            Match m = UrlDetailsRegex.Match(url);
 9844            if (m.Success)
 9645            {
 9646                protocol = m.Groups["proto"].Value;
 9647                host = m.Groups["host"].Value;
 48
 9649                return int.TryParse(m.Groups["port"].Value, out port);
 50            }
 51
 252            return false;
 9853        }
 54    }
 55}