Make WireMockAssertions extendable (#1082)

This commit is contained in:
Stef Heyenrath
2024-03-12 20:27:12 +01:00
committed by GitHub
parent 5b609915e1
commit 511540a7f1
9 changed files with 216 additions and 141 deletions

View File

@@ -0,0 +1,37 @@
using System;
using System.Linq;
using FluentAssertions;
using FluentAssertions.Execution;
// ReSharper disable once CheckNamespace
namespace WireMock.FluentAssertions;
public static class WireMockAssertionsExtensions
{
[CustomAssertion]
public static AndWhichConstraint<WireMockAssertions, string> AtAbsoluteUrl2(this WireMockAssertions assertions,
string absoluteUrl, string because = "", params object[] becauseArgs)
{
var (filter, condition) = assertions.BuildFilterAndCondition(request => string.Equals(request.AbsoluteUrl, absoluteUrl, StringComparison.OrdinalIgnoreCase));
Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => assertions.RequestMessages)
.ForCondition(requests => assertions.CallsCount == 0 || requests.Any())
.FailWith(
"Expected {context:wiremockserver} to have been called at address matching the absolute url {0}{reason}, but no calls were made.",
absoluteUrl
)
.Then
.ForCondition(condition)
.FailWith(
"Expected {context:wiremockserver} to have been called at address matching the absolute url {0}{reason}, but didn't find it among the calls to {1}.",
_ => absoluteUrl,
requests => requests.Select(request => request.AbsoluteUrl)
);
assertions.FilterRequestMessages(filter);
return new AndWhichConstraint<WireMockAssertions, string>(assertions, absoluteUrl);
}
}

View File

@@ -61,6 +61,16 @@ public class WireMockAssertionsTests : IDisposable
.AtAbsoluteUrl($"http://localhost:{_portUsed}/anyurl");
}
[Fact]
public async Task HaveReceived1Calls_AtAbsoluteUrl2_WhenACallWasMadeToAbsoluteUrl_Should_BeOK()
{
await _httpClient.GetAsync("anyurl").ConfigureAwait(false);
_server.Should()
.HaveReceived(1).Calls()
.AtAbsoluteUrl2($"http://localhost:{_portUsed}/anyurl");
}
[Fact]
public async Task HaveReceived1Calls_AtAbsoluteUrlUsingPost_WhenAPostCallWasMadeToAbsoluteUrl_Should_BeOK()
{
@@ -129,7 +139,7 @@ public class WireMockAssertionsTests : IDisposable
_server.Should()
.HaveReceivedACall()
.WitHeaderKey("Authorization");
.WitHeaderKey("Authorization").Which.Should().StartWith("A");
}
[Fact]