From ae82432be11b09ba3dcf7097b9eb26ed999c34fc Mon Sep 17 00:00:00 2001 From: cocoon Date: Wed, 4 Sep 2024 11:02:43 +0200 Subject: [PATCH 01/16] fix listen to AnyIP if url is 0.0.0.0 --- .../Owin/AspNetCoreSelfHost.NETStandard.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/WireMock.Net/Owin/AspNetCoreSelfHost.NETStandard.cs b/src/WireMock.Net/Owin/AspNetCoreSelfHost.NETStandard.cs index 26536ece..0cbbed20 100644 --- a/src/WireMock.Net/Owin/AspNetCoreSelfHost.NETStandard.cs +++ b/src/WireMock.Net/Owin/AspNetCoreSelfHost.NETStandard.cs @@ -75,8 +75,15 @@ private static void SetHttpsAndUrls(KestrelServerOptions kestrelOptions, IWireMo private static void Listen(KestrelServerOptions kestrelOptions, HostUrlDetails urlDetail, Action 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; @@ -113,4 +120,4 @@ internal static IWebHostBuilder ConfigureKestrelServerOptions(this IWebHostBuild } } } -#endif \ No newline at end of file +#endif From e4f587db9dd81562a754b4e6759d6b6d12761c7a Mon Sep 17 00:00:00 2001 From: cocoon Date: Wed, 4 Sep 2024 11:04:27 +0200 Subject: [PATCH 02/16] Add Test for listenin on AnyIP for url 0.0.0.0 --- .../WireMock.Net.Tests/WireMockServerTests.cs | 68 ++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index c53a7aa3..16cb1cfa 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -196,6 +196,72 @@ public async Task WireMockServer_When_HttpClientWithWebProxyCallsHttp_Should_Wor } #endif +[Fact] +public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs() +{ + // Arrange + + List IPv4 = new List(); + List IPv6 = new List(); + + foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces()) + { + if (netInterface.OperationalStatus == OperationalStatus.Up) + { + + IPInterfaceProperties ipProps = netInterface.GetIPProperties(); + + foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses) + { + Console.WriteLine(" " + addr.Address.ToString()); + + if (addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) + { + IPv4.Add(addr.Address.ToString()); + } + else if (addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) + { + IPv6.Add($"[{addr.Address.ToString()}]"); + } + } + } + } + + // Asser. + IPv4.Should().NotBeEmpty(); + IPv6.Should().NotBeEmpty(); + + + var settings = new WireMockServerSettings + { + Urls = new string[] { "http://0.0.0.0:80" }, + }; + 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); + + // Asser. + response.Should().Be("x"); + } + + foreach (var addr in IPv6) + { + // Act + var response = await new HttpClient().GetStringAsync($"http://{addr}:" + server.Ports[0] + "/foo").ConfigureAwait(false); + + // Asser. + response.Should().Be("x"); + } + + server.Stop(); +} + + [Fact] public async Task WireMockServer_Should_respond_a_redirect_without_body() { @@ -662,4 +728,4 @@ public async Task WireMockServer_Using_JsonMapping_And_CustomMatcher_WithIncorre server.Stop(); } #endif -} \ No newline at end of file +} From 3ce97e06e0e1a5faa71964e6814c2f8fba6e507a Mon Sep 17 00:00:00 2001 From: cocoon Date: Wed, 4 Sep 2024 13:55:48 +0200 Subject: [PATCH 03/16] add missing using, use var, indent, remove empty line --- .../WireMock.Net.Tests/WireMockServerTests.cs | 105 +++++++++--------- 1 file changed, 50 insertions(+), 55 deletions(-) diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index 16cb1cfa..3ee747fe 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -8,6 +8,7 @@ using System.Net; using System.Net.Http; using System.Net.Http.Headers; +using System.Net.NetworkInformation; using System.Text; using System.Threading.Tasks; using FluentAssertions; @@ -196,71 +197,65 @@ public async Task WireMockServer_When_HttpClientWithWebProxyCallsHttp_Should_Wor } #endif -[Fact] -public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs() -{ - // Arrange - - List IPv4 = new List(); - List IPv6 = new List(); - - foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces()) + [Fact] + public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs() { - if (netInterface.OperationalStatus == OperationalStatus.Up) + // Arrange + var IPv4 = new List(); + var IPv6 = new List(); + + foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces()) { - - IPInterfaceProperties ipProps = netInterface.GetIPProperties(); - - foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses) + if (netInterface.OperationalStatus == OperationalStatus.Up) { - Console.WriteLine(" " + addr.Address.ToString()); - - if (addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) - { - IPv4.Add(addr.Address.ToString()); - } - else if (addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) + var ipProps = netInterface.GetIPProperties(); + foreach (var addr in ipProps.UnicastAddresses) { - IPv6.Add($"[{addr.Address.ToString()}]"); + if (addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) + { + IPv4.Add(addr.Address.ToString()); + } + else if (addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) + { + IPv6.Add($"[{addr.Address.ToString()}]"); + } } } } - } - - // Asser. - IPv4.Should().NotBeEmpty(); - IPv6.Should().NotBeEmpty(); - - - var settings = new WireMockServerSettings - { - Urls = new string[] { "http://0.0.0.0:80" }, - }; - 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); - - // Asser. - response.Should().Be("x"); - } - - foreach (var addr in IPv6) - { - // Act - var response = await new HttpClient().GetStringAsync($"http://{addr}:" + server.Ports[0] + "/foo").ConfigureAwait(false); - + // Asser. - response.Should().Be("x"); + IPv4.Should().NotBeEmpty(); + IPv6.Should().NotBeEmpty(); + + var settings = new WireMockServerSettings + { + Urls = new string[] { "http://0.0.0.0:80" }, + }; + 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); + + // Asser. + response.Should().Be("x"); + } + + foreach (var addr in IPv6) + { + // Act + var response = await new HttpClient().GetStringAsync($"http://{addr}:" + server.Ports[0] + "/foo").ConfigureAwait(false); + + // Asser. + response.Should().Be("x"); + } + + server.Stop(); } - server.Stop(); -} - [Fact] public async Task WireMockServer_Should_respond_a_redirect_without_body() From 38ac9bfd1ca63eeacef282b5dd1e47e3ae06b85f Mon Sep 17 00:00:00 2001 From: cocoon Date: Wed, 4 Sep 2024 14:04:10 +0200 Subject: [PATCH 04/16] remove assert for ipv4/v6 address list --- test/WireMock.Net.Tests/WireMockServerTests.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index 3ee747fe..20a4aab6 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -222,11 +222,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs() } } } - - // Asser. - IPv4.Should().NotBeEmpty(); - IPv6.Should().NotBeEmpty(); - + var settings = new WireMockServerSettings { Urls = new string[] { "http://0.0.0.0:80" }, From 4a8430f0e2afb3abc2034b12d6c50a1fc77a2ec4 Mon Sep 17 00:00:00 2001 From: cocoon Date: Wed, 4 Sep 2024 14:21:04 +0200 Subject: [PATCH 05/16] test only if NET6_0_OR_GREATER --- test/WireMock.Net.Tests/WireMockServerTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index 20a4aab6..3d076f51 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -197,6 +197,7 @@ public async Task WireMockServer_When_HttpClientWithWebProxyCallsHttp_Should_Wor } #endif +#if NET6_0_OR_GREATER [Fact] public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs() { @@ -251,7 +252,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs() server.Stop(); } - +#endif [Fact] public async Task WireMockServer_Should_respond_a_redirect_without_body() From b27f0a5720c280206b084c93f73214f58ea549ad Mon Sep 17 00:00:00 2001 From: cocoon Date: Wed, 4 Sep 2024 14:30:36 +0200 Subject: [PATCH 06/16] use same code style --- test/WireMock.Net.Tests/WireMockServerTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index 3d076f51..7ca1b741 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -235,7 +235,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs() foreach (var addr in IPv4) { // 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); // Asser. response.Should().Be("x"); @@ -244,7 +244,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs() 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); // Asser. response.Should().Be("x"); From 3a2d68a2878c0f9cb909c487b0d6196c2180f537 Mon Sep 17 00:00:00 2001 From: cocoon Date: Wed, 4 Sep 2024 14:33:13 +0200 Subject: [PATCH 07/16] add missing + --- test/WireMock.Net.Tests/WireMockServerTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index 7ca1b741..ea1632c6 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -244,7 +244,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs() 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); // Asser. response.Should().Be("x"); From 97a3d9d44b23643a4063f679ea48a0815e51bf52 Mon Sep 17 00:00:00 2001 From: cocoon Date: Thu, 5 Sep 2024 16:31:10 +0200 Subject: [PATCH 08/16] Asser. to Assert --- test/WireMock.Net.Tests/WireMockServerTests.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index ea1632c6..1dab5962 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -237,7 +237,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs() // Act var response = await new HttpClient().GetStringAsync("http://" + addr + ":" + server.Ports[0] + "/foo").ConfigureAwait(false); - // Asser. + // Assert response.Should().Be("x"); } @@ -246,7 +246,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs() // Act var response = await new HttpClient().GetStringAsync("http://" + addr + ":" + server.Ports[0] + "/foo").ConfigureAwait(false); - // Asser. + // Assert response.Should().Be("x"); } @@ -306,7 +306,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(); @@ -332,7 +332,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(); From 6ccc15b5df7b83289178c681845fd1b5e9c0165d Mon Sep 17 00:00:00 2001 From: cocoon Date: Thu, 5 Sep 2024 16:44:16 +0200 Subject: [PATCH 09/16] split single test into one for IPv4 and one for IPv6 --- .../WireMock.Net.Tests/WireMockServerTests.cs | 43 ++++++++++++++++--- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index 1dab5962..2769a17a 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -199,12 +199,11 @@ public async Task WireMockServer_When_HttpClientWithWebProxyCallsHttp_Should_Wor #if NET6_0_OR_GREATER [Fact] - public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs() + public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv4() { // Arrange var IPv4 = new List(); - var IPv6 = new List(); - + foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces()) { if (netInterface.OperationalStatus == OperationalStatus.Up) @@ -216,10 +215,6 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs() { IPv4.Add(addr.Address.ToString()); } - else if (addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) - { - IPv6.Add($"[{addr.Address.ToString()}]"); - } } } } @@ -240,7 +235,41 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs() // Assert response.Should().Be("x"); } + + server.Stop(); + } +#endif + +#if NET6_0_OR_GREATER + [Fact] + public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv6() + { + // Arrange + var IPv6 = new List(); + + 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 settings = new WireMockServerSettings + { + Urls = new string[] { "http://0.0.0.0:80" }, + }; + var server = WireMockServer.Start(settings); + server.Given(Request.Create().WithPath("/*")).RespondWith(Response.Create().WithBody("x")); + foreach (var addr in IPv6) { // Act From 46aca87477885ec2194ebce201d8a00143871d74 Mon Sep 17 00:00:00 2001 From: cocoon Date: Thu, 5 Sep 2024 17:31:40 +0200 Subject: [PATCH 10/16] Create IgnoreOnContinuousIntegrationFact.cs --- .../IgnoreOnContinuousIntegrationFact.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 test/WireMock.Net.Tests/Facts/IgnoreOnContinuousIntegrationFact.cs diff --git a/test/WireMock.Net.Tests/Facts/IgnoreOnContinuousIntegrationFact.cs b/test/WireMock.Net.Tests/Facts/IgnoreOnContinuousIntegrationFact.cs new file mode 100644 index 00000000..27634609 --- /dev/null +++ b/test/WireMock.Net.Tests/Facts/IgnoreOnContinuousIntegrationFact.cs @@ -0,0 +1,19 @@ +// Copyright © WireMock.Net +using System; +using Xunit; + +namespace WireMock.Net.Tests.Facts +{ + public sealed class IgnoreOnContinuousIntegrationFact : FactAttribute + { + public IgnoreOnContinuousIntegrationFact() + { + if (IsContinuousIntegration()) + { + Skip = "Ignore when run via CI/CD"; + } + } + + private static bool IsContinuousIntegration() => Environment.GetEnvironmentVariable("TF_BUILD") != null && Environment.GetEnvironmentVariable("TF_BUILD").ToLower() == "true"; + } +} From 6075b5c9042dda42b896781aba322653b9bb725b Mon Sep 17 00:00:00 2001 From: cocoon Date: Thu, 5 Sep 2024 17:32:57 +0200 Subject: [PATCH 11/16] Ignore tests if CI/CD --- test/WireMock.Net.Tests/WireMockServerTests.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index 2769a17a..d3b4af59 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -17,6 +17,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; @@ -198,7 +199,7 @@ public async Task WireMockServer_When_HttpClientWithWebProxyCallsHttp_Should_Wor #endif #if NET6_0_OR_GREATER - [Fact] + [IgnoreOnContinuousIntegrationFact] public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv4() { // Arrange @@ -241,7 +242,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv4() #endif #if NET6_0_OR_GREATER - [Fact] + [IgnoreOnContinuousIntegrationFact] public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv6() { // Arrange From 6e2d5e9ad468baf335b707c60efc511b01b6329e Mon Sep 17 00:00:00 2001 From: cocoon Date: Fri, 6 Sep 2024 09:23:29 +0200 Subject: [PATCH 12/16] change to file - scoped namespace and add GITHUB_ACTIONS --- .../IgnoreOnContinuousIntegrationFact.cs | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/test/WireMock.Net.Tests/Facts/IgnoreOnContinuousIntegrationFact.cs b/test/WireMock.Net.Tests/Facts/IgnoreOnContinuousIntegrationFact.cs index 27634609..d1254d94 100644 --- a/test/WireMock.Net.Tests/Facts/IgnoreOnContinuousIntegrationFact.cs +++ b/test/WireMock.Net.Tests/Facts/IgnoreOnContinuousIntegrationFact.cs @@ -1,19 +1,22 @@ // Copyright © WireMock.Net + using System; using Xunit; -namespace WireMock.Net.Tests.Facts +namespace WireMock.Net.Tests.Facts; + +public sealed class IgnoreOnContinuousIntegrationFact : FactAttribute { - 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() { - public IgnoreOnContinuousIntegrationFact() + if (IsContinuousIntegration()) { - if (IsContinuousIntegration()) - { - Skip = "Ignore when run via CI/CD"; - } + Skip = _skipReason; } - - private static bool IsContinuousIntegration() => Environment.GetEnvironmentVariable("TF_BUILD") != null && Environment.GetEnvironmentVariable("TF_BUILD").ToLower() == "true"; } } From a3671a5fb8e18f6485a0c50be6b15a5e953cf35a Mon Sep 17 00:00:00 2001 From: cocoon Date: Fri, 6 Sep 2024 09:28:21 +0200 Subject: [PATCH 13/16] use PortUtils.FindFreeTcpPort() --- test/WireMock.Net.Tests/WireMockServerTests.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index d3b4af59..5f3759ef 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -203,6 +203,7 @@ public async Task WireMockServer_When_HttpClientWithWebProxyCallsHttp_Should_Wor public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv4() { // Arrange + var port = PortUtils.FindFreeTcpPort(); var IPv4 = new List(); foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces()) @@ -222,7 +223,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv4() var settings = new WireMockServerSettings { - Urls = new string[] { "http://0.0.0.0:80" }, + Urls = new string[] { "http://0.0.0.0:" + port }, }; var server = WireMockServer.Start(settings); @@ -246,6 +247,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv4() public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv6() { // Arrange + var port = PortUtils.FindFreeTcpPort(); var IPv6 = new List(); foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces()) @@ -265,7 +267,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv6() var settings = new WireMockServerSettings { - Urls = new string[] { "http://0.0.0.0:80" }, + Urls = new string[] { "http://0.0.0.0:" + port }, }; var server = WireMockServer.Start(settings); From 88b131cd20a79635c294379c5495c707823ae4f0 Mon Sep 17 00:00:00 2001 From: cocoon Date: Fri, 6 Sep 2024 09:42:42 +0200 Subject: [PATCH 14/16] add and use GetIPAddressesByFamily --- .../WireMock.Net.Tests/WireMockServerTests.cs | 48 +++++-------------- 1 file changed, 13 insertions(+), 35 deletions(-) diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index 5f3759ef..f90aa166 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -40,6 +40,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() { @@ -204,23 +214,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv4() { // Arrange var port = PortUtils.FindFreeTcpPort(); - var IPv4 = new List(); - - 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 }, @@ -248,23 +242,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv6() { // Arrange var port = PortUtils.FindFreeTcpPort(); - var IPv6 = new List(); - - 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 }, @@ -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"); From 45ad87cc89d8609d26ace79bd4b0ad8e219aefc5 Mon Sep 17 00:00:00 2001 From: cocoon Date: Fri, 6 Sep 2024 09:49:07 +0200 Subject: [PATCH 15/16] add using System.Net.Sockets --- test/WireMock.Net.Tests/WireMockServerTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index f90aa166..2e0f045a 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -9,6 +9,7 @@ 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; From 4a076b54fff2c58d2e98abe6dc70469d76ad128a Mon Sep 17 00:00:00 2001 From: cocoon Date: Fri, 6 Sep 2024 17:54:48 +0200 Subject: [PATCH 16/16] use #if for both unit tests and include new helper method inside --- .../WireMock.Net.Tests/WireMockServerTests.cs | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index 2e0f045a..dfe24c63 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -40,16 +40,6 @@ 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() @@ -210,6 +200,16 @@ public async Task WireMockServer_When_HttpClientWithWebProxyCallsHttp_Should_Wor #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() { @@ -235,9 +235,7 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv4() server.Stop(); } -#endif -#if NET6_0_OR_GREATER [IgnoreOnContinuousIntegrationFact] public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv6() {