diff --git a/test/WireMock.Net.Aspire.Tests/DockerUtils.cs b/test/WireMock.Net.Aspire.Tests/DockerUtils.cs index fa7b6378..2528dbcd 100644 --- a/test/WireMock.Net.Aspire.Tests/DockerUtils.cs +++ b/test/WireMock.Net.Aspire.Tests/DockerUtils.cs @@ -8,10 +8,7 @@ namespace WireMock.Net.Aspire.Tests; [ExcludeFromCodeCoverage] internal static class DockerUtils { - public static bool IsDockerRunningLinuxContainerMode() - { - return IsDockerRunning() && IsLinuxContainerMode(); - } + public static Lazy IsDockerRunningLinuxContainerMode => new(() => IsDockerRunning() && IsLinuxContainerMode()); private static bool IsDockerRunning() { diff --git a/test/WireMock.Net.Aspire.Tests/Facts/DockerIsRunningInLinuxContainerModeFact.cs b/test/WireMock.Net.Aspire.Tests/Facts/DockerIsRunningInLinuxContainerModeFact.cs new file mode 100644 index 00000000..7be0668d --- /dev/null +++ b/test/WireMock.Net.Aspire.Tests/Facts/DockerIsRunningInLinuxContainerModeFact.cs @@ -0,0 +1,16 @@ +// Copyright © WireMock.Net + +namespace WireMock.Net.Aspire.Tests.Facts; + +public sealed class DockerIsRunningInLinuxContainerModeFact : FactAttribute +{ + private const string SkipReason = "Docker is not running in Linux container mode. Skipping test."; + + public DockerIsRunningInLinuxContainerModeFact() + { + if (!DockerUtils.IsDockerRunningLinuxContainerMode.Value) + { + Skip = SkipReason; + } + } +} \ No newline at end of file diff --git a/test/WireMock.Net.Aspire.Tests/IntegrationTests.cs b/test/WireMock.Net.Aspire.Tests/IntegrationTests.cs index 8f78430b..e51c2d9b 100644 --- a/test/WireMock.Net.Aspire.Tests/IntegrationTests.cs +++ b/test/WireMock.Net.Aspire.Tests/IntegrationTests.cs @@ -3,6 +3,7 @@ using System.Net.Http.Json; using FluentAssertions; using Projects; +using WireMock.Net.Aspire.Tests.Facts; using Xunit.Abstractions; namespace WireMock.Net.Aspire.Tests; @@ -11,15 +12,9 @@ public class IntegrationTests(ITestOutputHelper output) { private record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary); - [Fact] + [DockerIsRunningInLinuxContainerModeFact] public async Task StartAppHostWithWireMockAndCreateHttpClientToCallTheMockedWeatherForecastEndpoint() { - if (!DockerUtils.IsDockerRunningLinuxContainerMode()) - { - output.WriteLine("Docker is not running in Linux container mode. Skipping test."); - return; - } - // Arrange var appHostBuilder = await DistributedApplicationTestingBuilder.CreateAsync(); await using var app = await appHostBuilder.BuildAsync(); @@ -44,15 +39,9 @@ public async Task StartAppHostWithWireMockAndCreateHttpClientToCallTheMockedWeat weatherForecasts2.Should().HaveCount(5); } - [Fact] + [DockerIsRunningInLinuxContainerModeFact] public async Task StartAppHostWithWireMockAndCreateWireMockAdminClientToCallTheAdminEndpoint() { - if (!DockerUtils.IsDockerRunningLinuxContainerMode()) - { - output.WriteLine("Docker is not running in Linux container mode. Skipping test."); - return; - } - // Arrange var appHostBuilder = await DistributedApplicationTestingBuilder.CreateAsync(); await using var app = await appHostBuilder.BuildAsync(); diff --git a/test/WireMock.Net.Tests/Facts/IgnoreOnContinuousIntegrationFact.cs b/test/WireMock.Net.Tests/Facts/IgnoreOnContinuousIntegrationFact.cs index d1254d94..f8f70818 100644 --- a/test/WireMock.Net.Tests/Facts/IgnoreOnContinuousIntegrationFact.cs +++ b/test/WireMock.Net.Tests/Facts/IgnoreOnContinuousIntegrationFact.cs @@ -7,16 +7,16 @@ 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; - + private const 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 readonly bool IsContinuousIntegration = IsContinuousIntegrationAzure || IsContinuousIntegrationGithub; + public IgnoreOnContinuousIntegrationFact() { - if (IsContinuousIntegration()) + if (IsContinuousIntegration) { - Skip = _skipReason; + Skip = SkipReason; } } -} +} \ No newline at end of file diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index dfe24c63..909ff660 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -40,7 +40,7 @@ public WireMockServerTests(ITestOutputHelper testOutputHelper) { _testOutputHelper = testOutputHelper; } - + [Fact] public void WireMockServer_Start() { @@ -209,7 +209,7 @@ private static string[] GetIPAddressesByFamily(AddressFamily addressFamily) .Select(addr => addr.Address.ToString()) .ToArray(); } - + [IgnoreOnContinuousIntegrationFact] public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv4() { @@ -218,22 +218,20 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv4() var IPv4 = GetIPAddressesByFamily(System.Net.Sockets.AddressFamily.InterNetwork); var settings = new WireMockServerSettings { - Urls = new string[] { "http://0.0.0.0:" + port }, + Urls = ["http://0.0.0.0:" + port], }; - var server = WireMockServer.Start(settings); - + using var server = WireMockServer.Start(settings); + server.Given(Request.Create().WithPath("/*")).RespondWith(Response.Create().WithBody("x")); - - foreach (var addr in IPv4) + + foreach (var address in IPv4) { // Act - var response = await new HttpClient().GetStringAsync("http://" + addr + ":" + server.Ports[0] + "/foo").ConfigureAwait(false); - + var response = await new HttpClient().GetStringAsync("http://" + address + ":" + server.Ports[0] + "/foo").ConfigureAwait(false); + // Assert response.Should().Be("x"); - } - - server.Stop(); + } } [IgnoreOnContinuousIntegrationFact] @@ -244,25 +242,23 @@ public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv6() var IPv6 = GetIPAddressesByFamily(System.Net.Sockets.AddressFamily.InterNetworkV6); var settings = new WireMockServerSettings { - Urls = new string[] { "http://0.0.0.0:" + port }, + Urls = ["http://0.0.0.0:" + port], }; - var server = WireMockServer.Start(settings); - + using var server = WireMockServer.Start(settings); + server.Given(Request.Create().WithPath("/*")).RespondWith(Response.Create().WithBody("x")); - foreach (var addr in IPv6) + foreach (var address in IPv6) { // Act - var response = await new HttpClient().GetStringAsync("http://[" + addr + "]:" + server.Ports[0] + "/foo").ConfigureAwait(false); - + var response = await new HttpClient().GetStringAsync("http://[" + address + "]:" + 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() {