Skip to content

Commit

Permalink
add and use GetIPAddressesByFamily
Browse files Browse the repository at this point in the history
  • Loading branch information
cocoon authored Sep 6, 2024
1 parent a3671a5 commit 88b131c
Showing 1 changed file with 13 additions and 35 deletions.
48 changes: 13 additions & 35 deletions test/WireMock.Net.Tests/WireMockServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ public WireMockServerTests(ITestOutputHelper testOutputHelper)
_testOutputHelper = testOutputHelper;
}

private static string[] GetIPAddressesByFamily(AddressFamily addressFamily)

Check failure on line 43 in test/WireMock.Net.Tests/WireMockServerTests.cs

View check run for this annotation

Azure Pipelines / CI (Linux_Build_Test_SonarCloud)

test/WireMock.Net.Tests/WireMockServerTests.cs#L43

test/WireMock.Net.Tests/WireMockServerTests.cs(43,52): Error CS0246: The type or namespace name 'AddressFamily' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 43 in test/WireMock.Net.Tests/WireMockServerTests.cs

View check run for this annotation

Azure Pipelines / CI

test/WireMock.Net.Tests/WireMockServerTests.cs#L43

test/WireMock.Net.Tests/WireMockServerTests.cs(43,52): Error CS0246: The type or namespace name 'AddressFamily' could not be found (are you missing a using directive or an assembly reference?)
{
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 @@ -204,23 +214,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv4()
{
// Arrange
var port = PortUtils.FindFreeTcpPort();
var IPv4 = new List<string>();

foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
{
if (netInterface.OperationalStatus == OperationalStatus.Up)
{
var ipProps = netInterface.GetIPProperties();
foreach (var addr in ipProps.UnicastAddresses)
{
if (addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
IPv4.Add(addr.Address.ToString());
}
}
}
}

var IPv4 = GetIPAddressesByFamily(System.Net.Sockets.AddressFamily.InterNetwork);
var settings = new WireMockServerSettings
{
Urls = new string[] { "http://0.0.0.0:" + port },
Expand Down Expand Up @@ -248,23 +242,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv6()
{
// Arrange
var port = PortUtils.FindFreeTcpPort();
var IPv6 = new List<string>();

foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
{
if (netInterface.OperationalStatus == OperationalStatus.Up)
{
var ipProps = netInterface.GetIPProperties();
foreach (var addr in ipProps.UnicastAddresses)
{
if (addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
{
IPv6.Add($"[{addr.Address.ToString()}]");
}
}
}
}

var IPv6 = GetIPAddressesByFamily(System.Net.Sockets.AddressFamily.InterNetworkV6);
var settings = new WireMockServerSettings
{
Urls = new string[] { "http://0.0.0.0:" + port },
Expand All @@ -276,7 +254,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv6()
foreach (var addr in IPv6)
{
// Act
var response = await new HttpClient().GetStringAsync("http://" + addr + ":" + server.Ports[0] + "/foo").ConfigureAwait(false);
var response = await new HttpClient().GetStringAsync("http://[" + addr + "]:" + server.Ports[0] + "/foo").ConfigureAwait(false);

// Assert
response.Should().Be("x");
Expand Down

0 comments on commit 88b131c

Please sign in to comment.