Skip to content

Commit

Permalink
Make WireMockAssertions extendable
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Mar 9, 2024
1 parent 5b60991 commit 6e88030
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma warning disable CS1591
using System;

// ReSharper disable once CheckNamespace
namespace WireMock.FluentAssertions;

public partial class WireMockAssertions
{
[CustomAssertion]
public AndWhichConstraint<WireMockAssertions, string> AtAbsoluteUrl(string absoluteUrl, string because = "", params object[] becauseArgs)
{
var (filter, condition) = BuildFilterAndCondition(request => string.Equals(request.AbsoluteUrl, absoluteUrl, StringComparison.OrdinalIgnoreCase));

Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => RequestMessages)
.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
)
.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)
);

FilterRequestMessages(filter);

return new AndWhichConstraint<WireMockAssertions, string>(this, absoluteUrl);
}

[CustomAssertion]
public AndWhichConstraint<WireMockAssertions, string> AtUrl(string url, string because = "", params object[] becauseArgs)
{
var (filter, condition) = BuildFilterAndCondition(request => string.Equals(request.Url, url, StringComparison.OrdinalIgnoreCase));

Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => RequestMessages)
.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
)
.Then
.ForCondition(condition)
.FailWith(
"Expected {context:wiremockserver} to have been called at address matching the url {0}{reason}, but didn't find it among the calls to {1}.",
_ => url,
requests => requests.Select(request => request.Url)
);

FilterRequestMessages(filter);

return new AndWhichConstraint<WireMockAssertions, string>(this, url);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma warning disable CS1591
using System;

// ReSharper disable once CheckNamespace
namespace WireMock.FluentAssertions;

public partial class WireMockAssertions
{
[CustomAssertion]
public AndWhichConstraint<WireMockAssertions, string> FromClientIP(string clientIP, string because = "", params object[] becauseArgs)
{
var (filter, condition) = BuildFilterAndCondition(request => string.Equals(request.ClientIP, clientIP, StringComparison.OrdinalIgnoreCase));

Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => RequestMessages)
.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(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)
);

FilterRequestMessages(filter);

return new AndWhichConstraint<WireMockAssertions, string>(this, clientIP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public AndConstraint<WireMockAssertions> UsingMethod(string method, string becau

Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _requestMessages)
.ForCondition(requests => _callsCount == 0 || requests.Any())
.Given(() => RequestMessages)
.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 @@ -72,7 +72,7 @@ public AndConstraint<WireMockAssertions> UsingMethod(string method, string becau
requests => requests.Select(request => request.Method)
);

_requestMessages = filter(_requestMessages).ToList();
FilterRequestMessages(filter);

return new AndConstraint<WireMockAssertions>(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ private AndConstraint<WireMockAssertions> ExecuteAssertionWithBodyStringMatcher(
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _requestMessages)
.ForCondition(requests => _callsCount == 0 || requests.Any())
.Given(() => RequestMessages)
.ForCondition(requests => CallsCount == 0 || requests.Any())
.FailWith(
MessageFormatNoCalls,
matcher.GetPatterns()
Expand All @@ -84,7 +84,7 @@ private AndConstraint<WireMockAssertions> ExecuteAssertionWithBodyStringMatcher(
requests => requests.Select(expression)
);

_requestMessages = filter(_requestMessages).ToList();
FilterRequestMessages(filter);

return new AndConstraint<WireMockAssertions>(this);
}
Expand All @@ -100,8 +100,8 @@ private AndConstraint<WireMockAssertions> ExecuteAssertionWithBodyAsIObjectMatch
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _requestMessages)
.ForCondition(requests => _callsCount == 0 || requests.Any())
.Given(() => RequestMessages)
.ForCondition(requests => CallsCount == 0 || requests.Any())
.FailWith(
MessageFormatNoCalls,
matcher.Value
Expand All @@ -114,7 +114,7 @@ private AndConstraint<WireMockAssertions> ExecuteAssertionWithBodyAsIObjectMatch
requests => requests.Select(expression)
);

_requestMessages = filter(_requestMessages).ToList();
FilterRequestMessages(filter);

return new AndConstraint<WireMockAssertions>(this);
}
Expand All @@ -130,8 +130,8 @@ private AndConstraint<WireMockAssertions> ExecuteAssertionWithBodyAsBytesExactOb
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _requestMessages)
.ForCondition(requests => _callsCount == 0 || requests.Any())
.Given(() => RequestMessages)
.ForCondition(requests => CallsCount == 0 || requests.Any())
.FailWith(
MessageFormatNoCalls,
matcher.Value
Expand All @@ -144,7 +144,7 @@ private AndConstraint<WireMockAssertions> ExecuteAssertionWithBodyAsBytesExactOb
requests => requests.Select(expression)
);

