# 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

Method | Cyclomatic complexity | NPath complexity | Sequence coverage | Branch coverage
--- | --- | --- | --- | ---
FindFreeTcpPort() | 0 | 0 | 1 | 1
TryExtract(...) | 0 | 0 | 1 | 1
.cctor() | 0 | 0 | 1 | 0

# File(s)

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

| # | Line |  | Line coverage
--- | --- | --- | --- | ---
|  | `1` |  | `using System.Net;`
|  | `2` |  | `using System.Net.Sockets;`
|  | `3` |  | `using System.Text.RegularExpressions;`
|  | `4` |  | ``
|  | `5` |  | `namespace WireMock.Util`
|  | `6` |  | `{`
|  | `7` |  | `/// <summary>`
|  | `8` |  | `/// Port Utility class`
|  | `9` |  | `/// </summary>`
|  | `10` |  | `public static class PortUtils`
|  | `11` |  | `{`
| 1 | `12` |  | `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()`
| 54 | `19` |  | `{`
| 54 | `20` |  | `TcpListener tcpListener = null;`
|  | `21` |  | `try`
| 54 | `22` |  | `{`
| 54 | `23` |  | `tcpListener = new TcpListener(IPAddress.Loopback, 0);`
| 54 | `24` |  | `tcpListener.Start();`
|  | `25` |  | ``
| 54 | `26` |  | `return ((IPEndPoint)tcpListener.LocalEndpoint).Port;`
|  | `27` |  | `}`
|  | `28` |  | `finally`
| 54 | `29` |  | `{`
| 54 | `30` |  | `tcpListener?.Stop();`
| 54 | `31` |  | `}`
| 54 | `32` |  | `}`
|  | `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)`
| 114 | `38` |  | `{`
| 114 | `39` |  | `protocol = null;`
| 114 | `40` |  | `host = null;`
| 114 | `41` |  | `port = default(int);`
|  | `42` |  | ``
| 114 | `43` |  | `Match m = UrlDetailsRegex.Match(url);`
| 114 | `44` |  | `if (m.Success)`
| 112 | `45` |  | `{`
| 112 | `46` |  | `protocol = m.Groups["proto"].Value;`
| 112 | `47` |  | `host = m.Groups["host"].Value;`
|  | `48` |  | ``
| 112 | `49` |  | `return int.TryParse(m.Groups["port"].Value, out port);`
|  | `50` |  | `}`
|  | `51` |  | ``
| 2 | `52` |  | `return false;`
| 114 | `53` |  | `}`
|  | `54` |  | `}`
|  | `55` |  | `}`

# Methods/Properties

.cctor()
FindFreeTcpPort()
TryExtract(System.String,System.String&,System.String&,System.Int32&)