Skip to content

Commit

Permalink
When only Port is provided, bind to * (Fixes #1100) (#1107)
Browse files Browse the repository at this point in the history
* Fix for #1100

* tst
  • Loading branch information
StefH committed May 22, 2024
1 parent 11b39cf commit dd35cea
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
65 changes: 47 additions & 18 deletions examples/WireMock.Net.Console.Net452.Classic/MainApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using Newtonsoft.Json;
using WireMock.Logging;
Expand Down Expand Up @@ -96,33 +97,61 @@ scalar MyCustomScalar

private static void RunOnLocal()
{
try
{
var serverOnPrivateIPAddress192_168_1 = WireMockServer.Start(new WireMockServerSettings
{
Urls = new[] { "http:https://192.168.1.166:8102" }
});
System.Console.WriteLine($"{string.Join(", ", serverOnPrivateIPAddress192_168_1.Urls)}");
serverOnPrivateIPAddress192_168_1.Stop();
}
catch (Exception e)
{
System.Console.WriteLine("serverOnPrivateIPAddress192: " + e);
}
var localIP = Dns.GetHostEntry(Dns.GetHostName()).AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork);

//try
//{
// var server = WireMockServer.Start(new WireMockServerSettings
// {
// Urls = new[] { $"http:https://{localIP}:9091" },
// StartAdminInterface = true
// });
// System.Console.WriteLine($"1: {string.Join(", ", server.Urls)}");

// System.Console.WriteLine("Press any key to stop...");
// System.Console.ReadKey();
// server.Stop();
//}
//catch (Exception e)
//{
// System.Console.WriteLine(e);
//}

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)}");
serverOnPrivateIPAddress172_19.Stop();
System.Console.WriteLine($"2: {string.Join(", ", server.Urls)}");

System.Console.WriteLine("Press any key to stop...");
System.Console.ReadKey();
server.Stop();
}
catch (Exception e)
{
System.Console.WriteLine("serverOnPrivateIPAddress172_19: " + e);
System.Console.WriteLine(e);
}

//try
//{
// var server = WireMockServer.Start(new WireMockServerSettings
// {
// Urls = new[] { "http:https://*: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()
Expand Down
8 changes: 4 additions & 4 deletions src/WireMock.Net/Owin/HostUrlOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace WireMock.Owin;

internal class HostUrlOptions
{
private const string Localhost = "localhost";
private const string Star = "*";

public ICollection<string>? Urls { get; set; }

Expand All @@ -25,16 +25,16 @@ public IReadOnlyList<HostUrlDetails> GetDetails()
{
var port = Port > 0 ? Port.Value : FindFreeTcpPort();
var scheme = HostingScheme == HostingScheme.Https ? "https" : "http";
list.Add(new HostUrlDetails { IsHttps = HostingScheme == HostingScheme.Https, IsHttp2 = UseHttp2 == true, Url = $"{scheme}:https://{Localhost}:{port}", Scheme = scheme, Host = Localhost, Port = port });
list.Add(new HostUrlDetails { IsHttps = HostingScheme == HostingScheme.Https, IsHttp2 = UseHttp2 == true, Url = $"{scheme}:https://{Star}:{port}", Scheme = scheme, Host = Star, Port = port });
}

if (HostingScheme == HostingScheme.HttpAndHttps)
{
var httpPort = Port > 0 ? Port.Value : FindFreeTcpPort();
list.Add(new HostUrlDetails { IsHttps = false, IsHttp2 = UseHttp2 == true, Url = $"http:https://{Localhost}:{httpPort}", Scheme = "http", Host = Localhost, Port = httpPort });
list.Add(new HostUrlDetails { IsHttps = false, IsHttp2 = UseHttp2 == true, Url = $"http:https://{Star}:{httpPort}", Scheme = "http", Host = Star, Port = httpPort });

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
Expand Down
8 changes: 4 additions & 4 deletions test/WireMock.Net.Tests/Owin/HostUrlOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace WireMock.Net.Tests.Owin;
public class HostUrlOptionsTests
{
[Fact]
public void GetDetails_WithNoUrlsAndHttpScheme_ShouldReturnCorrectDetails()
public void GetDetails_WithHostingSchemeHttpAndPort_ShouldReturnCorrectDetails()
{
// Arrange
var options = new HostUrlOptions
Expand All @@ -28,14 +28,14 @@ public void GetDetails_WithNoUrlsAndHttpScheme_ShouldReturnCorrectDetails()
var detail = details.Single();
detail.Should().Match<HostUrlDetails>(d =>
d.Scheme == "http" &&
d.Host == "localhost" &&
d.Host == "*" &&
d.Port == 8080 &&
d.IsHttps == false
);
}

[Fact]
public void GetDetails_WithNoUrlsAndHttpsScheme_ShouldReturnCorrectDetails()
public void GetDetails_WithHostingSchemeHttpsAndPort_ShouldReturnCorrectDetails()
{
// Arrange
var options = new HostUrlOptions
Expand All @@ -52,7 +52,7 @@ public void GetDetails_WithNoUrlsAndHttpsScheme_ShouldReturnCorrectDetails()
var detail = details.Single();
detail.Should().Match<HostUrlDetails>(d =>
d.Scheme == "https" &&
d.Host == "localhost" &&
d.Host == "*" &&
d.Port == 8081 &&
d.IsHttps == true
);
Expand Down

0 comments on commit dd35cea

Please sign in to comment.