mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-06-09 08:22:47 +02:00
Add WithoutHeader to WireMock.FluentAssertions (#1049)
* Add WithoutHeader to WireMock.FluentAssertions * . * .
This commit is contained in:
@@ -125,20 +125,27 @@ public partial class WireMockAssertions
|
|||||||
}
|
}
|
||||||
|
|
||||||
[CustomAssertion]
|
[CustomAssertion]
|
||||||
public AndConstraint<WireMockAssertions> WithHeader(string expectedKey, string value, string because = "", params object[] becauseArgs)
|
public AndConstraint<WireMockAssertions> WitHeaderKey(string expectedKey, 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("headers from requests sent"))
|
using (new AssertionScope("headers from requests sent"))
|
||||||
{
|
{
|
||||||
_headers.Select(h => h.Key).Should().Contain(expectedKey, because, becauseArgs);
|
_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)"))
|
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();
|
var matchingHeaderValues = _headers.Where(h => h.Key == expectedKey).SelectMany(h => h.Value.ToArray())
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
if (expectedValues.Length == 1)
|
if (expectedValues.Length == 1)
|
||||||
{
|
{
|
||||||
@@ -157,6 +164,45 @@ public partial class WireMockAssertions
|
|||||||
return new AndConstraint<WireMockAssertions>(this);
|
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().NotContain(unexpectedKey, because, becauseArgs);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 == unexpectedKey).SelectMany(h => h.Value.ToArray()).ToArray();
|
||||||
|
|
||||||
|
if (expectedValues.Length == 1)
|
||||||
|
{
|
||||||
|
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().NotContain(expectedValue, because, becauseArgs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new AndConstraint<WireMockAssertions>(this);
|
||||||
|
}
|
||||||
|
|
||||||
private (Func<IReadOnlyList<IRequestMessage>, IReadOnlyList<IRequestMessage>> Filter, Func<IReadOnlyList<IRequestMessage>, bool> Condition) BuildFilterAndCondition(Func<IRequestMessage, bool> predicate)
|
private (Func<IReadOnlyList<IRequestMessage>, IReadOnlyList<IRequestMessage>> Filter, Func<IReadOnlyList<IRequestMessage>, bool> Condition) BuildFilterAndCondition(Func<IRequestMessage, bool> predicate)
|
||||||
{
|
{
|
||||||
Func<IReadOnlyList<IRequestMessage>, IReadOnlyList<IRequestMessage>> filter = requests => requests.Where(predicate).ToList();
|
Func<IReadOnlyList<IRequestMessage>, IReadOnlyList<IRequestMessage>> filter = requests => requests.Where(predicate).ToList();
|
||||||
|
|||||||
@@ -130,6 +130,17 @@ public class WireMockAssertionsTests : IDisposable
|
|||||||
_httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer a");
|
_httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer a");
|
||||||
await _httpClient.GetAsync("").ConfigureAwait(false);
|
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()
|
_server.Should()
|
||||||
.HaveReceivedACall()
|
.HaveReceivedACall()
|
||||||
.WithHeader("Authorization", "Bearer a");
|
.WithHeader("Authorization", "Bearer a");
|
||||||
@@ -163,7 +174,7 @@ public class WireMockAssertionsTests : IDisposable
|
|||||||
|
|
||||||
act.Should().Throw<Exception>()
|
act.Should().Throw<Exception>()
|
||||||
.And.Message.Should()
|
.And.Message.Should()
|
||||||
.Contain("to contain \"Authorization\".");
|
.Contain("\"Authorization\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -240,8 +251,13 @@ public class WireMockAssertionsTests : IDisposable
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
server.Should().HaveReceivedACall().WithHeader("Authorization", "Bearer invalidToken");
|
server.Should()
|
||||||
server.Should().HaveReceivedACall().WithHeader("Authorization", "Bearer validToken");
|
.HaveReceivedACall()
|
||||||
|
.WithHeader("Authorization", "Bearer invalidToken").And.WithoutHeader("x", "y").And.WithoutHeaderKey("a");
|
||||||
|
|
||||||
|
server.Should().
|
||||||
|
HaveReceivedACall()
|
||||||
|
.WithHeader("Authorization", "Bearer validToken").And.WithoutHeader("Authorization", "y");
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
Reference in New Issue
Block a user