mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-26 18:59:02 +02:00
Fix listen on AnyIP for url 0.0.0.0 (#1165)
* fix listen to AnyIP if url is 0.0.0.0 * Add Test for listenin on AnyIP for url 0.0.0.0 * add missing using, use var, indent, remove empty line * remove assert for ipv4/v6 address list * test only if NET6_0_OR_GREATER * use same code style * add missing + * Asser. to Assert * split single test into one for IPv4 and one for IPv6 * Create IgnoreOnContinuousIntegrationFact.cs * Ignore tests if CI/CD * change to file - scoped namespace and add GITHUB_ACTIONS * use PortUtils.FindFreeTcpPort() * add and use GetIPAddressesByFamily * add using System.Net.Sockets * use #if for both unit tests and include new helper method inside
This commit is contained in:
@@ -75,8 +75,15 @@ namespace WireMock.Owin
|
|||||||
|
|
||||||
private static void Listen(KestrelServerOptions kestrelOptions, HostUrlDetails urlDetail, Action<ListenOptions> configure)
|
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.
|
// 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);
|
kestrelOptions.ListenLocalhost(urlDetail.Port, configure);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,8 @@ using System.Linq;
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
|
using System.Net.NetworkInformation;
|
||||||
|
using System.Net.Sockets;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
@@ -16,6 +18,7 @@ using NFluent;
|
|||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
using WireMock.Http;
|
using WireMock.Http;
|
||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
|
using WireMock.Net.Tests.Facts;
|
||||||
using WireMock.Net.Tests.Serialization;
|
using WireMock.Net.Tests.Serialization;
|
||||||
using WireMock.Net.Xunit;
|
using WireMock.Net.Xunit;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
@@ -196,6 +199,70 @@ public partial class WireMockServerTests
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if NET6_0_OR_GREATER
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
[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();
|
||||||
|
}
|
||||||
|
|
||||||
|
[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]
|
[Fact]
|
||||||
public async Task WireMockServer_Should_respond_a_redirect_without_body()
|
public async Task WireMockServer_Should_respond_a_redirect_without_body()
|
||||||
{
|
{
|
||||||
@@ -248,7 +315,7 @@ public partial class WireMockServerTests
|
|||||||
// Act
|
// Act
|
||||||
var response = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
|
var response = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
|
||||||
|
|
||||||
// Asser.
|
// Assert
|
||||||
response.Should().Be("x");
|
response.Should().Be("x");
|
||||||
|
|
||||||
server.Stop();
|
server.Stop();
|
||||||
@@ -274,7 +341,7 @@ public partial class WireMockServerTests
|
|||||||
await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
|
await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo").ConfigureAwait(false);
|
||||||
watch.Stop();
|
watch.Stop();
|
||||||
|
|
||||||
// Asser.
|
// Assert
|
||||||
watch.ElapsedMilliseconds.Should().BeGreaterOrEqualTo(0);
|
watch.ElapsedMilliseconds.Should().BeGreaterOrEqualTo(0);
|
||||||
|
|
||||||
server.Stop();
|
server.Stop();
|
||||||
|
|||||||
Reference in New Issue
Block a user