Add fluent assertions for headers (#485)

* Add headers assertions

* Update FluentAssertions tests with suggested changes
This commit is contained in:
Mahmoud Ali
2020-07-19 05:09:07 -03:00
committed by GitHub
parent 197f96e303
commit da62a43875
2 changed files with 153 additions and 11 deletions

View File

@@ -8,19 +8,20 @@ namespace WireMock.FluentAssertions
{
public class WireMockAssertions
{
private readonly IWireMockServer _instance;
private readonly IWireMockServer _subject;
public WireMockAssertions(IWireMockServer instance, int? callsCount)
public WireMockAssertions(IWireMockServer subject, int? callsCount)
{
_instance = instance;
_subject = subject;
}
[CustomAssertion]
public AndConstraint<WireMockAssertions> AtAbsoluteUrl(string absoluteUrl, string because = "", params object[] becauseArgs)
public AndConstraint<WireMockAssertions> AtAbsoluteUrl(string absoluteUrl, string because = "",
params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => _instance.LogEntries.Select(x => x.RequestMessage).ToList())
.Given(() => _subject.LogEntries.Select(x => x.RequestMessage).ToList())
.ForCondition(requests => requests.Any())
.FailWith(
"Expected {context:wiremockserver} to have been called at address matching the absolute url {0}{reason}, but no calls were made.",
@@ -33,5 +34,43 @@ namespace WireMock.FluentAssertions
return new AndConstraint<WireMockAssertions>(this);
}
[CustomAssertion]
public AndConstraint<WireMockAssertions> WithHeader(string expectedKey, string value,
string because = "", params object[] becauseArgs)
=> WithHeader(expectedKey, new[] {value}, because, becauseArgs);
[CustomAssertion]
public AndConstraint<WireMockAssertions> WithHeader(string expectedKey, string[] expectedValues,
string because = "", params object[] becauseArgs)
{
var headersDictionary = _subject.LogEntries.SelectMany(x => x.RequestMessage.Headers)
.ToDictionary(x => x.Key, x => x.Value);
using (new AssertionScope("headers from requests sent"))
{
headersDictionary.Should().ContainKey(expectedKey, because, becauseArgs);
}
using (new AssertionScope($"header \"{expectedKey}\" from requests sent with value(s)"))
{
if (expectedValues.Length == 1)
{
headersDictionary[expectedKey].Should().Contain(expectedValues.First());
}
else
{
var trimmedHeaderValues = string.Join(",", headersDictionary[expectedKey].Select(x => x)).Split(',')
.Select(x => x.Trim())
.ToList();
foreach (var expectedValue in expectedValues)
{
trimmedHeaderValues.Should().Contain(expectedValue);
}
}
}
return new AndConstraint<WireMockAssertions>(this);
}
}
}