When only Port is provided, bind to * (Fixes #1100) (#1107)

* Fix for #1100

* tst
This commit is contained in:
Stef Heyenrath
2024-05-22 16:33:35 +02:00
committed by GitHub
parent 11b39cf57c
commit dd35cea44e
3 changed files with 55 additions and 26 deletions

View File

@@ -4,6 +4,7 @@ using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using WireMock.Logging; using WireMock.Logging;
@@ -96,33 +97,61 @@ message HelloReply {
private static void RunOnLocal() private static void RunOnLocal()
{ {
try var localIP = Dns.GetHostEntry(Dns.GetHostName()).AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork);
{
var serverOnPrivateIPAddress192_168_1 = WireMockServer.Start(new WireMockServerSettings //try
{ //{
Urls = new[] { "http://192.168.1.166:8102" } // var server = WireMockServer.Start(new WireMockServerSettings
}); // {
System.Console.WriteLine($"{string.Join(", ", serverOnPrivateIPAddress192_168_1.Urls)}"); // Urls = new[] { $"http://{localIP}:9091" },
serverOnPrivateIPAddress192_168_1.Stop(); // StartAdminInterface = true
} // });
catch (Exception e) // System.Console.WriteLine($"1: {string.Join(", ", server.Urls)}");
{
System.Console.WriteLine("serverOnPrivateIPAddress192: " + e); // System.Console.WriteLine("Press any key to stop...");
} // System.Console.ReadKey();
// server.Stop();
//}
//catch (Exception e)
//{
// System.Console.WriteLine(e);
//}
try try
{ {
var serverOnPrivateIPAddress172_19 = WireMockServer.Start(new WireMockServerSettings var server = WireMockServer.Start(new WireMockServerSettings
{ {
Urls = new[] { "https://172.19.80.1:8103" } Port = 9091,
StartAdminInterface = true
}); });
System.Console.WriteLine($"{string.Join(", ", serverOnPrivateIPAddress172_19.Urls)}"); System.Console.WriteLine($"2: {string.Join(", ", server.Urls)}");
serverOnPrivateIPAddress172_19.Stop();
System.Console.WriteLine("Press any key to stop...");
System.Console.ReadKey();
server.Stop();
} }
catch (Exception e) catch (Exception e)
{ {
System.Console.WriteLine("serverOnPrivateIPAddress172_19: " + e); System.Console.WriteLine(e);
} }
//try
//{
// var server = WireMockServer.Start(new WireMockServerSettings
// {
// Urls = new[] { "http://*:9091" },
// StartAdminInterface = true
// });
// System.Console.WriteLine($"3: {string.Join(", ", server.Urls)}");
// System.Console.WriteLine("Press any key to stop...");
// System.Console.ReadKey();
// server.Stop();
//}
//catch (Exception e)
//{
// System.Console.WriteLine(e);
//}
} }
public static void Run() public static void Run()

View File

@@ -6,7 +6,7 @@ namespace WireMock.Owin;
internal class HostUrlOptions internal class HostUrlOptions
{ {
private const string Localhost = "localhost"; private const string Star = "*";
public ICollection<string>? Urls { get; set; } public ICollection<string>? Urls { get; set; }
@@ -25,16 +25,16 @@ internal class HostUrlOptions
{ {
var port = Port > 0 ? Port.Value : FindFreeTcpPort(); var port = Port > 0 ? Port.Value : FindFreeTcpPort();
var scheme = HostingScheme == HostingScheme.Https ? "https" : "http"; var scheme = HostingScheme == HostingScheme.Https ? "https" : "http";
list.Add(new HostUrlDetails { IsHttps = HostingScheme == HostingScheme.Https, IsHttp2 = UseHttp2 == true, Url = $"{scheme}://{Localhost}:{port}", Scheme = scheme, Host = Localhost, Port = port }); list.Add(new HostUrlDetails { IsHttps = HostingScheme == HostingScheme.Https, IsHttp2 = UseHttp2 == true, Url = $"{scheme}://{Star}:{port}", Scheme = scheme, Host = Star, Port = port });
} }
if (HostingScheme == HostingScheme.HttpAndHttps) if (HostingScheme == HostingScheme.HttpAndHttps)
{ {
var httpPort = Port > 0 ? Port.Value : FindFreeTcpPort(); var httpPort = Port > 0 ? Port.Value : FindFreeTcpPort();
list.Add(new HostUrlDetails { IsHttps = false, IsHttp2 = UseHttp2 == true, Url = $"http://{Localhost}:{httpPort}", Scheme = "http", Host = Localhost, Port = httpPort }); list.Add(new HostUrlDetails { IsHttps = false, IsHttp2 = UseHttp2 == true, Url = $"http://{Star}:{httpPort}", Scheme = "http", Host = Star, Port = httpPort });
var httpsPort = FindFreeTcpPort(); // In this scenario, always get a free port for https. var httpsPort = FindFreeTcpPort(); // In this scenario, always get a free port for https.
list.Add(new HostUrlDetails { IsHttps = true, IsHttp2 = UseHttp2 == true, Url = $"https://{Localhost}:{httpsPort}", Scheme = "https", Host = Localhost, Port = httpsPort }); list.Add(new HostUrlDetails { IsHttps = true, IsHttp2 = UseHttp2 == true, Url = $"https://{Star}:{httpsPort}", Scheme = "https", Host = Star, Port = httpsPort });
} }
} }
else else

View File

@@ -11,7 +11,7 @@ namespace WireMock.Net.Tests.Owin;
public class HostUrlOptionsTests public class HostUrlOptionsTests
{ {
[Fact] [Fact]
public void GetDetails_WithNoUrlsAndHttpScheme_ShouldReturnCorrectDetails() public void GetDetails_WithHostingSchemeHttpAndPort_ShouldReturnCorrectDetails()
{ {
// Arrange // Arrange
var options = new HostUrlOptions var options = new HostUrlOptions
@@ -28,14 +28,14 @@ public class HostUrlOptionsTests
var detail = details.Single(); var detail = details.Single();
detail.Should().Match<HostUrlDetails>(d => detail.Should().Match<HostUrlDetails>(d =>
d.Scheme == "http" && d.Scheme == "http" &&
d.Host == "localhost" && d.Host == "*" &&
d.Port == 8080 && d.Port == 8080 &&
d.IsHttps == false d.IsHttps == false
); );
} }
[Fact] [Fact]
public void GetDetails_WithNoUrlsAndHttpsScheme_ShouldReturnCorrectDetails() public void GetDetails_WithHostingSchemeHttpsAndPort_ShouldReturnCorrectDetails()
{ {
// Arrange // Arrange
var options = new HostUrlOptions var options = new HostUrlOptions
@@ -52,7 +52,7 @@ public class HostUrlOptionsTests
var detail = details.Single(); var detail = details.Single();
detail.Should().Match<HostUrlDetails>(d => detail.Should().Match<HostUrlDetails>(d =>
d.Scheme == "https" && d.Scheme == "https" &&
d.Host == "localhost" && d.Host == "*" &&
d.Port == 8081 && d.Port == 8081 &&
d.IsHttps == true d.IsHttps == true
); );