Summary

Class:WireMock.Http.PortUtil
Assembly:WireMock.Net
File(s):C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Http\PortUtil.cs
Covered lines:20
Uncovered lines:1
Coverable lines:21
Total lines:53
Line coverage:95.2%
Branch coverage:50%

Metrics

MethodCyclomatic complexity  NPath complexity  Sequence coverage  Branch coverage  
FindFreeTcpPort()2210066.67
TryExtractProtocolAndPort(...)229066.67
.cctor()10100100

File(s)

C:\Users\azureuser\Documents\Github\WireMock.Net\src\WireMock.Net\Http\PortUtil.cs

#LineLine coverage
 1using System.Net;
 2using System.Net.Sockets;
 3using System.Text.RegularExpressions;
 4
 5namespace WireMock.Http
 6{
 7    /// <summary>
 8    /// Port Utility class
 9    /// </summary>
 10    public static class PortUtil
 11    {
 112        private static readonly Regex UrlDetailsRegex = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>\d+)?/", RegexOptions
 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()
 4519        {
 4520            TcpListener tcpListener = null;
 21            try
 4522            {
 4523                tcpListener = new TcpListener(IPAddress.Loopback, 0);
 4524                tcpListener.Start();
 25
 4526                return ((IPEndPoint)tcpListener.LocalEndpoint).Port;
 27            }
 28            finally
 4529            {
 4530                 tcpListener?.Stop();
 4531            }
 4532        }
 33
 34        /// <summary>
 35        /// Extract a proto and port from a URL.
 36        /// </summary>
 37        public static bool TryExtractProtocolAndPort(string url, out string proto, out int port)
 4738        {
 4739            proto = null;
 4740            port = 0;
 41
 4742            Match m = UrlDetailsRegex.Match(url);
 4743             if (m.Success)
 4744            {
 4745                proto = m.Groups["proto"].Value;
 46
 4747                return int.TryParse(m.Groups["port"].Value, out port);
 48            }
 49
 050            return false;
 4751        }
 52    }
 53}