Skip to content

Commit 7d12ab4

Browse files
committed
Add logic to match AnyValue headers when no headers are given.
1 parent f072480 commit 7d12ab4

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/TestableHttpClient/Utils/HttpRequestMessagePattern.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private bool MatchesHeaders(Dictionary<string, string> requestHeaders)
3838

3939
foreach (var header in Headers)
4040
{
41-
bool matched = false;
41+
bool matched = IsAnyHeader(header);
4242

4343
foreach (var requestHeader in requestHeaders)
4444
{
@@ -54,6 +54,11 @@ private bool MatchesHeaders(Dictionary<string, string> requestHeaders)
5454
return true;
5555
}
5656

57+
private bool IsAnyHeader(KeyValuePair<Value<string>, Value<string>> header)
58+
{
59+
return header.Key == Value.Any<string>() && header.Value == Value.Any<string>();
60+
}
61+
5762
private bool MatchesContent(HttpContent? requestContent)
5863
{
5964
if (requestContent == null)
@@ -79,5 +84,7 @@ internal sealed class HttpRequestMessagePatternMatchingResult
7984
public bool Version { get; init; }
8085
public bool Headers { get; init; }
8186
public bool Content { get; init; }
87+
88+
public bool All => Method && RequestUri && Version && Headers && Content;
8289
}
8390

test/TestableHttpClient.Tests/Utils/HttpRequestMessagePatternTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ public void Matches_HttpRequestMessageWithHeaders_DoesNotMatchNoHeaders()
157157
Assert.False(sut.Matches(matchingRequest, defaultOptions).Headers);
158158
}
159159

160+
[Fact]
161+
public void Matches_HttpRequestMessageWithoutHeaders_DoesMatchAnyHeaders()
162+
{
163+
HttpRequestMessagePattern sut = new();
164+
165+
using HttpRequestMessage matchingRequest = new();
166+
167+
Assert.True(sut.Matches(matchingRequest, defaultOptions).Headers);
168+
}
169+
160170
[Fact]
161171
public void Matches_HttpRequestMessageWithHeaders_DoesMatchAnyHeaders()
162172
{

0 commit comments

Comments
 (0)