Skip to content

Commit dd35cea

Browse files
authored
When only Port is provided, bind to * (Fixes #1100) (#1107)
* Fix for #1100 * tst
1 parent 11b39cf commit dd35cea

File tree

3 files changed

+55
-26
lines changed

3 files changed

+55
-26
lines changed

examples/WireMock.Net.Console.Net452.Classic/MainApp.cs

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Linq;
66
using System.Net;
7+
using System.Net.Sockets;
78
using System.Threading.Tasks;
89
using Newtonsoft.Json;
910
using WireMock.Logging;
@@ -96,33 +97,61 @@ type Student {
9697

9798
private static void RunOnLocal()
9899
{
99-
try
100-
{
101-
var serverOnPrivateIPAddress192_168_1 = WireMockServer.Start(new WireMockServerSettings
102-
{
103-
Urls = new[] { "http://192.168.1.166:8102" }
104-
});
105-
System.Console.WriteLine($"{string.Join(", ", serverOnPrivateIPAddress192_168_1.Urls)}");
106-
serverOnPrivateIPAddress192_168_1.Stop();
107-
}
108-
catch (Exception e)
109-
{
110-
System.Console.WriteLine("serverOnPrivateIPAddress192: " + e);
111-
}
100+
var localIP = Dns.GetHostEntry(Dns.GetHostName()).AddressList.First(a => a.AddressFamily == AddressFamily.InterNetwork);
101+
102+
//try
103+
//{
104+
// var server = WireMockServer.Start(new WireMockServerSettings
105+
// {
106+
// Urls = new[] { $"http://{localIP}:9091" },
107+
// StartAdminInterface = true
108+
// });
109+
// System.Console.WriteLine($"1: {string.Join(", ", server.Urls)}");
110+
111+
// System.Console.WriteLine("Press any key to stop...");
112+
// System.Console.ReadKey();
113+
// server.Stop();
114+
//}
115+
//catch (Exception e)
116+
//{
117+
// System.Console.WriteLine(e);
118+
//}
112119

113120
try
114121
{
115-
var serverOnPrivateIPAddress172_19 = WireMockServer.Start(new WireMockServerSettings
122+
var server = WireMockServer.Start(new WireMockServerSettings
116123
{
117-
Urls = new[] { "https://172.19.80.1:8103" }
124+
Port = 9091,
125+
StartAdminInterface = true
118126
});
119-
System.Console.WriteLine($"{string.Join(", ", serverOnPrivateIPAddress172_19.Urls)}");
120-
serverOnPrivateIPAddress172_19.Stop();
127+
System.Console.WriteLine($"2: {string.Join(", ", server.Urls)}");
128+
129+
System.Console.WriteLine("Press any key to stop...");
130+
System.Console.ReadKey();
131+
server.Stop();
121132
}
122133
catch (Exception e)
123134
{
124-
System.Console.WriteLine("serverOnPrivateIPAddress172_19: " + e);
135+
System.Console.WriteLine(e);
125136
}
137+
138+
//try
139+
//{
140+
// var server = WireMockServer.Start(new WireMockServerSettings
141+
// {
142+
// Urls = new[] { "http://*:9091" },
143+
// StartAdminInterface = true
144+
// });
145+
// System.Console.WriteLine($"3: {string.Join(", ", server.Urls)}");
146+
147+
// System.Console.WriteLine("Press any key to stop...");
148+
// System.Console.ReadKey();
149+
// server.Stop();
150+
//}
151+
//catch (Exception e)
152+
//{
153+
// System.Console.WriteLine(e);
154+
//}
126155
}
127156

128157
public static void Run()

src/WireMock.Net/Owin/HostUrlOptions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace WireMock.Owin;
66

77
internal class HostUrlOptions
88
{
9-
private const string Localhost = "localhost";
9+
private const string Star = "*";
1010

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

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

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

3636
var httpsPort = FindFreeTcpPort(); // In this scenario, always get a free port for https.
37-
list.Add(new HostUrlDetails { IsHttps = true, IsHttp2 = UseHttp2 == true, Url = $"https://{Localhost}:{httpsPort}", Scheme = "https", Host = Localhost, Port = httpsPort });
37+
list.Add(new HostUrlDetails { IsHttps = true, IsHttp2 = UseHttp2 == true, Url = $"https://{Star}:{httpsPort}", Scheme = "https", Host = Star, Port = httpsPort });
3838
}
3939
}
4040
else

test/WireMock.Net.Tests/Owin/HostUrlOptionsTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace WireMock.Net.Tests.Owin;
1111
public class HostUrlOptionsTests
1212
{
1313
[Fact]
14-
public void GetDetails_WithNoUrlsAndHttpScheme_ShouldReturnCorrectDetails()
14+
public void GetDetails_WithHostingSchemeHttpAndPort_ShouldReturnCorrectDetails()
1515
{
1616
// Arrange
1717
var options = new HostUrlOptions
@@ -28,14 +28,14 @@ public void GetDetails_WithNoUrlsAndHttpScheme_ShouldReturnCorrectDetails()
2828
var detail = details.Single();
2929
detail.Should().Match<HostUrlDetails>(d =>
3030
d.Scheme == "http" &&
31-
d.Host == "localhost" &&
31+
d.Host == "*" &&
3232
d.Port == 8080 &&
3333
d.IsHttps == false
3434
);
3535
}
3636

3737
[Fact]
38-
public void GetDetails_WithNoUrlsAndHttpsScheme_ShouldReturnCorrectDetails()
38+
public void GetDetails_WithHostingSchemeHttpsAndPort_ShouldReturnCorrectDetails()
3939
{
4040
// Arrange
4141
var options = new HostUrlOptions
@@ -52,7 +52,7 @@ public void GetDetails_WithNoUrlsAndHttpsScheme_ShouldReturnCorrectDetails()
5252
var detail = details.Single();
5353
detail.Should().Match<HostUrlDetails>(d =>
5454
d.Scheme == "https" &&
55-
d.Host == "localhost" &&
55+
d.Host == "*" &&
5656
d.Port == 8081 &&
5757
d.IsHttps == true
5858
);

0 commit comments

Comments
 (0)