_requestMessages = filter(_requestMessages).ToList();
FilterRequestMessages(filter);

return new AndConstraint<WireMockAssertions>(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace WireMock.FluentAssertions;
public partial class WireMockAssertions
{
[CustomAssertion]
public AndConstraint<WireMockAssertions> WitHeaderKey(string expectedKey, string because = "", params object[] becauseArgs)
public AndWhichConstraint<WireMockAssertions, string> WitHeaderKey(string expectedKey, string because = "", params object[] becauseArgs)
{
var (filter, condition) = BuildFilterAndCondition(request =>
{
Expand All @@ -17,8 +17,8 @@ public AndConstraint<WireMockAssertions> WitHeaderKey(string expectedKey, string

Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _requestMessages)
.ForCondition(requests => _callsCount == 0 || requests.Any())
.Given(() => RequestMessages)
.ForCondition(requests => CallsCount == 0 || requests.Any())
.FailWith(
"Expected {context:wiremockserver} to have been called with Header {0}{reason}.",
expectedKey
Expand All @@ -31,9 +31,9 @@ public AndConstraint<WireMockAssertions> WitHeaderKey(string expectedKey, string
requests => requests.Select(request => request.Headers)
);

_requestMessages = filter(_requestMessages).ToList();
FilterRequestMessages(filter);

return new AndConstraint<WireMockAssertions>(this);
return new AndWhichConstraint<WireMockAssertions, string>(this, expectedKey);
}

[CustomAssertion]
Expand All @@ -60,8 +60,8 @@ public AndConstraint<WireMockAssertions> WithHeader(string expectedKey, string[]

Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _requestMessages)
.ForCondition(requests => _callsCount == 0 || requests.Any())
.Given(() => RequestMessages)
.ForCondition(requests => CallsCount == 0 || requests.Any())
.FailWith(
"Expected {context:wiremockserver} to have been called with Header {0} and Values {1}{reason}.",
expectedKey,
Expand All @@ -76,7 +76,7 @@ public AndConstraint<WireMockAssertions> WithHeader(string expectedKey, string[]
requests => requests.Select(request => request.Headers)
);

_requestMessages = filter(_requestMessages).ToList();
FilterRequestMessages(filter);

return new AndConstraint<WireMockAssertions>(this);
}
Expand All @@ -91,8 +91,8 @@ public AndConstraint<WireMockAssertions> WithoutHeaderKey(string unexpectedKey,

Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _requestMessages)
.ForCondition(requests => _callsCount == 0 || requests.Any())
.Given(() => RequestMessages)
.ForCondition(requests => CallsCount == 0 || requests.Any())
.FailWith(
"Expected {context:wiremockserver} not to have been called with Header {0}{reason}.",
unexpectedKey
Expand All @@ -105,7 +105,7 @@ public AndConstraint<WireMockAssertions> WithoutHeaderKey(string unexpectedKey,
requests => requests.Select(request => request.Headers)
);

_requestMessages = filter(_requestMessages).ToList();
FilterRequestMessages(filter);

return new AndConstraint<WireMockAssertions>(this);
}
Expand Down Expand Up @@ -134,8 +134,8 @@ public AndConstraint<WireMockAssertions> WithoutHeader(string unexpectedKey, str

Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _requestMessages)
.ForCondition(requests => _callsCount == 0 || requests.Any())
.Given(() => RequestMessages)
.ForCondition(requests => CallsCount == 0 || requests.Any())
.FailWith(
"Expected {context:wiremockserver} not to have been called with Header {0} and Values {1}{reason}.",
unexpectedKey,
Expand All @@ -150,7 +150,7 @@ public AndConstraint<WireMockAssertions> WithoutHeader(string unexpectedKey, str
requests => requests.Select(request => request.Headers)
);

_requestMessages = filter(_requestMessages).ToList();
FilterRequestMessages(filter);

return new AndConstraint<WireMockAssertions>(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma warning disable CS1591
using System;

// ReSharper disable once CheckNamespace
namespace WireMock.FluentAssertions;

public partial class WireMockAssertions
{
[CustomAssertion]
public AndWhichConstraint<WireMockAssertions, string> WithProxyUrl(string proxyUrl, string because = "", params object[] becauseArgs)
{
var (filter, condition) = BuildFilterAndCondition(request => string.Equals(request.ProxyUrl, proxyUrl, StringComparison.OrdinalIgnoreCase));

Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => RequestMessages)
.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
)
.Then
.ForCondition(condition)
.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)
);

FilterRequestMessages(filter);

return new AndWhichConstraint<WireMockAssertions, string>(this, proxyUrl);
}
}
Loading

0 comments on commit 6e88030

Please sign in to comment.