Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Sep 20, 2018
1 parent 40ab1cd commit dbf72e2
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/WireMock.Net/Owin/AspNetCoreSelfHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public AspNetCoreSelfHost([NotNull] WireMockMiddlewareOptions options, [NotNull]
{
Urls.Add(uriPrefix);

PortUtils.TryExtractProtocolAndPort(uriPrefix, out string host, out int port);
PortUtils.TryExtract(uriPrefix, out string protocol, out string host, out int port);
Ports.Add(port);
}

Expand Down Expand Up @@ -75,13 +75,13 @@ public Task StartAsync()
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?tabs=aspnetcore2x
foreach (string url in _urls.Where(u => u.StartsWith("http://", StringComparison.OrdinalIgnoreCase)))
{
PortUtils.TryExtractProtocolAndPort(url, out string host, out int port);
PortUtils.TryExtract(url, out string protocol, out string host, out int port);
options.Listen(System.Net.IPAddress.Any, port);
}

foreach (string url in _urls.Where(u => u.StartsWith("https://", StringComparison.OrdinalIgnoreCase)))
{
PortUtils.TryExtractProtocolAndPort(url, out string host, out int port);
PortUtils.TryExtract(url, out string protocol, out string host, out int port);
options.Listen(System.Net.IPAddress.Any, port, listenOptions =>
{
listenOptions.UseHttps(PublicCertificateHelper.GetX509Certificate2());
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net/Owin/OwinSelfHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public OwinSelfHost([NotNull] WireMockMiddlewareOptions options, [NotNull] param
{
Urls.Add(uriPrefix);

PortUtils.TryExtractProtocolAndPort(uriPrefix, out string host, out int port);
PortUtils.TryExtract(uriPrefix, out string protocol, out string host, out int port);
Ports.Add(port);
}

Expand Down
14 changes: 8 additions & 6 deletions src/WireMock.Net/Util/PortUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace WireMock.Util
/// </summary>
public static class PortUtils
{
private static readonly Regex UrlDetailsRegex = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>\d+)/?", RegexOptions.Compiled);
private static readonly Regex UrlDetailsRegex = new Regex(@"^((?<proto>\w+)://)(?<host>[^/]+?):(?<port>\d+)\/?$", RegexOptions.Compiled);

/// <summary>
/// Finds a free TCP port.
Expand All @@ -32,17 +32,19 @@ public static int FindFreeTcpPort()
}

/// <summary>
/// Extract a proto and port from a URL.
/// Extract the protocol, host and port from a URL.
/// </summary>
public static bool TryExtractProtocolAndPort(string url, out string proto, out int port)
public static bool TryExtract(string url, out string protocol, out string host, out int port)
{
proto = null;
port = 0;
protocol = null;
host = null;
port = default(int);

Match m = UrlDetailsRegex.Match(url);
if (m.Success)
{
proto = m.Groups["proto"].Value;
protocol = m.Groups["proto"].Value;
host = m.Groups["host"].Value;

return int.TryParse(m.Groups["port"].Value, out port);
}
Expand Down
85 changes: 85 additions & 0 deletions test/WireMock.Net.Tests/Util/PortUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using NFluent;
using WireMock.Util;
using Xunit;

namespace WireMock.Net.Tests.Util
{
public class PortUtilsTests
{
[Fact]
public void PortUtils_TryExtract_InvalidUrl_Returns_False()
{
// Assign
string url = "test";
string proto = "x";
string host = "h";
int port = 1;

// Act
var result = PortUtils.TryExtract(url, out proto, out host, out port);

// Assert
Check.That(result).IsFalse();
Check.That(proto).IsNull();
Check.That(host).IsNull();
Check.That(port).IsEqualTo(default(int));
}

[Fact]
public void PortUtils_TryExtract_UrlIsMissingPort_Returns_False()
{
// Assign
string url = "http://0.0.0.0";
string proto = "x";
string host = "h";
int port = 1;

// Act
var result = PortUtils.TryExtract(url, out proto, out host, out port);

// Assert
Check.That(result).IsFalse();
Check.That(proto).IsNull();
Check.That(host).IsNull();
Check.That(port).IsEqualTo(default(int));
}

[Fact]
public void PortUtils_TryExtract_ValidUrl1_Returns_True()
{
// Assign
string url = "https://wiremock.net:5000";
string proto = "x";
string host = "h";
int port = 1;

// Act
var result = PortUtils.TryExtract(url, out proto, out host, out port);

// Assert
Check.That(result).IsTrue();
Check.That(proto).IsEqualTo("https");
Check.That(host).IsEqualTo("wiremock.net");
Check.That(port).IsEqualTo(5000);
}

[Fact]
public void PortUtils_TryExtract_ValidUrl2_Returns_True()
{
// Assign
string url = "https://0.0.0.0:5000";
string proto = "x";
string host = "h";
int port = 1;

// Act
var result = PortUtils.TryExtract(url, out proto, out host, out port);

// Assert
Check.That(result).IsTrue();
Check.That(proto).IsEqualTo("https");
Check.That(host).IsEqualTo("0.0.0.0");
Check.That(port).IsEqualTo(5000);
}
}
}

0 comments on commit dbf72e2

Please sign in to comment.