Skip to content

Commit

Permalink
Add WithoutHeader to WireMock.FluentAssertions (#1049)
Browse files Browse the repository at this point in the history
* Add WithoutHeader to WireMock.FluentAssertions

* .

* .
  • Loading branch information
StefH authored Dec 27, 2023
1 parent 30ee768 commit 4020a17
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
56 changes: 51 additions & 5 deletions src/WireMock.Net.FluentAssertions/Assertions/WireMockAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,32 +124,78 @@ public AndWhichConstraint<WireMockAssertions, string> FromClientIP(string client
return new AndWhichConstraint<WireMockAssertions, string>(this, clientIP);
}

[CustomAssertion]
public AndConstraint<WireMockAssertions> WitHeaderKey(string expectedKey, string because = "", params object[] becauseArgs)
{
using (new AssertionScope("headers from requests sent"))
{
_headers.Select(h => h.Key).Should().Contain(expectedKey, because, becauseArgs);
}

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)
{
using (new AssertionScope($"header \"{expectedKey}\" from requests sent with value(s)"))
{
var matchingHeaderValues = _headers.Where(h => h.Key == expectedKey).SelectMany(h => h.Value.ToArray())
.ToArray();

if (expectedValues.Length == 1)
{
matchingHeaderValues.Should().Contain(expectedValues.First(), because, becauseArgs);
}
else
{
var trimmedHeaderValues = string.Join(",", matchingHeaderValues.Select(x => x)).Split(',').Select(x => x.Trim()).ToList();
foreach (var expectedValue in expectedValues)
{
trimmedHeaderValues.Should().Contain(expectedValue, because, becauseArgs);
}
}
}

return new AndConstraint<WireMockAssertions>(this);
}

[CustomAssertion]
public AndConstraint<WireMockAssertions> WithoutHeaderKey(string unexpectedKey, string because = "", params object[] becauseArgs)
{
using (new AssertionScope("headers from requests sent"))
{
_headers.Select(h => h.Key).Should().Contain(expectedKey, because, becauseArgs);
_headers.Select(h => h.Key).Should().NotContain(unexpectedKey, because, becauseArgs);
}

using (new AssertionScope($"header \"{expectedKey}\" from requests sent with value(s)"))
return new AndConstraint<WireMockAssertions>(this);
}

[CustomAssertion]
public AndConstraint<WireMockAssertions> WithoutHeader(string unexpectedKey, string value, string because = "", params object[] becauseArgs)
=> WithoutHeader(unexpectedKey, new[] { value }, because, becauseArgs);

[CustomAssertion]
public AndConstraint<WireMockAssertions> WithoutHeader(string unexpectedKey, string[] expectedValues, string because = "", params object[] becauseArgs)
{
using (new AssertionScope($"header \"{unexpectedKey}\" from requests sent with value(s)"))
{
var matchingHeaderValues = _headers.Where(h => h.Key == expectedKey).SelectMany(h => h.Value.ToArray()).ToArray();
var matchingHeaderValues = _headers.Where(h => h.Key == unexpectedKey).SelectMany(h => h.Value.ToArray()).ToArray();

if (expectedValues.Length == 1)
{
matchingHeaderValues.Should().Contain(expectedValues.First(), because, becauseArgs);
matchingHeaderValues.Should().NotContain(expectedValues.First(), because, becauseArgs);
}
else
{
var trimmedHeaderValues = string.Join(",", matchingHeaderValues.Select(x => x)).Split(',').Select(x => x.Trim()).ToList();
foreach (var expectedValue in expectedValues)
{
trimmedHeaderValues.Should().Contain(expectedValue, because, becauseArgs);
trimmedHeaderValues.Should().NotContain(expectedValue, because, becauseArgs);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ public async Task HaveReceivedACall_WithHeader_WhenACallWasMadeWithExpectedHeade
_httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer a");
await _httpClient.GetAsync("").ConfigureAwait(false);

_server.Should()
.HaveReceivedACall()
.WitHeaderKey("Authorization");
}

[Fact]
public async Task HaveReceivedACall_WithHeader_WhenACallWasMadeWithExpectedHeaderWithValue_Should_BeOK()
{
_httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer a");
await _httpClient.GetAsync("").ConfigureAwait(false);

_server.Should()
.HaveReceivedACall()
.WithHeader("Authorization", "Bearer a");
Expand Down Expand Up @@ -163,7 +174,7 @@ public async Task HaveReceivedACall_WithHeader_Should_ThrowWhenNoCallsMatchingTh

act.Should().Throw<Exception>()
.And.Message.Should()
.Contain("to contain \"Authorization\".");
.Contain("\"Authorization\"");
}

[Fact]
Expand Down Expand Up @@ -240,8 +251,13 @@ await client2.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/")
});

// Assert
server.Should().HaveReceivedACall().WithHeader("Authorization", "Bearer invalidToken");
server.Should().HaveReceivedACall().WithHeader("Authorization", "Bearer validToken");
server.Should()
.HaveReceivedACall()
.WithHeader("Authorization", "Bearer invalidToken").And.WithoutHeader("x", "y").And.WithoutHeaderKey("a");

server.Should().
HaveReceivedACall()
.WithHeader("Authorization", "Bearer validToken").And.WithoutHeader("Authorization", "y");
}

[Fact]
Expand Down

0 comments on commit 4020a17

Please sign in to comment.