From dbf72e21a0752f9d27d9b237d9dc8d9531f52ca0 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Thu, 20 Sep 2018 13:48:24 +0200 Subject: [PATCH] #200 --- src/WireMock.Net/Owin/AspNetCoreSelfHost.cs | 6 +- src/WireMock.Net/Owin/OwinSelfHost.cs | 2 +- src/WireMock.Net/Util/PortUtils.cs | 14 +-- .../WireMock.Net.Tests/Util/PortUtilsTests.cs | 85 +++++++++++++++++++ 4 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 test/WireMock.Net.Tests/Util/PortUtilsTests.cs diff --git a/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs b/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs index ff3eb8e02..8e69624a9 100644 --- a/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs +++ b/src/WireMock.Net/Owin/AspNetCoreSelfHost.cs @@ -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); } @@ -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()); diff --git a/src/WireMock.Net/Owin/OwinSelfHost.cs b/src/WireMock.Net/Owin/OwinSelfHost.cs index 8710926b3..26a2a61b2 100644 --- a/src/WireMock.Net/Owin/OwinSelfHost.cs +++ b/src/WireMock.Net/Owin/OwinSelfHost.cs @@ -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); } diff --git a/src/WireMock.Net/Util/PortUtils.cs b/src/WireMock.Net/Util/PortUtils.cs index 65861313c..83ffe39f6 100644 --- a/src/WireMock.Net/Util/PortUtils.cs +++ b/src/WireMock.Net/Util/PortUtils.cs @@ -9,7 +9,7 @@ namespace WireMock.Util /// public static class PortUtils { - private static readonly Regex UrlDetailsRegex = new Regex(@"^(?\w+)://[^/]+?(?\d+)/?", RegexOptions.Compiled); + private static readonly Regex UrlDetailsRegex = new Regex(@"^((?\w+)://)(?[^/]+?):(?\d+)\/?$", RegexOptions.Compiled); /// /// Finds a free TCP port. @@ -32,17 +32,19 @@ public static int FindFreeTcpPort() } /// - /// Extract a proto and port from a URL. + /// Extract the protocol, host and port from a URL. /// - 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); } diff --git a/test/WireMock.Net.Tests/Util/PortUtilsTests.cs b/test/WireMock.Net.Tests/Util/PortUtilsTests.cs new file mode 100644 index 000000000..e7e793fab --- /dev/null +++ b/test/WireMock.Net.Tests/Util/PortUtilsTests.cs @@ -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); + } + } +} \ No newline at end of file