# WireMock.Http.PortUtil - Coverage Report

# 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

Method | Cyclomatic complexity | NPath complexity | Sequence coverage | Branch coverage
--- | --- | --- | --- | ---
FindFreeTcpPort() | 2 | 2 | 100 | 66.67
TryExtractProtocolAndPort(...) | 2 | 2 | 90 | 66.67
.cctor() | 1 | 0 | 100 | 100

# File(s)

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

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

# Methods/Properties

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