diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5c73489e..c1e7906b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,7 +31,7 @@ jobs: run: dotnet test './test/WireMock.Net.Tests.UsingNuGet/WireMock.Net.Tests.UsingNuGet.csproj' -c Release - name: 'WireMock.Net.TUnitTests' - run: dotnet test './test/WireMock.Net.TUnitTests/WireMock.Net.TUnitTests.csproj' -c Release --framework net8.0 + run: dotnet test './test/WireMock.Net.TUnitTests/WireMock.Net.TUnitTests.csproj' -c Release --framework net8.0 -p:UseVSTest=false - name: 'WireMock.Net.Middleware.Tests' run: dotnet test './test/WireMock.Net.Middleware.Tests/WireMock.Net.Middleware.Tests.csproj' -c Release --framework net8.0 diff --git a/WireMock.Net Solution.sln b/WireMock.Net Solution.sln index 958679c9..7ec3b687 100644 --- a/WireMock.Net Solution.sln +++ b/WireMock.Net Solution.sln @@ -58,9 +58,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Actions", "Actions", "{1DAE EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Azure Pipelines", "Azure Pipelines", "{5B64F6CA-BF6B-4F67-BB2A-9C47E441703E}" ProjectSection(SolutionItems) = preProject - azure-pipelines-ci-linux.yml = azure-pipelines-ci-linux.yml azure-pipelines-ci.yml = azure-pipelines-ci.yml - azure-pipelines-linux.yml = azure-pipelines-linux.yml azure-pipelines-nuget.yml = azure-pipelines-nuget.yml EndProjectSection EndProject diff --git a/azure-pipelines-ci.yml b/azure-pipelines-ci.yml index a0273ecc..a2b2a4a4 100644 --- a/azure-pipelines-ci.yml +++ b/azure-pipelines-ci.yml @@ -53,7 +53,7 @@ jobs: - task: CmdLine@2 inputs: script: | - dotnet-coverage collect "dotnet test ./test/WireMock.Net.TUnitTests/WireMock.Net.TUnitTests.csproj --configuration Debug --no-build --framework net8.0" -f xml -o "wiremock-coverage-tunit.xml" + dotnet-coverage collect "dotnet test ./test/WireMock.Net.TUnitTests/WireMock.Net.TUnitTests.csproj --configuration Debug -p:UseVSTest=false --no-build --framework net8.0" -f xml -o "wiremock-coverage-tunit.xml" displayName: 'WireMock.Net.TUnitTests with Coverage' - task: CmdLine@2 diff --git a/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.cs b/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.cs index 62c64247..d02280ad 100644 --- a/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.cs +++ b/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.cs @@ -47,6 +47,7 @@ public partial class WireMockAdminApiTests public async Task IWireMockAdminApi_WaitForHealthAsync_AndCall_GetHealthAsync_OK() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var adminUsername = $"username_{Guid.NewGuid()}"; var adminPassword = $"password_{Guid.NewGuid()}"; var server = WireMockServer.Start(w => @@ -59,10 +60,10 @@ public partial class WireMockAdminApiTests .WithAuthorization(adminUsername, adminPassword); // Act 1 - await api.WaitForHealthAsync(); + await api.WaitForHealthAsync(cancellationToken: cancellationToken); // Act 2 - var status = await api.GetHealthAsync(); + var status = await api.GetHealthAsync(cancellationToken); status.Should().Be("Healthy"); } @@ -80,7 +81,7 @@ public partial class WireMockAdminApiTests var api = RestClient.For(server.Urls[0]); // Act - Func act = () => api.WaitForHealthAsync(maxRetries: 3); + Func act = () => api.WaitForHealthAsync(maxRetries: 3, cancellationToken: TestContext.Current.CancellationToken); await act.Should().ThrowAsync(); } @@ -92,7 +93,7 @@ public partial class WireMockAdminApiTests var api = RestClient.For(server.Urls[0]); // Act - var settings = await api.GetSettingsAsync(); + var settings = await api.GetSettingsAsync(TestContext.Current.CancellationToken); Check.That(settings).IsNotNull(); } @@ -108,7 +109,7 @@ public partial class WireMockAdminApiTests var api = RestClient.For(server.Urls[0] + "/foo"); // Act - var settings = await api.GetSettingsAsync(); + var settings = await api.GetSettingsAsync(TestContext.Current.CancellationToken); // Assert Check.That(settings).IsNotNull(); @@ -127,7 +128,7 @@ public partial class WireMockAdminApiTests // Act var settings = new SettingsModel(); - var status = await api.PostSettingsAsync(settings); + var status = await api.PostSettingsAsync(settings, TestContext.Current.CancellationToken); Check.That(status.Status).Equals("Settings updated"); } @@ -140,7 +141,7 @@ public partial class WireMockAdminApiTests // Act var settings = new SettingsModel(); - var status = await api.PutSettingsAsync(settings); + var status = await api.PutSettingsAsync(settings, TestContext.Current.CancellationToken); Check.That(status.Status).Equals("Settings updated"); } @@ -160,7 +161,7 @@ public partial class WireMockAdminApiTests Priority = 500, Title = "test" }; - var result = await api.PutMappingAsync(new Guid("a0000000-0000-0000-0000-000000000000"), model); + var result = await api.PutMappingAsync(new Guid("a0000000-0000-0000-0000-000000000000"), model, TestContext.Current.CancellationToken); // Assert Check.That(result).IsNotNull(); @@ -180,6 +181,7 @@ public partial class WireMockAdminApiTests public async Task IWireMockAdminApi_FindRequestsAsync() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var server = WireMockServer.Start(new WireMockServerSettings { StartAdminInterface = true, @@ -191,11 +193,11 @@ public partial class WireMockAdminApiTests var serverUrl = "http://localhost:" + server.Ports[0]; using var httpClient = new HttpClient(); - await httpClient.GetAsync(serverUrl + "/foo"); + await httpClient.GetAsync(serverUrl + "/foo", cancellationToken); var api = RestClient.For(serverUrl); // Act - var requests = await api.FindRequestsAsync(new RequestModel { Methods = new[] { "GET" } }); + var requests = await api.FindRequestsAsync(new RequestModel { Methods = new[] { "GET" } }, cancellationToken); // Assert requests.Should().HaveCount(1); @@ -209,6 +211,7 @@ public partial class WireMockAdminApiTests public async Task IWireMockAdminApi_FindRequestsByMappingGuidAsync_Found() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var mappingGuid = Guid.NewGuid(); var server = WireMockServer.Start(new WireMockServerSettings { @@ -222,12 +225,12 @@ public partial class WireMockAdminApiTests var serverUrl = "http://localhost:" + server.Ports[0]; using var client = new HttpClient(); - await client.GetAsync(serverUrl + "/foo"); - await client.GetAsync(serverUrl + "/foo?bar=baz"); + await client.GetAsync(serverUrl + "/foo", cancellationToken); + await client.GetAsync(serverUrl + "/foo?bar=baz", cancellationToken); var api = RestClient.For(serverUrl); // Act - var logEntryModels = await api.FindRequestsByMappingGuidAsync(mappingGuid); + var logEntryModels = await api.FindRequestsByMappingGuidAsync(mappingGuid, cancellationToken); // Assert logEntryModels.Should().HaveCount(2); @@ -250,6 +253,7 @@ public partial class WireMockAdminApiTests public async Task IWireMockAdminApi_FindRequestsByMappingGuidAsync_NotFound() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var server = WireMockServer.Start(new WireMockServerSettings { StartAdminInterface = true, @@ -262,11 +266,11 @@ public partial class WireMockAdminApiTests var serverUrl = "http://localhost:" + server.Ports[0]; using var httpClient = new HttpClient(); - await httpClient.GetAsync(serverUrl + "/foo"); + await httpClient.GetAsync(serverUrl + "/foo", cancellationToken); var api = RestClient.For(serverUrl); // Act - var logEntryModels = await api.FindRequestsByMappingGuidAsync(Guid.NewGuid()); + var logEntryModels = await api.FindRequestsByMappingGuidAsync(Guid.NewGuid(), cancellationToken); // Assert logEntryModels.Should().BeEmpty(); @@ -283,7 +287,7 @@ public partial class WireMockAdminApiTests }); // Act - var result = await server.CreateClient().GetAsync("/__admin/requests/find?mappingGuid=x"); + var result = await server.CreateClient().GetAsync("/__admin/requests/find?mappingGuid=x", TestContext.Current.CancellationToken); // Assert result.StatusCode.Should().Be(HttpStatusCode.BadRequest); @@ -293,6 +297,7 @@ public partial class WireMockAdminApiTests public async Task IWireMockAdminApi_GetRequestsAsync() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var server = WireMockServer.Start(new WireMockServerSettings { StartAdminInterface = true, @@ -300,11 +305,11 @@ public partial class WireMockAdminApiTests }); var serverUrl = "http://localhost:" + server.Ports[0]; using var httpClient = new HttpClient(); - await httpClient.GetAsync(serverUrl + "/foo"); + await httpClient.GetAsync(serverUrl + "/foo", cancellationToken); var api = RestClient.For(serverUrl); // Act - var requests = await api.GetRequestsAsync(); + var requests = await api.GetRequestsAsync(cancellationToken); // Assert Check.That(requests).HasSize(1); @@ -318,6 +323,7 @@ public partial class WireMockAdminApiTests public async Task IWireMockAdminApi_GetRequestsAsync_JsonApi() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var server = WireMockServer.Start(new WireMockServerSettings { StartAdminInterface = true, @@ -335,13 +341,13 @@ public partial class WireMockAdminApiTests request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(jsonApiContentType); using var client = new HttpClient(); - var response = await client.SendAsync(request); + var response = await client.SendAsync(request, cancellationToken); Check.That(response).IsNotNull(); var api = RestClient.For(serverUrl); // Act - var requests = await api.GetRequestsAsync(); + var requests = await api.GetRequestsAsync(cancellationToken); // Assert Check.That(requests).HasSize(1); @@ -355,6 +361,7 @@ public partial class WireMockAdminApiTests public async Task IWireMockAdminApi_GetMappingAsync_WithBodyModelMatcherModel_WithoutMethods_ShouldReturnCorrectMappingModel() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var guid = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05"); var server = WireMockServer.StartWithAdminInterface(); var api = RestClient.For(server.Url); @@ -378,7 +385,7 @@ public partial class WireMockAdminApiTests }, Response = new ResponseModel { Body = "world" } }; - var postMappingResult = await api.PostMappingAsync(model); + var postMappingResult = await api.PostMappingAsync(model, cancellationToken); // Assert postMappingResult.Should().NotBeNull(); @@ -386,7 +393,7 @@ public partial class WireMockAdminApiTests var mapping = server.Mappings.FirstOrDefault(m => m.Guid == guid); mapping.Should().NotBeNull(); - var getMappingResult = await api.GetMappingAsync(guid); + var getMappingResult = await api.GetMappingAsync(guid, cancellationToken); await Verify(getMappingResult, VerifySettings).DontScrubGuids(); @@ -397,6 +404,7 @@ public partial class WireMockAdminApiTests public async Task IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplaceSettings() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var guid = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05"); var server = WireMockServer.StartWithAdminInterface(); var api = RestClient.For(server.Url); @@ -429,7 +437,7 @@ public partial class WireMockAdminApiTests } } }; - var postMappingResult = await api.PostMappingAsync(model); + var postMappingResult = await api.PostMappingAsync(model, cancellationToken); // Assert postMappingResult.Should().NotBeNull(); @@ -437,7 +445,7 @@ public partial class WireMockAdminApiTests var mapping = server.Mappings.FirstOrDefault(m => m.Guid == guid); mapping.Should().NotBeNull(); - var getMappingResult = await api.GetMappingAsync(guid); + var getMappingResult = await api.GetMappingAsync(guid, cancellationToken); await Verify(getMappingResult, VerifySettings).DontScrubGuids(); @@ -448,6 +456,7 @@ public partial class WireMockAdminApiTests public async Task IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplaceSettings_And_TransformTemplate() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var guid = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f06"); var server = WireMockServer.StartWithAdminInterface(); var api = RestClient.For(server.Url); @@ -478,7 +487,7 @@ public partial class WireMockAdminApiTests } } }; - var postMappingResult = await api.PostMappingAsync(model).ConfigureAwait(false); + var postMappingResult = await api.PostMappingAsync(model, cancellationToken); // Assert postMappingResult.Should().NotBeNull(); @@ -486,7 +495,7 @@ public partial class WireMockAdminApiTests var mapping = server.Mappings.FirstOrDefault(m => m.Guid == guid); mapping.Should().NotBeNull(); - var getMappingResult = await api.GetMappingAsync(guid).ConfigureAwait(false); + var getMappingResult = await api.GetMappingAsync(guid, cancellationToken); await Verify(getMappingResult, VerifySettings).DontScrubGuids(); @@ -497,6 +506,7 @@ public partial class WireMockAdminApiTests public async Task IWireMockAdminApi_GetRequestsAsync_Json() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var server = WireMockServer.Start(new WireMockServerSettings { StartAdminInterface = true, @@ -513,13 +523,13 @@ public partial class WireMockAdminApiTests request.Content = new StringContent(data); request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(jsonApiContentType); using var client = new HttpClient(); - var response = await client.SendAsync(request); + var response = await client.SendAsync(request, cancellationToken); Check.That(response).IsNotNull(); var api = RestClient.For(serverUrl); // Act - var requests = await api.GetRequestsAsync(); + var requests = await api.GetRequestsAsync(cancellationToken); // Assert Check.That(requests).HasSize(1); @@ -548,7 +558,7 @@ public partial class WireMockAdminApiTests var api = RestClient.For(server.Urls[0]); // Act - var request = await api.PostFileAsync("filename.txt", "abc"); + var request = await api.PostFileAsync("filename.txt", "abc", TestContext.Current.CancellationToken); // Assert Check.That(request.Guid).IsNull(); @@ -581,7 +591,7 @@ public partial class WireMockAdminApiTests var api = RestClient.For(server.Urls[0]); // Act - var request = await api.PutFileAsync("filename.txt", "abc-abc"); + var request = await api.PutFileAsync("filename.txt", "abc-abc", TestContext.Current.CancellationToken); // Assert Check.That(request.Guid).IsNull(); @@ -612,7 +622,7 @@ public partial class WireMockAdminApiTests var api = RestClient.For(server.Urls[0]); // Act and Assert - Check.ThatCode(() => api.PutFileAsync("filename.txt", "xxx")).Throws(); + Check.ThatCode(() => api.PutFileAsync("filename.txt", "xxx", TestContext.Current.CancellationToken)).Throws(); // Verify filesystemHandlerMock.Verify(fs => fs.FileExists(It.Is(p => p == "filename.txt")), Times.Once); @@ -639,7 +649,7 @@ public partial class WireMockAdminApiTests var api = RestClient.For(server.Urls[0]); // Act and Assert - Check.ThatCode(() => api.GetFileAsync("filename.txt")).Throws(); + Check.ThatCode(() => api.GetFileAsync("filename.txt", TestContext.Current.CancellationToken)).Throws(); // Verify filesystemHandlerMock.Verify(fs => fs.FileExists(It.Is(p => p == "filename.txt")), Times.Once); @@ -667,7 +677,7 @@ public partial class WireMockAdminApiTests var api = RestClient.For(server.Urls[0]); // Act - string file = await api.GetFileAsync("filename.txt"); + string file = await api.GetFileAsync("filename.txt", TestContext.Current.CancellationToken); // Assert Check.That(file).Equals(data); @@ -698,7 +708,7 @@ public partial class WireMockAdminApiTests var api = RestClient.For(server.Urls[0]); // Act - await api.DeleteFileAsync("filename.txt"); + await api.DeleteFileAsync("filename.txt", TestContext.Current.CancellationToken); // Verify filesystemHandlerMock.Verify(fs => fs.FileExists(It.Is(p => p == "filename.txt")), Times.Once); @@ -726,7 +736,7 @@ public partial class WireMockAdminApiTests var api = RestClient.For(server.Urls[0]); // Act and Assert - Check.ThatCode(() => api.DeleteFileAsync("filename.txt")).Throws(); + Check.ThatCode(() => api.DeleteFileAsync("filename.txt", TestContext.Current.CancellationToken)).Throws(); // Verify filesystemHandlerMock.Verify(fs => fs.FileExists(It.Is(p => p == "filename.txt")), Times.Once); @@ -752,7 +762,7 @@ public partial class WireMockAdminApiTests var api = RestClient.For(server.Urls[0]); // Act and Assert - Check.ThatCode(() => api.FileExistsAsync("filename.txt")).Throws(); + Check.ThatCode(() => api.FileExistsAsync("filename.txt", TestContext.Current.CancellationToken)).Throws(); // Verify filesystemHandlerMock.Verify(fs => fs.FileExists(It.Is(p => p == "filename.txt")), Times.Once); @@ -770,7 +780,7 @@ public partial class WireMockAdminApiTests var api = RestClient.For(server.Urls[0]); // Act - var status = await api.DeleteScenarioAsync(name); + var status = await api.DeleteScenarioAsync(name, TestContext.Current.CancellationToken); status.Status.Should().Be("No scenario found by name 'x'."); } @@ -783,7 +793,7 @@ public partial class WireMockAdminApiTests var api = RestClient.For(server.Urls[0]); // Act - var status = await api.ResetScenarioAsync(name); + var status = await api.ResetScenarioAsync(name, TestContext.Current.CancellationToken); status.Status.Should().Be("No scenario found by name 'x'."); } @@ -800,7 +810,7 @@ public partial class WireMockAdminApiTests { State = null }; - var status = await api.PutScenarioStateAsync("x", update); + var status = await api.PutScenarioStateAsync("x", update, TestContext.Current.CancellationToken); status.Status.Should().Be("No scenario found by name 'x'."); } @@ -834,7 +844,7 @@ public partial class WireMockAdminApiTests { State = null }; - var status = await api.PutScenarioStateAsync("s1", update); + var status = await api.PutScenarioStateAsync("s1", update, TestContext.Current.CancellationToken); status.Status.Should().Be("Scenario state set to ''"); } @@ -861,7 +871,7 @@ public partial class WireMockAdminApiTests // Act var api = RestClient.For(server.Url); - var getMappingResult = await api.GetMappingAsync(guid); + var getMappingResult = await api.GetMappingAsync(guid, TestContext.Current.CancellationToken); // Assert var mapping = server.Mappings.FirstOrDefault(m => m.Guid == guid); @@ -876,6 +886,7 @@ public partial class WireMockAdminApiTests public async Task IWireMockAdminApi_GetMappingCodeByGuidAsync() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var guid = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05"); var server = WireMockServer.StartWithAdminInterface(); @@ -896,10 +907,10 @@ public partial class WireMockAdminApiTests // Act var api = RestClient.For(server.Url); - var mappings = await api.GetMappingsAsync(); + var mappings = await api.GetMappingsAsync(cancellationToken); mappings.Should().HaveCount(1); - var code = await api.GetMappingCodeAsync(guid); + var code = await api.GetMappingCodeAsync(guid, cancellationToken: cancellationToken); // Assert await Verify(code).DontScrubDateTimes().DontScrubGuids(); @@ -911,6 +922,7 @@ public partial class WireMockAdminApiTests public async Task IWireMockAdminApi_GetMappingsCode() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var guid1 = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05"); var guid2 = Guid.Parse("1b731398-4a5b-457f-a6e3-d65e541c428f"); var guid3 = Guid.Parse("f74fd144-df53-404f-8e35-da22a640bd5f"); @@ -1018,10 +1030,10 @@ text // Act var api = RestClient.For(server.Url); - var mappings = await api.GetMappingsAsync(); + var mappings = await api.GetMappingsAsync(cancellationToken); mappings.Should().HaveCount(5); - var code = await api.GetMappingsCodeAsync(); + var code = await api.GetMappingsCodeAsync(cancellationToken: cancellationToken); // Assert await Verify(code).DontScrubDateTimes().DontScrubGuids(); @@ -1039,7 +1051,7 @@ text var api = RestClient.For(server.Url); // Act - var mappings = await api.OpenApiConvertAsync(openApiDocument); + var mappings = await api.OpenApiConvertAsync(openApiDocument, TestContext.Current.CancellationToken); // Assert server.MappingModels.Should().BeEmpty(); @@ -1058,7 +1070,7 @@ text var api = RestClient.For(server.Url); // Act - var mappings = await api.OpenApiConvertAsync(openApiDocument); + var mappings = await api.OpenApiConvertAsync(openApiDocument, TestContext.Current.CancellationToken); // Assert server.MappingModels.Should().BeEmpty(); @@ -1077,7 +1089,7 @@ text var api = RestClient.For(server.Url); // Act - var statusModel = await api.OpenApiSaveAsync(openApiDocument); + var statusModel = await api.OpenApiSaveAsync(openApiDocument, TestContext.Current.CancellationToken); // Assert statusModel.Status.Should().Be("OpenApi document converted to Mappings"); @@ -1096,7 +1108,7 @@ text var api = RestClient.For(server.Url); // Act - var mappings = await api.OpenApiConvertAsync(openApiDocument); + var mappings = await api.OpenApiConvertAsync(openApiDocument, TestContext.Current.CancellationToken); // Assert server.MappingModels.Should().BeEmpty(); @@ -1113,7 +1125,7 @@ text var api = RestClient.For(server.Url); // Act - var status = await api.ReloadStaticMappingsAsync(); + var status = await api.ReloadStaticMappingsAsync(TestContext.Current.CancellationToken); // Assert status.Status.Should().Be("Static Mappings reloaded"); diff --git a/test/WireMock.Net.Tests/StatefulBehaviorTests.cs b/test/WireMock.Net.Tests/StatefulBehaviorTests.cs index f11734c9..dca170cf 100644 --- a/test/WireMock.Net.Tests/StatefulBehaviorTests.cs +++ b/test/WireMock.Net.Tests/StatefulBehaviorTests.cs @@ -26,7 +26,7 @@ public class StatefulBehaviorTests .RespondWith(Response.Create()); // when - var response = await new HttpClient().GetAsync("http://localhost:" + server.Ports[0] + path); + var response = await new HttpClient().GetAsync("http://localhost:" + server.Ports[0] + path, TestContext.Current.CancellationToken); // then Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.NotFound); @@ -38,6 +38,7 @@ public class StatefulBehaviorTests public async Task Scenarios_Should_process_request_if_equals_state_and_single_state_defined() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var path = $"/foo_{Guid.NewGuid()}"; using var server = WireMockServer.Start(); @@ -54,8 +55,8 @@ public class StatefulBehaviorTests .RespondWith(Response.Create().WithBody("Test state msg")); // Act - var responseNoState = await new HttpClient().GetStringAsync(server.Url + path); - var responseWithState = await new HttpClient().GetStringAsync(server.Url + path); + var responseNoState = await new HttpClient().GetStringAsync(server.Url + path, cancellationToken); + var responseWithState = await new HttpClient().GetStringAsync(server.Url + path, cancellationToken); // Assert Check.That(responseNoState).Equals("No state msg"); @@ -68,6 +69,7 @@ public class StatefulBehaviorTests public async Task Scenarios_Should_ContinueOnCorrectState_WhenStateIsUpdated(string? state, string expected1, string expected2) { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var path = $"/foo_{Guid.NewGuid()}"; var scenario = "s"; using var server = WireMockServer.Start(); @@ -93,13 +95,13 @@ public class StatefulBehaviorTests // Act var client = server.CreateClient(); - var response1 = await client.GetStringAsync(server.Url + path); - var response2 = await client.GetStringAsync(server.Url + path); - var response3 = await client.GetStringAsync(server.Url + path); + var response1 = await client.GetStringAsync(server.Url + path, cancellationToken); + var response2 = await client.GetStringAsync(server.Url + path, cancellationToken); + var response3 = await client.GetStringAsync(server.Url + path, cancellationToken); server.SetScenarioState(scenario, state); - var responseA = await client.GetStringAsync(server.Url + path); - var responseB = await client.GetStringAsync(server.Url + path); + var responseA = await client.GetStringAsync(server.Url + path, cancellationToken); + var responseB = await client.GetStringAsync(server.Url + path, cancellationToken); // Assert Check.That(response1).Equals("step 1"); @@ -113,6 +115,7 @@ public class StatefulBehaviorTests public async Task Scenarios_With_Same_Path_Should_Use_Times_When_Moving_To_Next_State() { // given + var cancellationToken = TestContext.Current.CancellationToken; const int times = 2; string path = $"/foo_{Guid.NewGuid()}"; string body1 = "Scenario S1, No State, Setting State T2"; @@ -133,9 +136,9 @@ public class StatefulBehaviorTests // when var client = new HttpClient(); - var responseScenario1 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path); - var responseScenario2 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path); - var responseWithState = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path); + var responseScenario1 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path, cancellationToken); + var responseScenario2 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path, cancellationToken); + var responseWithState = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path, cancellationToken); // then responseScenario1.Should().Be(body1); @@ -149,6 +152,7 @@ public class StatefulBehaviorTests public async Task Scenarios_With_Different_Paths_Should_Use_Times_When_Moving_To_Next_State() { // given + var cancellationToken = TestContext.Current.CancellationToken; const int times = 2; string path1 = $"/a_{Guid.NewGuid()}"; string path2 = $"/b_{Guid.NewGuid()}"; @@ -180,11 +184,11 @@ public class StatefulBehaviorTests // when var client = new HttpClient(); - var t1a = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path1); - var t1b = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path1); - var t2a = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path2); - var t2b = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path2); - var t3 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path3); + var t1a = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path1, cancellationToken); + var t1b = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path1, cancellationToken); + var t2a = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path2, cancellationToken); + var t2b = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path2, cancellationToken); + var t3 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path3, cancellationToken); // then t1a.Should().Be(body1); @@ -200,6 +204,7 @@ public class StatefulBehaviorTests public async Task Scenarios_Should_Respect_Int_Valued_Scenarios_and_States() { // given + var cancellationToken = TestContext.Current.CancellationToken; string path = $"/foo_{Guid.NewGuid()}"; var server = WireMockServer.Start(); @@ -216,8 +221,8 @@ public class StatefulBehaviorTests .RespondWith(Response.Create().WithBody("Scenario 1, State 2")); // when - var responseIntScenario = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path); - var responseWithIntState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path); + var responseIntScenario = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path, cancellationToken); + var responseWithIntState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path, cancellationToken); // then Check.That(responseIntScenario).Equals("Scenario 1, Setting State 2"); @@ -230,6 +235,7 @@ public class StatefulBehaviorTests public async Task Scenarios_Should_Respect_Mixed_String_Scenario_and_Int_State() { // given + var cancellationToken = TestContext.Current.CancellationToken; string path = $"/foo_{Guid.NewGuid()}"; var server = WireMockServer.Start(); @@ -246,8 +252,8 @@ public class StatefulBehaviorTests .RespondWith(Response.Create().WithBody("string state, State 2")); // when - var responseIntScenario = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path); - var responseWithIntState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path); + var responseIntScenario = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path, cancellationToken); + var responseWithIntState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path, cancellationToken); // then Check.That(responseIntScenario).Equals("string state, Setting State 2"); @@ -260,6 +266,7 @@ public class StatefulBehaviorTests public async Task Scenarios_Should_Respect_Mixed_Int_Scenario_and_String_Scenario_and_String_State() { // given + var cancellationToken = TestContext.Current.CancellationToken; string path = $"/foo_{Guid.NewGuid()}"; var server = WireMockServer.Start(); @@ -276,8 +283,8 @@ public class StatefulBehaviorTests .RespondWith(Response.Create().WithBody("string state, State 2")); // when - var responseIntScenario = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path); - var responseWithIntState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path); + var responseIntScenario = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path, cancellationToken); + var responseWithIntState = await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + path, cancellationToken); // then Check.That(responseIntScenario).Equals("int state, Setting State 2"); diff --git a/test/WireMock.Net.Tests/WireMockServer.Admin.cs b/test/WireMock.Net.Tests/WireMockServer.Admin.cs index 5ff779e2..df2034a4 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Admin.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Admin.cs @@ -244,6 +244,7 @@ public class WireMockServerAdminTests public async Task WireMockServer_Admin_Mapping_WithoutPathOrUrl() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; using var server = WireMockServer.StartWithAdminInterface(); // Act @@ -257,16 +258,16 @@ public class WireMockServerAdminTests pathMatcher.Should().BeNull(); var api = RestClient.For(server.Url); - var mappingModels = await api.GetMappingsAsync(); + var mappingModels = await api.GetMappingsAsync(cancellationToken); var mappingModel = mappingModels.First(); mappingModel.Request.Path.Should().BeNull(); mappingModel.Request.Url.Should().BeNull(); - await api.DeleteMappingsAsync(); + await api.DeleteMappingsAsync(cancellationToken); - await api.PostMappingAsync(mappingModel); - await api.GetMappingsAsync(); - mappingModels = await api.GetMappingsAsync(); + await api.PostMappingAsync(mappingModel, cancellationToken); + await api.GetMappingsAsync(cancellationToken); + mappingModels = await api.GetMappingsAsync(cancellationToken); mappingModel = mappingModels.First(); mappingModel.Request.Path.Should().BeNull(); mappingModel.Request.Url.Should().BeNull(); @@ -351,7 +352,7 @@ public class WireMockServerAdminTests Check.That(mappings).HasSize(2); // when - var response = await new HttpClient().GetAsync("http://localhost:" + server.Port + "/1"); + var response = await new HttpClient().GetAsync("http://localhost:" + server.Port + "/1", TestContext.Current.CancellationToken); // then Check.That((int)response.StatusCode).IsEqualTo(400); @@ -366,7 +367,7 @@ public class WireMockServerAdminTests var server = WireMockServer.Start(); // when - await new HttpClient().GetAsync("http://localhost:" + server.Ports[0] + "/foo"); + await new HttpClient().GetAsync("http://localhost:" + server.Ports[0] + "/foo", TestContext.Current.CancellationToken); // then Check.That(server.LogEntries).HasSize(1); @@ -381,14 +382,15 @@ public class WireMockServerAdminTests public async Task WireMockServer_Admin_Logging_SetMaxRequestLogCount() { // Assign + var cancellationToken = TestContext.Current.CancellationToken; var client = new HttpClient(); // Act var server = WireMockServer.Start(); server.SetMaxRequestLogCount(2); - await client.GetAsync("http://localhost:" + server.Ports[0] + "/foo1"); - await client.GetAsync("http://localhost:" + server.Ports[0] + "/foo2"); - await client.GetAsync("http://localhost:" + server.Ports[0] + "/foo3"); + await client.GetAsync("http://localhost:" + server.Ports[0] + "/foo1", cancellationToken); + await client.GetAsync("http://localhost:" + server.Ports[0] + "/foo2", cancellationToken); + await client.GetAsync("http://localhost:" + server.Ports[0] + "/foo3", cancellationToken); // Assert Check.That(server.LogEntries).HasSize(2); @@ -406,15 +408,16 @@ public class WireMockServerAdminTests public async Task WireMockServer_Admin_Logging_SetMaxRequestLogCount_To_0_Should_Not_AddLogging() { // Assign + var cancellationToken = TestContext.Current.CancellationToken; var client = new HttpClient(); // Act var server = WireMockServer.Start(); server.SetMaxRequestLogCount(0); - await client.GetAsync("http://localhost:" + server.Port + "/foo1"); - await client.GetAsync("http://localhost:" + server.Port + "/foo2"); - await client.GetAsync("http://localhost:" + server.Port + "/foo3"); + await client.GetAsync("http://localhost:" + server.Port + "/foo1", cancellationToken); + await client.GetAsync("http://localhost:" + server.Port + "/foo2", cancellationToken); + await client.GetAsync("http://localhost:" + server.Port + "/foo3", cancellationToken); // Assert server.LogEntries.Should().BeEmpty(); @@ -426,6 +429,7 @@ public class WireMockServerAdminTests public async Task WireMockServer_Admin_Logging_FindLogEntries() { // Assign + var cancellationToken = TestContext.Current.CancellationToken; using var client = new HttpClient(); // Act @@ -433,9 +437,9 @@ public class WireMockServerAdminTests var tasks = new[] { - client.GetAsync($"{server.Url}/foo1"), - client.PostAsync($"{server.Url}/foo2", new StringContent("test")), - client.GetAsync($"{server.Url}/foo3") + client.GetAsync($"{server.Url}/foo1", cancellationToken), + client.PostAsync($"{server.Url}/foo2", new StringContent("test"), cancellationToken), + client.GetAsync($"{server.Url}/foo3", cancellationToken) }; await Task.WhenAll(tasks); @@ -585,11 +589,12 @@ public class WireMockServerAdminTests public async Task WireMockServer_CreateClient_And_CallEndpoint() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var server = WireMockServer.Start(); var client = server.CreateClient(); // Act - await client.GetAsync($"{server.Url}/foo"); + await client.GetAsync($"{server.Url}/foo", cancellationToken); // Assert Check.That(server.LogEntries).HasSize(1); @@ -606,6 +611,7 @@ public class WireMockServerAdminTests public async Task WireMockServer_CreateClient_And_CallAdminSettingsEndpoint() { // Arrange + var cancellationToken = TestContext.Current.CancellationToken; var server = WireMockServer.Start(w => { w.StartAdminInterface = true; @@ -614,7 +620,7 @@ public class WireMockServerAdminTests var client = server.CreateClient(); // Act - var settings = await client.GetStringAsync($"{server.Url}/adm/settings"); + var settings = await client.GetStringAsync($"{server.Url}/adm/settings", cancellationToken); // Assert settings.Should().NotBeNull();