Skip to content

Commit

Permalink
Fixes for WireMock.Net.FluentAssertions (callcount behaviour) (#832)
Browse files Browse the repository at this point in the history
* UsingAnyMethod

* fix

* .
  • Loading branch information
StefH authored Oct 17, 2022
1 parent fb8fec0 commit 306c69f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 24 deletions.
51 changes: 28 additions & 23 deletions src/WireMock.Net.FluentAssertions/Assertions/WireMockAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ namespace WireMock.FluentAssertions;

public class WireMockAssertions
{
private const string Any = "*";
private readonly int? _callsCount;
private IReadOnlyList<IRequestMessage> _requestMessages;
private IReadOnlyList<KeyValuePair<string, WireMockList<string>>> _headers;
private readonly IReadOnlyList<KeyValuePair<string, WireMockList<string>>> _headers;

public WireMockAssertions(IWireMockServer subject, int? callsCount)
{
Expand All @@ -27,12 +28,13 @@ public WireMockAssertions(IWireMockServer subject, int? callsCount)
public AndWhichConstraint<WireMockAssertions, string> AtAbsoluteUrl(string absoluteUrl, string because = "", params object[] becauseArgs)
{
Func<IRequestMessage, bool> predicate = request => string.Equals(request.AbsoluteUrl, absoluteUrl, StringComparison.OrdinalIgnoreCase);

var (filter, condition) = BuildFilterAndCondition(predicate);

Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _requestMessages)
.ForCondition(requests => requests.Any())
.ForCondition(requests => _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
Expand All @@ -53,15 +55,17 @@ public AndWhichConstraint<WireMockAssertions, string> AtAbsoluteUrl(string absol
public AndWhichConstraint<WireMockAssertions, string> AtUrl(string url, string because = "", params object[] becauseArgs)
{
Func<IRequestMessage, bool> predicate = request => string.Equals(request.Url, url, StringComparison.OrdinalIgnoreCase);

var (filter, condition) = BuildFilterAndCondition(predicate);

Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _requestMessages)
.ForCondition(requests => requests.Any())
.ForCondition(requests => _callsCount == 0 || requests.Any())
.FailWith(
"Expected {context:wiremockserver} to have been called at address matching the url {0}{reason}, but no calls were made.",
url)
url
)
.Then
.ForCondition(condition)
.FailWith(
Expand All @@ -79,12 +83,13 @@ public AndWhichConstraint<WireMockAssertions, string> AtUrl(string url, string b
public AndWhichConstraint<WireMockAssertions, string> WithProxyUrl(string proxyUrl, string because = "", params object[] becauseArgs)
{
Func<IRequestMessage, bool> predicate = request => string.Equals(request.ProxyUrl, proxyUrl, StringComparison.OrdinalIgnoreCase);

var (filter, condition) = BuildFilterAndCondition(predicate);

Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _requestMessages)
.ForCondition(requests => requests.Any())
.ForCondition(requests => _callsCount == 0 || requests.Any())
.FailWith(
"Expected {context:wiremockserver} to have been called with proxy url {0}{reason}, but no calls were made.",
proxyUrl
Expand All @@ -94,8 +99,7 @@ public AndWhichConstraint<WireMockAssertions, string> WithProxyUrl(string proxyU
.FailWith(
"Expected {context:wiremockserver} to have been called with proxy url {0}{reason}, but didn't find it among the calls with {1}.",
_ => proxyUrl,
requests => requests
.Select(request => request.ProxyUrl)
requests => requests.Select(request => request.ProxyUrl)
);

_requestMessages = filter(_requestMessages).ToList();
Expand All @@ -107,23 +111,23 @@ public AndWhichConstraint<WireMockAssertions, string> WithProxyUrl(string proxyU
public AndWhichConstraint<WireMockAssertions, string> FromClientIP(string clientIP, string because = "", params object[] becauseArgs)
{
Func<IRequestMessage, bool> predicate = request => string.Equals(request.ClientIP, clientIP, StringComparison.OrdinalIgnoreCase);

var (filter, condition) = BuildFilterAndCondition(predicate);

Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _requestMessages)
.ForCondition(requests => requests.Any())
.ForCondition(requests => _callsCount == 0 || requests.Any())
.FailWith(
"Expected {context:wiremockserver} to have been called from client IP {0}{reason}, but no calls were made.",
clientIP)
.Then
.ForCondition(requests =>
(_callsCount == null && requests.Any(req => req.ClientIP == clientIP)) ||
(_callsCount == requests.Count(req => req.ClientIP == clientIP))
clientIP
)
.Then
.ForCondition(condition)
.FailWith(
"Expected {context:wiremockserver} to have been called from client IP {0}{reason}, but didn't find it among the calls from IP(s) {1}.",
_ => clientIP, requests => requests.Select(request => request.ClientIP));
_ => clientIP, requests => requests.Select(request => request.ClientIP)
);

_requestMessages = filter(_requestMessages).ToList();

Expand Down Expand Up @@ -199,16 +203,23 @@ public AndConstraint<WireMockAssertions> UsingPut(string because = "", params ob
public AndConstraint<WireMockAssertions> UsingTrace(string because = "", params object[] becauseArgs)
=> UsingMethod("TRACE", because, becauseArgs);

[CustomAssertion]
public AndConstraint<WireMockAssertions> UsingAnyMethod(string because = "", params object[] becauseArgs)
=> UsingMethod(Any, because, becauseArgs);

[CustomAssertion]
public AndConstraint<WireMockAssertions> UsingMethod(string method, string because = "", params object[] becauseArgs)
{
Func<IRequestMessage, bool> predicate = request => string.Equals(request.Method, method, StringComparison.OrdinalIgnoreCase);
var any = method == Any;
Func<IRequestMessage, bool> predicate = request => (any && !string.IsNullOrEmpty(request.Method)) ||
string.Equals(request.Method, method, StringComparison.OrdinalIgnoreCase);

var (filter, condition) = BuildFilterAndCondition(predicate);

Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _requestMessages)
.ForCondition(requests => requests.Any())
.ForCondition(requests => _callsCount == 0 || requests.Any())
.FailWith(
"Expected {context:wiremockserver} to have been called using method {0}{reason}, but no calls were made.",
method
Expand All @@ -230,12 +241,6 @@ public AndConstraint<WireMockAssertions> UsingMethod(string method, string becau
{
Func<IReadOnlyList<IRequestMessage>, IReadOnlyList<IRequestMessage>> filter = requests => requests.Where(predicate).ToList();

return
(
filter,
requests =>
(_callsCount == null && filter(_requestMessages).Any()) ||
(_callsCount == filter(_requestMessages).Count())
);
return (filter, requests => (_callsCount is null && filter(requests).Any()) || _callsCount == filter(requests).Count);
}
}
1 change: 0 additions & 1 deletion src/WireMock.Net/Settings/WireMockServerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using WireMock.Types;
#if USE_ASPNETCORE
using Microsoft.Extensions.DependencyInjection;
using WireMock.Types;
#endif

namespace WireMock.Settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,44 @@ public async Task HaveReceivedACall_UsingTrace_WhenACallWasMadeUsingTrace_Should
.UsingTrace();
}

[Fact]
public async Task HaveReceivedACall_UsingAnyMethod_WhenACallWasMadeUsingGet_Should_BeOK()
{
await _httpClient.SendAsync(new HttpRequestMessage(new HttpMethod("GET"), "anyurl")).ConfigureAwait(false);

_server.Should()
.HaveReceivedACall()
.UsingAnyMethod();
}

[Fact]
public void HaveReceivedNoCalls_UsingAnyMethod_WhenNoCallsWereMade_Should_BeOK()
{
_server
.Should()
.HaveReceived(0)
.Calls()
.UsingAnyMethod();

_server
.Should()
.HaveReceivedNoCalls()
.UsingAnyMethod();
}

[Fact]
public void HaveReceivedNoCalls_AtUrl_WhenNoCallsWereMade_Should_BeOK()
{
_server.Should()
.HaveReceived(0)
.Calls()
.AtUrl(_server.Url ?? string.Empty);

_server.Should()
.HaveReceivedNoCalls()
.AtUrl(_server.Url ?? string.Empty);
}

public void Dispose()
{
_server?.Stop();
Expand Down

0 comments on commit 306c69f

Please sign in to comment.