Skip to content

Commit c0a391a

Browse files
committed
Add IsAny property to value.
1 parent b1cf4df commit c0a391a

File tree

5 files changed

+42
-16
lines changed

5 files changed

+42
-16
lines changed

src/TestableHttpClient/TestableHttpClient.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@
3939
<PackageReference Include="PolyKit.Embedded" Version="3.0.9" />
4040
</ItemGroup>
4141

42+
<<<<<<< HEAD
43+
=======
44+
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
45+
<PackageReference Include="Perfors.UnreachableException" Version="1.0.0" />
46+
<PackageReference Include="PolyKit.Embedded" Version="3.0.9" />
47+
</ItemGroup>
48+
49+
>>>>>>> b3a5f8e (Add IsAny property to value.)
4250
<ItemGroup>
4351
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
4452
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4" PrivateAssets="All">

src/TestableHttpClient/Utils/HttpRequestMessagePattern.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ internal sealed class HttpRequestMessagePattern
88
public Dictionary<Value<string>, Value<string>> Headers { get; init; } = new() { [Value.Any<string>()] = Value.Any<string>() };
99
public Value<string> Content { get; init; } = Value.Any<string>();
1010

11-
public HttpRequestMessagePatternMatchingResult Matches(HttpRequestMessage httpRequestMessage, HttpRequestMessagePatternMatchingOptions options) =>
11+
internal HttpRequestMessagePatternMatchingResult Matches(HttpRequestMessage httpRequestMessage, HttpRequestMessagePatternMatchingOptions options) =>
1212
new()
1313
{
14+
RequestMessage = httpRequestMessage,
1415
Method = Method.Matches(httpRequestMessage.Method, false),
1516
RequestUri = RequestUri.Matches(httpRequestMessage.RequestUri, options.RequestUriMatchingOptions),
1617
Version = Version.Matches(httpRequestMessage.Version, false),
@@ -54,16 +55,16 @@ private bool MatchesHeaders(Dictionary<string, string> requestHeaders)
5455
return true;
5556
}
5657

57-
private bool IsAnyHeader(KeyValuePair<Value<string>, Value<string>> header)
58+
private static bool IsAnyHeader(KeyValuePair<Value<string>, Value<string>> header)
5859
{
59-
return header.Key == Value.Any<string>() && header.Value == Value.Any<string>();
60+
return header.Key.IsAny && header.Value.IsAny;
6061
}
6162

6263
private bool MatchesContent(HttpContent? requestContent)
6364
{
6465
if (requestContent == null)
6566
{
66-
return Content == Value.Any<string>();
67+
return Content.IsAny;
6768
}
6869

6970
var contentValue = requestContent.ReadAsStringAsync().Result;
@@ -79,6 +80,7 @@ internal sealed class HttpRequestMessagePatternMatchingOptions
7980

8081
internal sealed class HttpRequestMessagePatternMatchingResult
8182
{
83+
public required HttpRequestMessage RequestMessage { get; init; }
8284
public bool Method { get; init; }
8385
public bool RequestUri { get; init; }
8486
public bool Version { get; init; }

src/TestableHttpClient/Utils/HttpRequestMessagePatternMatcher.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ public HttpRequestMessagePatternMatchResult Match(IEnumerable<HttpRequestMessage
1616
}
1717
else
1818
{
19-
result.UnmatchedRequests.Add(request);
19+
result.UnmatchedRequests.Add(match);
2020
}
2121
}
2222
return result;
2323
}
2424
}
2525

26-
internal class HttpRequestMessagePatternMatchResult
26+
internal sealed class HttpRequestMessagePatternMatchResult
2727
{
2828
public List<HttpRequestMessage> MatchedRequests { get; } = new List<HttpRequestMessage>();
29-
public List<HttpRequestMessage> UnmatchedRequests { get; } = new List<HttpRequestMessage>();
29+
public List<HttpRequestMessagePatternMatchingResult> UnmatchedRequests { get; } = new List<HttpRequestMessagePatternMatchingResult>();
3030
}

src/TestableHttpClient/Utils/Value.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ internal abstract record Value
1515

1616
internal abstract record Value<T>
1717
{
18+
public virtual bool IsAny => false;
1819
internal abstract bool Matches(T value, bool ignoreCase);
1920
}
2021

2122
[DebuggerDisplay("Any value")]
2223
file sealed record AnyValue<T> : Value<T>
2324
{
25+
public override bool IsAny => true;
2426
internal override bool Matches(T value, bool ignoreCase) => true;
2527
}
2628

test/TestableHttpClient.Tests/Utils/HttpRequestMessagePatternMatcherTests.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ public class HttpRequestMessagePatternMatcherTests
99
[Fact]
1010
public void MultipleRequests_MatchingAnyPattern_ReturnsAllRequests()
1111
{
12-
HttpRequestMessage[] requestMessages = [
13-
new HttpRequestMessage(HttpMethod.Get, "https://localhost/get"),
14-
new HttpRequestMessage(HttpMethod.Post, "https://localhost/post"),
15-
new HttpRequestMessage(HttpMethod.Options, "https://localhost/options")
16-
];
12+
using HttpRequestMessage getRequest = new(HttpMethod.Get, "https://localhost/get");
13+
using HttpRequestMessage postRequest = new(HttpMethod.Post, "https://localhost/post");
14+
using HttpRequestMessage optionsRequest = new(HttpMethod.Options, "https://localhost/options");
15+
HttpRequestMessage[] requestMessages = [getRequest, postRequest, optionsRequest];
1716

1817
HttpRequestMessagePattern pattern = new();
1918

@@ -28,9 +27,9 @@ public void MultipleRequests_MatchingAnyPattern_ReturnsAllRequests()
2827
[Fact]
2928
public void MultipleRequests_MatchingSpecificHttpMethod_ReturnsMatchinRequests()
3029
{
31-
HttpRequestMessage getRequest = new(HttpMethod.Get, "https://localhost/get");
32-
HttpRequestMessage postRequest = new(HttpMethod.Post, "https://localhost/post");
33-
HttpRequestMessage optionsRequest = new(HttpMethod.Options, "https://localhost/options");
30+
using HttpRequestMessage getRequest = new(HttpMethod.Get, "https://localhost/get");
31+
using HttpRequestMessage postRequest = new(HttpMethod.Post, "https://localhost/post");
32+
using HttpRequestMessage optionsRequest = new(HttpMethod.Options, "https://localhost/options");
3433
HttpRequestMessage[] requestMessages = [ getRequest, postRequest, optionsRequest ];
3534

3635
HttpRequestMessagePattern pattern = new()
@@ -43,6 +42,21 @@ public void MultipleRequests_MatchingSpecificHttpMethod_ReturnsMatchinRequests()
4342
HttpRequestMessagePatternMatchResult result = matcher.Match(requestMessages, pattern);
4443

4544
result.MatchedRequests.Should().BeEquivalentTo([postRequest]);
46-
result.UnmatchedRequests.Should().BeEquivalentTo([getRequest, optionsRequest]);
45+
result.UnmatchedRequests.Should().BeEquivalentTo([
46+
new HttpRequestMessagePatternMatchingResult{
47+
RequestMessage = getRequest,
48+
Method = false,
49+
RequestUri = true,
50+
Version = true,
51+
Headers = true,
52+
Content = true
53+
}, new HttpRequestMessagePatternMatchingResult{
54+
RequestMessage = optionsRequest,
55+
Method = false,
56+
RequestUri = true,
57+
Version = true,
58+
Headers = true,
59+
Content = true
60+
}]);
4761
}
4862
}

0 commit comments

Comments
 (0)