mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-31 06:33:25 +02:00
Fixes for WireMock.Net.FluentAssertions (callcount behaviour) (#832)
* UsingAnyMethod * fix * .
This commit is contained in:
@@ -12,9 +12,10 @@ namespace WireMock.FluentAssertions;
|
|||||||
|
|
||||||
public class WireMockAssertions
|
public class WireMockAssertions
|
||||||
{
|
{
|
||||||
|
private const string Any = "*";
|
||||||
private readonly int? _callsCount;
|
private readonly int? _callsCount;
|
||||||
private IReadOnlyList<IRequestMessage> _requestMessages;
|
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)
|
public WireMockAssertions(IWireMockServer subject, int? callsCount)
|
||||||
{
|
{
|
||||||
@@ -27,12 +28,13 @@ public class WireMockAssertions
|
|||||||
public AndWhichConstraint<WireMockAssertions, string> AtAbsoluteUrl(string absoluteUrl, string because = "", params object[] becauseArgs)
|
public AndWhichConstraint<WireMockAssertions, string> AtAbsoluteUrl(string absoluteUrl, string because = "", params object[] becauseArgs)
|
||||||
{
|
{
|
||||||
Func<IRequestMessage, bool> predicate = request => string.Equals(request.AbsoluteUrl, absoluteUrl, StringComparison.OrdinalIgnoreCase);
|
Func<IRequestMessage, bool> predicate = request => string.Equals(request.AbsoluteUrl, absoluteUrl, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
var (filter, condition) = BuildFilterAndCondition(predicate);
|
var (filter, condition) = BuildFilterAndCondition(predicate);
|
||||||
|
|
||||||
Execute.Assertion
|
Execute.Assertion
|
||||||
.BecauseOf(because, becauseArgs)
|
.BecauseOf(because, becauseArgs)
|
||||||
.Given(() => _requestMessages)
|
.Given(() => _requestMessages)
|
||||||
.ForCondition(requests => requests.Any())
|
.ForCondition(requests => _callsCount == 0 || requests.Any())
|
||||||
.FailWith(
|
.FailWith(
|
||||||
"Expected {context:wiremockserver} to have been called at address matching the absolute url {0}{reason}, but no calls were made.",
|
"Expected {context:wiremockserver} to have been called at address matching the absolute url {0}{reason}, but no calls were made.",
|
||||||
absoluteUrl
|
absoluteUrl
|
||||||
@@ -53,15 +55,17 @@ public class WireMockAssertions
|
|||||||
public AndWhichConstraint<WireMockAssertions, string> AtUrl(string url, string because = "", params object[] becauseArgs)
|
public AndWhichConstraint<WireMockAssertions, string> AtUrl(string url, string because = "", params object[] becauseArgs)
|
||||||
{
|
{
|
||||||
Func<IRequestMessage, bool> predicate = request => string.Equals(request.Url, url, StringComparison.OrdinalIgnoreCase);
|
Func<IRequestMessage, bool> predicate = request => string.Equals(request.Url, url, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
var (filter, condition) = BuildFilterAndCondition(predicate);
|
var (filter, condition) = BuildFilterAndCondition(predicate);
|
||||||
|
|
||||||
Execute.Assertion
|
Execute.Assertion
|
||||||
.BecauseOf(because, becauseArgs)
|
.BecauseOf(because, becauseArgs)
|
||||||
.Given(() => _requestMessages)
|
.Given(() => _requestMessages)
|
||||||
.ForCondition(requests => requests.Any())
|
.ForCondition(requests => _callsCount == 0 || requests.Any())
|
||||||
.FailWith(
|
.FailWith(
|
||||||
"Expected {context:wiremockserver} to have been called at address matching the url {0}{reason}, but no calls were made.",
|
"Expected {context:wiremockserver} to have been called at address matching the url {0}{reason}, but no calls were made.",
|
||||||
url)
|
url
|
||||||
|
)
|
||||||
.Then
|
.Then
|
||||||
.ForCondition(condition)
|
.ForCondition(condition)
|
||||||
.FailWith(
|
.FailWith(
|
||||||
@@ -79,12 +83,13 @@ public class WireMockAssertions
|
|||||||
public AndWhichConstraint<WireMockAssertions, string> WithProxyUrl(string proxyUrl, string because = "", params object[] becauseArgs)
|
public AndWhichConstraint<WireMockAssertions, string> WithProxyUrl(string proxyUrl, string because = "", params object[] becauseArgs)
|
||||||
{
|
{
|
||||||
Func<IRequestMessage, bool> predicate = request => string.Equals(request.ProxyUrl, proxyUrl, StringComparison.OrdinalIgnoreCase);
|
Func<IRequestMessage, bool> predicate = request => string.Equals(request.ProxyUrl, proxyUrl, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
var (filter, condition) = BuildFilterAndCondition(predicate);
|
var (filter, condition) = BuildFilterAndCondition(predicate);
|
||||||
|
|
||||||
Execute.Assertion
|
Execute.Assertion
|
||||||
.BecauseOf(because, becauseArgs)
|
.BecauseOf(because, becauseArgs)
|
||||||
.Given(() => _requestMessages)
|
.Given(() => _requestMessages)
|
||||||
.ForCondition(requests => requests.Any())
|
.ForCondition(requests => _callsCount == 0 || requests.Any())
|
||||||
.FailWith(
|
.FailWith(
|
||||||
"Expected {context:wiremockserver} to have been called with proxy url {0}{reason}, but no calls were made.",
|
"Expected {context:wiremockserver} to have been called with proxy url {0}{reason}, but no calls were made.",
|
||||||
proxyUrl
|
proxyUrl
|
||||||
@@ -94,8 +99,7 @@ public class WireMockAssertions
|
|||||||
.FailWith(
|
.FailWith(
|
||||||
"Expected {context:wiremockserver} to have been called with proxy url {0}{reason}, but didn't find it among the calls with {1}.",
|
"Expected {context:wiremockserver} to have been called with proxy url {0}{reason}, but didn't find it among the calls with {1}.",
|
||||||
_ => proxyUrl,
|
_ => proxyUrl,
|
||||||
requests => requests
|
requests => requests.Select(request => request.ProxyUrl)
|
||||||
.Select(request => request.ProxyUrl)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
_requestMessages = filter(_requestMessages).ToList();
|
_requestMessages = filter(_requestMessages).ToList();
|
||||||
@@ -107,23 +111,23 @@ public class WireMockAssertions
|
|||||||
public AndWhichConstraint<WireMockAssertions, string> FromClientIP(string clientIP, string because = "", params object[] becauseArgs)
|
public AndWhichConstraint<WireMockAssertions, string> FromClientIP(string clientIP, string because = "", params object[] becauseArgs)
|
||||||
{
|
{
|
||||||
Func<IRequestMessage, bool> predicate = request => string.Equals(request.ClientIP, clientIP, StringComparison.OrdinalIgnoreCase);
|
Func<IRequestMessage, bool> predicate = request => string.Equals(request.ClientIP, clientIP, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
var (filter, condition) = BuildFilterAndCondition(predicate);
|
var (filter, condition) = BuildFilterAndCondition(predicate);
|
||||||
|
|
||||||
Execute.Assertion
|
Execute.Assertion
|
||||||
.BecauseOf(because, becauseArgs)
|
.BecauseOf(because, becauseArgs)
|
||||||
.Given(() => _requestMessages)
|
.Given(() => _requestMessages)
|
||||||
.ForCondition(requests => requests.Any())
|
.ForCondition(requests => _callsCount == 0 || requests.Any())
|
||||||
.FailWith(
|
.FailWith(
|
||||||
"Expected {context:wiremockserver} to have been called from client IP {0}{reason}, but no calls were made.",
|
"Expected {context:wiremockserver} to have been called from client IP {0}{reason}, but no calls were made.",
|
||||||
clientIP)
|
clientIP
|
||||||
.Then
|
|
||||||
.ForCondition(requests =>
|
|
||||||
(_callsCount == null && requests.Any(req => req.ClientIP == clientIP)) ||
|
|
||||||
(_callsCount == requests.Count(req => req.ClientIP == clientIP))
|
|
||||||
)
|
)
|
||||||
|
.Then
|
||||||
|
.ForCondition(condition)
|
||||||
.FailWith(
|
.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}.",
|
"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();
|
_requestMessages = filter(_requestMessages).ToList();
|
||||||
|
|
||||||
@@ -199,16 +203,23 @@ public class WireMockAssertions
|
|||||||
public AndConstraint<WireMockAssertions> UsingTrace(string because = "", params object[] becauseArgs)
|
public AndConstraint<WireMockAssertions> UsingTrace(string because = "", params object[] becauseArgs)
|
||||||
=> UsingMethod("TRACE", because, becauseArgs);
|
=> UsingMethod("TRACE", because, becauseArgs);
|
||||||
|
|
||||||
|
[CustomAssertion]
|
||||||
|
public AndConstraint<WireMockAssertions> UsingAnyMethod(string because = "", params object[] becauseArgs)
|
||||||
|
=> UsingMethod(Any, because, becauseArgs);
|
||||||
|
|
||||||
[CustomAssertion]
|
[CustomAssertion]
|
||||||
public AndConstraint<WireMockAssertions> UsingMethod(string method, string because = "", params object[] becauseArgs)
|
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);
|
var (filter, condition) = BuildFilterAndCondition(predicate);
|
||||||
|
|
||||||
Execute.Assertion
|
Execute.Assertion
|
||||||
.BecauseOf(because, becauseArgs)
|
.BecauseOf(because, becauseArgs)
|
||||||
.Given(() => _requestMessages)
|
.Given(() => _requestMessages)
|
||||||
.ForCondition(requests => requests.Any())
|
.ForCondition(requests => _callsCount == 0 || requests.Any())
|
||||||
.FailWith(
|
.FailWith(
|
||||||
"Expected {context:wiremockserver} to have been called using method {0}{reason}, but no calls were made.",
|
"Expected {context:wiremockserver} to have been called using method {0}{reason}, but no calls were made.",
|
||||||
method
|
method
|
||||||
@@ -230,12 +241,6 @@ public class WireMockAssertions
|
|||||||
{
|
{
|
||||||
Func<IReadOnlyList<IRequestMessage>, IReadOnlyList<IRequestMessage>> filter = requests => requests.Where(predicate).ToList();
|
Func<IReadOnlyList<IRequestMessage>, IReadOnlyList<IRequestMessage>> filter = requests => requests.Where(predicate).ToList();
|
||||||
|
|
||||||
return
|
return (filter, requests => (_callsCount is null && filter(requests).Any()) || _callsCount == filter(requests).Count);
|
||||||
(
|
|
||||||
filter,
|
|
||||||
requests =>
|
|
||||||
(_callsCount == null && filter(_requestMessages).Any()) ||
|
|
||||||
(_callsCount == filter(_requestMessages).Count())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,6 @@ using WireMock.RegularExpressions;
|
|||||||
using WireMock.Types;
|
using WireMock.Types;
|
||||||
#if USE_ASPNETCORE
|
#if USE_ASPNETCORE
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using WireMock.Types;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace WireMock.Settings
|
namespace WireMock.Settings
|
||||||
|
|||||||
@@ -562,6 +562,44 @@ public class WireMockAssertionsTests : IDisposable
|
|||||||
.UsingTrace();
|
.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()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_server?.Stop();
|
_server?.Stop();
|
||||||
|
|||||||
Reference in New Issue
Block a user