diff --git a/src/WireMock.Net.Shared/Util/BodyParser.cs b/src/WireMock.Net.Shared/Util/BodyParser.cs index 826735ed..a04185e2 100644 --- a/src/WireMock.Net.Shared/Util/BodyParser.cs +++ b/src/WireMock.Net.Shared/Util/BodyParser.cs @@ -207,7 +207,6 @@ internal static class BodyParser { return true; } - ; // 1) Quick binary detection for (int i = 0; i < data.Length; i++) diff --git a/test/WireMock.Net.Tests/FluentAssertions/WireMockAdminApiAssertionsTests.cs b/test/WireMock.Net.Tests/FluentAssertions/WireMockAdminApiAssertionsTests.cs index abcf5080..7a512cc6 100644 --- a/test/WireMock.Net.Tests/FluentAssertions/WireMockAdminApiAssertionsTests.cs +++ b/test/WireMock.Net.Tests/FluentAssertions/WireMockAdminApiAssertionsTests.cs @@ -253,6 +253,106 @@ public class WireMockAdminApiAssertionsTests : IDisposable .WithMessage($"Expected _adminApi to have been called at address matching the absolute path \"/anypath\", but didn't find it among the calls to {{\"/\"}}."); } + [Fact] + public async Task HaveReceivedNoCalls_AtPath_WhenACallWasNotMadeToPath_Should_BeOK() + { + await _httpClient.GetAsync("xxx", _ct); + + _adminApi.Should() + .HaveReceivedNoCalls() + .AtPath("anypath"); + } + + [Fact] + public async Task HaveReceived0Calls_AtPath_WhenACallWasNotMadeToPath_Should_BeOK() + { + await _httpClient.GetAsync("xxx", _ct); + + _adminApi.Should() + .HaveReceived(0).Calls() + .AtPath("anypath"); + } + + [Fact] + public async Task HaveReceived1Call_AtPath_WhenACallWasMadeToPath_Should_BeOK() + { + await _httpClient.GetAsync("anypath", _ct); + + _adminApi.Should() + .HaveReceived(1).Calls() + .AtPath("/anypath"); + } + + [Fact] + public async Task HaveReceived1Call_AtPathUsingPost_WhenAPostCallWasMadeToPath_Should_BeOK() + { + await _httpClient.PostAsync("anypath", new StringContent(""), _ct); + + _adminApi.Should() + .HaveReceived(1).Calls() + .AtPath("/anypath") + .And + .UsingPost(); + } + + [Fact] + public async Task HaveReceived2Calls_AtPath_WhenACallWasMadeToPath_Should_BeOK() + { + await _httpClient.GetAsync("anypath", _ct); + + await _httpClient.GetAsync("anypath", _ct); + + _adminApi.Should() + .HaveReceived(2).Calls() + .AtPath("/anypath"); + } + + [Fact] + public async Task HaveReceivedACall_AtPath_WhenACallWasMadeToPath_Should_BeOK() + { + await _httpClient.GetAsync("anypath", _ct); + + _adminApi.Should() + .HaveReceivedACall() + .AtPath(new WildcardMatcher("/any*")); + } + + [Fact] + public async Task HaveReceivedACall_AtPathWildcardMatcher_WhenACallWasMadeToPath_Should_BeOK() + { + await _httpClient.GetAsync("anypath", _ct); + + _adminApi.Should() + .HaveReceivedACall() + .AtPath("/anypath"); + } + + [Fact] + public void HaveReceivedACall_AtPath_Should_ThrowWhenNoCallsWereMade() + { + Action act = () => _adminApi.Should() + .HaveReceivedACall() + .AtPath("anypath"); + + act.Should() + .Throw() + .WithMessage("Expected _adminApi to have been called at address matching the path \"anypath\", but no calls were made."); + } + + [Fact] + public async Task HaveReceivedACall_AtPath_Should_ThrowWhenNoCallsMatchingThePathWereMade() + { + await _httpClient.GetAsync("", _ct); + + Action act = () => _adminApi.Should() + .HaveReceivedACall() + .AtPath("/anypath"); + + act.Should() + .Throw() + .WithMessage($"Expected _adminApi to have been called at address matching the path \"/anypath\", but didn't find it among the calls to {{\"/\"}}."); + } + [Fact] public async Task HaveReceivedACall_WithHeader_WhenACallWasMadeWithExpectedHeader_Should_BeOK() { @@ -344,7 +444,6 @@ public class WireMockAdminApiAssertionsTests : IDisposable public async Task HaveReceivedACall_WithHeader_ShouldCheckAllRequests() { // Arrange - var cancellationToken = _ct; using var server = WireMockServer.Start(settings => { settings.StartAdminInterface = true; @@ -363,7 +462,7 @@ public class WireMockAdminApiAssertionsTests : IDisposable { Authorization = new AuthenticationHeaderValue("Bearer", "invalidToken") } - }, cancellationToken); + }, _ct); // Act 2 var task2 = client2.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/") @@ -372,7 +471,7 @@ public class WireMockAdminApiAssertionsTests : IDisposable { Authorization = new AuthenticationHeaderValue("Bearer", "validToken") } - }, cancellationToken); + }, _ct); await Task.WhenAll(task1, task2); diff --git a/test/WireMock.Net.Tests/FluentAssertions/WireMockAssertionsTests.cs b/test/WireMock.Net.Tests/FluentAssertions/WireMockAssertionsTests.cs index eaffd1aa..4820664a 100644 --- a/test/WireMock.Net.Tests/FluentAssertions/WireMockAssertionsTests.cs +++ b/test/WireMock.Net.Tests/FluentAssertions/WireMockAssertionsTests.cs @@ -241,6 +241,106 @@ public class WireMockAssertionsTests : IDisposable .WithMessage($"Expected _server to have been called at address matching the absolute path \"/anypath\", but didn't find it among the calls to {{\"/\"}}."); } + [Fact] + public async Task HaveReceivedNoCalls_AtPath_WhenACallWasNotMadeToPath_Should_BeOK() + { + await _httpClient.GetAsync("xxx", _ct); + + _server.Should() + .HaveReceivedNoCalls() + .AtPath("anypath"); + } + + [Fact] + public async Task HaveReceived0Calls_AtPath_WhenACallWasNotMadeToPath_Should_BeOK() + { + await _httpClient.GetAsync("xxx", _ct); + + _server.Should() + .HaveReceived(0).Calls() + .AtPath("anypath"); + } + + [Fact] + public async Task HaveReceived1Call_AtPath_WhenACallWasMadeToPath_Should_BeOK() + { + await _httpClient.GetAsync("anypath", _ct); + + _server.Should() + .HaveReceived(1).Calls() + .AtPath("/anypath"); + } + + [Fact] + public async Task HaveReceived1Call_AtPathUsingPost_WhenAPostCallWasMadeToPath_Should_BeOK() + { + await _httpClient.PostAsync("anypath", new StringContent(""), _ct); + + _server.Should() + .HaveReceived(1).Calls() + .AtPath("/anypath") + .And + .UsingPost(); + } + + [Fact] + public async Task HaveReceived2Calls_AtPath_WhenACallWasMadeToPath_Should_BeOK() + { + await _httpClient.GetAsync("anypath", _ct); + + await _httpClient.GetAsync("anypath", _ct); + + _server.Should() + .HaveReceived(2).Calls() + .AtPath("/anypath"); + } + + [Fact] + public async Task HaveReceivedACall_AtPath_WhenACallWasMadeToPath_Should_BeOK() + { + await _httpClient.GetAsync("anypath", _ct); + + _server.Should() + .HaveReceivedACall() + .AtPath(new WildcardMatcher("/any*")); + } + + [Fact] + public async Task HaveReceivedACall_AtPathWildcardMatcher_WhenACallWasMadeToPath_Should_BeOK() + { + await _httpClient.GetAsync("anypath", _ct); + + _server.Should() + .HaveReceivedACall() + .AtPath("/anypath"); + } + + [Fact] + public void HaveReceivedACall_AtPath_Should_ThrowWhenNoCallsWereMade() + { + Action act = () => _server.Should() + .HaveReceivedACall() + .AtPath("anypath"); + + act.Should() + .Throw() + .WithMessage("Expected _server to have been called at address matching the path \"anypath\", but no calls were made."); + } + + [Fact] + public async Task HaveReceivedACall_AtPath_Should_ThrowWhenNoCallsMatchingThePathWereMade() + { + await _httpClient.GetAsync("", _ct); + + Action act = () => _server.Should() + .HaveReceivedACall() + .AtPath("/anypath"); + + act.Should() + .Throw() + .WithMessage($"Expected _server to have been called at address matching the path \"anypath\", but didn't find it among the calls to {{\"\"}}."); + } + [Fact] public async Task HaveReceivedACall_WithHeader_WhenACallWasMadeWithExpectedHeader_Should_BeOK() { @@ -332,7 +432,6 @@ public class WireMockAssertionsTests : IDisposable public async Task HaveReceivedACall_WithHeader_ShouldCheckAllRequests() { // Arrange - var cancellationToken = _ct; using var server = WireMockServer.Start(); using var client1 = server.CreateClient(); @@ -346,7 +445,7 @@ public class WireMockAssertionsTests : IDisposable { Authorization = new AuthenticationHeaderValue("Bearer", "invalidToken") } - }, cancellationToken); + }, _ct); // Act 2 var task2 = client2.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/") @@ -355,7 +454,7 @@ public class WireMockAssertionsTests : IDisposable { Authorization = new AuthenticationHeaderValue("Bearer", "validToken") } - }, cancellationToken); + }, _ct); await Task.WhenAll(task1, task2);