Skip to content

Commit

Permalink
Add IsAny property to value.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnperfors committed Oct 14, 2024
1 parent b8691d2 commit 3c23e88
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/TestableHttpClient/TestableHttpClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Perfors.UnreachableException" Version="1.0.0" />
<PackageReference Include="PolyKit.Embedded" Version="3.0.9" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 6 additions & 4 deletions src/TestableHttpClient/Utils/HttpRequestMessagePattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ internal sealed class HttpRequestMessagePattern
public Dictionary<Value<string>, Value<string>> Headers { get; init; } = new() { [Value.Any<string>()] = Value.Any<string>() };
public Value<string> Content { get; init; } = Value.Any<string>();

public HttpRequestMessagePatternMatchingResult Matches(HttpRequestMessage httpRequestMessage, HttpRequestMessagePatternMatchingOptions options) =>
internal HttpRequestMessagePatternMatchingResult Matches(HttpRequestMessage httpRequestMessage, HttpRequestMessagePatternMatchingOptions options) =>
new()
{
RequestMessage = httpRequestMessage,
Method = Method.Matches(httpRequestMessage.Method, false),
RequestUri = RequestUri.Matches(httpRequestMessage.RequestUri, options.RequestUriMatchingOptions),
Version = Version.Matches(httpRequestMessage.Version, false),
Expand Down Expand Up @@ -54,16 +55,16 @@ private bool MatchesHeaders(Dictionary<string, string> requestHeaders)
return true;
}

private bool IsAnyHeader(KeyValuePair<Value<string>, Value<string>> header)
private static bool IsAnyHeader(KeyValuePair<Value<string>, Value<string>> header)
{
return header.Key == Value.Any<string>() && header.Value == Value.Any<string>();
return header.Key.IsAny && header.Value.IsAny;
}

private bool MatchesContent(HttpContent? requestContent)
{
if (requestContent == null)
{
return Content == Value.Any<string>();
return Content.IsAny;
}

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

internal sealed class HttpRequestMessagePatternMatchingResult
{
public required HttpRequestMessage RequestMessage { get; init; }
public bool Method { get; init; }
public bool RequestUri { get; init; }
public bool Version { get; init; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public HttpRequestMessagePatternMatchResult Match(IEnumerable<HttpRequestMessage
}
else
{
result.UnmatchedRequests.Add(request);
result.UnmatchedRequests.Add(match);
}
}
return result;
}
}

internal class HttpRequestMessagePatternMatchResult
internal sealed class HttpRequestMessagePatternMatchResult
{
public List<HttpRequestMessage> MatchedRequests { get; } = new List<HttpRequestMessage>();
public List<HttpRequestMessage> UnmatchedRequests { get; } = new List<HttpRequestMessage>();
public List<HttpRequestMessagePatternMatchingResult> UnmatchedRequests { get; } = new List<HttpRequestMessagePatternMatchingResult>();
}
2 changes: 2 additions & 0 deletions src/TestableHttpClient/Utils/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ internal abstract record Value

internal abstract record Value<T>
{
public virtual bool IsAny => false;
internal abstract bool Matches(T value, bool ignoreCase);
}

[DebuggerDisplay("Any value")]
file sealed record AnyValue<T> : Value<T>
{
public override bool IsAny => true;
internal override bool Matches(T value, bool ignoreCase) => true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ public class HttpRequestMessagePatternMatcherTests
[Fact]
public void MultipleRequests_MatchingAnyPattern_ReturnsAllRequests()
{
HttpRequestMessage[] requestMessages = [
new HttpRequestMessage(HttpMethod.Get, "https://localhost/get"),
new HttpRequestMessage(HttpMethod.Post, "https://localhost/post"),
new HttpRequestMessage(HttpMethod.Options, "https://localhost/options")
];
using HttpRequestMessage getRequest = new(HttpMethod.Get, "https://localhost/get");
using HttpRequestMessage postRequest = new(HttpMethod.Post, "https://localhost/post");
using HttpRequestMessage optionsRequest = new(HttpMethod.Options, "https://localhost/options");
HttpRequestMessage[] requestMessages = [getRequest, postRequest, optionsRequest];

HttpRequestMessagePattern pattern = new();

Expand All @@ -28,9 +27,9 @@ public void MultipleRequests_MatchingAnyPattern_ReturnsAllRequests()
[Fact]
public void MultipleRequests_MatchingSpecificHttpMethod_ReturnsMatchinRequests()
{
HttpRequestMessage getRequest = new(HttpMethod.Get, "https://localhost/get");
HttpRequestMessage postRequest = new(HttpMethod.Post, "https://localhost/post");
HttpRequestMessage optionsRequest = new(HttpMethod.Options, "https://localhost/options");
using HttpRequestMessage getRequest = new(HttpMethod.Get, "https://localhost/get");
using HttpRequestMessage postRequest = new(HttpMethod.Post, "https://localhost/post");
using HttpRequestMessage optionsRequest = new(HttpMethod.Options, "https://localhost/options");
HttpRequestMessage[] requestMessages = [ getRequest, postRequest, optionsRequest ];

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

result.MatchedRequests.Should().BeEquivalentTo([postRequest]);
result.UnmatchedRequests.Should().BeEquivalentTo([getRequest, optionsRequest]);
result.UnmatchedRequests.Should().BeEquivalentTo([
new HttpRequestMessagePatternMatchingResult{
RequestMessage = getRequest,
Method = false,
RequestUri = true,
Version = true,
Headers = true,
Content = true
}, new HttpRequestMessagePatternMatchingResult{
RequestMessage = optionsRequest,
Method = false,
RequestUri = true,
Version = true,
Headers = true,
Content = true
}]);
}
}

0 comments on commit 3c23e88

Please sign in to comment.