Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix listen on AnyIP for url 0.0.0.0 #1165

Merged
merged 16 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/WireMock.Net/Owin/AspNetCoreSelfHost.NETStandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,15 @@ private static void SetHttpsAndUrls(KestrelServerOptions kestrelOptions, IWireMo

private static void Listen(KestrelServerOptions kestrelOptions, HostUrlDetails urlDetail, Action<ListenOptions> configure)
{
// Listens on any IP with the given port.
if (urlDetail is { Port: > 0, Host: "0.0.0.0" })
{
kestrelOptions.ListenAnyIP(urlDetail.Port, configure);
return;
}

// Listens on ::1 and 127.0.0.1 with the given port.
if (urlDetail is { Port: > 0, Host: "localhost" or "127.0.0.1" or "0.0.0.0" or "::1" })
if (urlDetail is { Port: > 0, Host: "localhost" or "127.0.0.1" or "::1" })
{
kestrelOptions.ListenLocalhost(urlDetail.Port, configure);
return;
Expand Down Expand Up @@ -113,4 +120,4 @@ internal static IWebHostBuilder ConfigureKestrelServerOptions(this IWebHostBuild
}
}
}
#endif
#endif
22 changes: 22 additions & 0 deletions test/WireMock.Net.Tests/Facts/IgnoreOnContinuousIntegrationFact.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright © WireMock.Net

using System;
using Xunit;

namespace WireMock.Net.Tests.Facts;

public sealed class IgnoreOnContinuousIntegrationFact : FactAttribute
{
private static readonly string _skipReason = "Ignore when run via CI/CD";
private static readonly bool _isContinuousIntegrationAzure = bool.TryParse(Environment.GetEnvironmentVariable("TF_BUILD"), out var isTF) && isTF;
private static readonly bool _isContinuousIntegrationGithub = bool.TryParse(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"), out var isGH) && isGH;
private static bool IsContinuousIntegration() => _isContinuousIntegrationAzure || _isContinuousIntegrationGithub;

public IgnoreOnContinuousIntegrationFact()
{
if (IsContinuousIntegration())
{
Skip = _skipReason;
}
}
}
75 changes: 72 additions & 3 deletions test/WireMock.Net.Tests/WireMockServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
Expand All @@ -16,6 +18,7 @@
using WireMock.Admin.Mappings;
using WireMock.Http;
using WireMock.Matchers;
using WireMock.Net.Tests.Facts;
using WireMock.Net.Tests.Serialization;
using WireMock.Net.Xunit;
using WireMock.RequestBuilders;
Expand All @@ -38,6 +41,16 @@ public WireMockServerTests(ITestOutputHelper testOutputHelper)
_testOutputHelper = testOutputHelper;
}

private static string[] GetIPAddressesByFamily(AddressFamily addressFamily)
{
return NetworkInterface.GetAllNetworkInterfaces()
.Where(ni => ni.OperationalStatus == OperationalStatus.Up)
.SelectMany(ni => ni.GetIPProperties().UnicastAddresses)
.Where(addr => addr.Address.AddressFamily == addressFamily)
.Select(addr => addr.Address.ToString())
.ToArray();
}

[Fact]
public void WireMockServer_Start()
{
Expand Down Expand Up @@ -196,6 +209,62 @@ public async Task WireMockServer_When_HttpClientWithWebProxyCallsHttp_Should_Wor
}
#endif

#if NET6_0_OR_GREATER
[IgnoreOnContinuousIntegrationFact]
public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv4()
{
// Arrange
var port = PortUtils.FindFreeTcpPort();
var IPv4 = GetIPAddressesByFamily(System.Net.Sockets.AddressFamily.InterNetwork);
var settings = new WireMockServerSettings
{
Urls = new string[] { "http://0.0.0.0:" + port },
};
var server = WireMockServer.Start(settings);

server.Given(Request.Create().WithPath("/*")).RespondWith(Response.Create().WithBody("x"));

foreach (var addr in IPv4)
{
// Act
var response = await new HttpClient().GetStringAsync("http://" + addr + ":" + server.Ports[0] + "/foo").ConfigureAwait(false);

// Assert
response.Should().Be("x");
}

server.Stop();
}
#endif

#if NET6_0_OR_GREATER
[IgnoreOnContinuousIntegrationFact]
public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv6()
{
// Arrange
var port = PortUtils.FindFreeTcpPort();
var IPv6 = GetIPAddressesByFamily(System.Net.Sockets.AddressFamily.InterNetworkV6);
var settings = new WireMockServerSettings
{
Urls = new string[] { "http://0.0.0.0:" + port },
};
var server = WireMockServer.Start(settings);

server.Given(Request.Create().WithPath("/*")).RespondWith(Response.Create().WithBody("x"));

foreach (var addr in IPv6)
{
// Act
var response = await new HttpClient().GetStringAsync("http://[" + addr + "]:" + server.Ports[0] + "/foo").ConfigureAwait(false);

// Assert
response.Should().Be("x");
}

server.Stop();
}
#endif

[Fact]
public async Task WireMockServer_Should_respond_a_redirect_without_body()
{
Expand Down Expand Up @@ -248,7 +317,7 @@ public async Task WireMockServer_WithCorsPolicyOptions_Should_Work_Correct()
// Act
var response = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);

// Asser.
// Assert
response.Should().Be("x");

server.Stop();
Expand All @@ -274,7 +343,7 @@ public async Task WireMockServer_Should_delay_responses_for_a_given_route()
await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
watch.Stop();

// Asser.
// Assert
watch.ElapsedMilliseconds.Should().BeGreaterOrEqualTo(0);

server.Stop();
Expand Down Expand Up @@ -662,4 +731,4 @@ public async Task WireMockServer_Using_JsonMapping_And_CustomMatcher_WithIncorre
server.Stop();
}
#endif
}
}