Fix FormUrlEncodedMatcher (+ refactor values / matchers logic) (#1503)

* Fix FormUrlEncodedMatcher

* tests

* Apply suggestions from code review

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix comments

---------

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Stef Heyenrath
2026-08-20 17:31:16 +02:00
committed by GitHub
co-authored by Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com> Copilot Autofix powered by AI
parent 2b165eb6a1
commit ee0f890795
17 changed files with 598 additions and 142 deletions
@@ -1,9 +1,8 @@
// Copyright © WireMock.Net
using System.Net.Http;
using AnyOfTypes;
using WireMock.Extensions;
using WireMock.Matchers;
using WireMock.Models;
namespace WireMock.Net.Tests.Matchers;
@@ -12,59 +11,155 @@ public class FormUrlEncodedMatcherTest
private readonly CancellationToken _ct = TestContext.Current.CancellationToken;
[Theory]
[InlineData("*=*")]
[InlineData("name=John Doe")]
[InlineData("name=*")]
[InlineData("*=John Doe")]
[InlineData("email=johndoe@example.com")]
[InlineData("email=*")]
[InlineData("*=johndoe@example.com")]
[InlineData("name=John Doe", "email=johndoe@example.com")]
[InlineData("name=John Doe", "email=*")]
[InlineData("name=*", "email=*")]
[InlineData("*=John Doe", "*=johndoe@example.com")]
public async Task FormUrlEncodedMatcher_IsMatch(params string[] patterns)
[InlineData(true, "*=*")]
[InlineData(true, "name=John Doe")]
[InlineData(false, "name=Stef")]
[InlineData(false, "name=John Doe&name=Stef")]
[InlineData(true, "name=*")]
[InlineData(true, "*=John Doe")]
[InlineData(false, "*=Stef")]
[InlineData(false, "*=John Doe&*=Stef")]
[InlineData(true, "email=johndoe@example.com")]
[InlineData(true, "email=*")]
[InlineData(true, "*=johndoe@example.com")]
[InlineData(true, "name=John Doe", "email=johndoe@example.com")]
[InlineData(true, "name=John Doe", "email=*")]
[InlineData(true, "name=John Doe&name=Stef", "email=*")]
[InlineData(true, "name=Stef", "email=*")]
[InlineData(true, "name=*", "email=*")]
[InlineData(true, "*=John Doe", "*=johndoe@example.com")]
[InlineData(true, "*=Stef", "*=johndoe@example.com")]
[InlineData(true, "name=John Doe&name=Stef", "*=johndoe@example.com")]
public async Task FormUrlEncodedMatcher_IsMatch_Single_Or(bool expected, params string[] patterns)
{
// Arrange
var content = new FormUrlEncodedContent(
using var content = new FormUrlEncodedContent(
[
new KeyValuePair<string, string>("name", "John Doe"),
new KeyValuePair<string, string>("email", "johndoe@example.com")
]);
var contentAsString = await content.ReadAsStringAsync(_ct);
var matcher = new FormUrlEncodedMatcher(patterns.Select(p => new AnyOf<string, StringPattern>(p)).ToArray());
var matcher = new FormUrlEncodedMatcher(patterns.ToAnyOfPatterns());
// Act
var score = matcher.IsMatch(contentAsString).IsPerfect();
// Assert
score.Should().BeTrue();
score.Should().Be(expected);
}
[Theory]
[InlineData(true, "*=*")]
[InlineData(false, "name=John Doe")]
[InlineData(false, "name=Stef")]
[InlineData(true, "name=John Doe&name=Stef")]
[InlineData(true, "name=*")]
[InlineData(false, "*=John Doe")]
[InlineData(false, "*=Stef")]
[InlineData(true, "*=John Doe&*=Stef")]
[InlineData(true, "email=johndoe@example.com")]
[InlineData(true, "email=*")]
[InlineData(true, "*=johndoe@example.com")]
[InlineData(true, "name=John Doe", "email=johndoe@example.com")]
[InlineData(true, "name=John Doe", "email=*")]
[InlineData(true, "name=John Doe&name=Stef", "email=*")]
[InlineData(true, "name=Stef", "email=*")]
[InlineData(true, "name=*", "email=*")]
[InlineData(true, "*=John Doe", "*=johndoe@example.com")]
[InlineData(true, "*=Stef", "*=johndoe@example.com")]
[InlineData(true, "name=John Doe&name=Stef", "*=johndoe@example.com")]
public async Task FormUrlEncodedMatcher_IsMatch_Multiple_Or(bool expected, params string[] patterns)
{
// Arrange
using var content = new FormUrlEncodedContent(
[
new KeyValuePair<string, string>("name", "John Doe"),
new KeyValuePair<string, string>("name", "Stef"),
new KeyValuePair<string, string>("email", "johndoe@example.com")
]);
var contentAsString = await content.ReadAsStringAsync(_ct);
var matcher = new FormUrlEncodedMatcher(patterns.ToAnyOfPatterns());
// Act
var score = matcher.IsMatch(contentAsString).IsPerfect();
// Assert
score.Should().Be(expected);
}
[Theory]
[InlineData(true, "*=*")]
[InlineData(false, "name=John Doe")]
[InlineData(false, "name=Stef")]
[InlineData(false, "name=John Doe&name=Stef")]
[InlineData(false, "name=*")]
[InlineData(false, "*=John Doe")]
[InlineData(false, "*=Stef")]
[InlineData(false, "*=John Doe&*=Stef")]
[InlineData(false, "email=johndoe@example.com")]
[InlineData(false, "email=*")]
[InlineData(false, "*=johndoe@example.com")]
[InlineData(true, "name=John Doe", "email=johndoe@example.com")]
[InlineData(true, "name=John Doe", "email=*")]
[InlineData(false, "name=John Doe&name=Stef", "email=*")]
[InlineData(false, "name=Stef", "email=*")]
[InlineData(true, "name=*", "email=*")]
[InlineData(true, "*=John Doe", "*=johndoe@example.com")]
[InlineData(true, "*=*")]
public async Task FormUrlEncodedMatcher_IsMatch_And(bool expected, params string[] patterns)
[InlineData(false, "*=Stef", "*=johndoe@example.com")]
[InlineData(false, "name=John Doe&name=Stef", "*=johndoe@example.com")]
public async Task FormUrlEncodedMatcher_IsMatch_Single_And(bool expected, params string[] patterns)
{
// Arrange
var content = new FormUrlEncodedContent(
using var content = new FormUrlEncodedContent(
[
new KeyValuePair<string, string>("name", "John Doe"),
new KeyValuePair<string, string>("email", "johndoe@example.com")
]);
var contentAsString = await content.ReadAsStringAsync(_ct);
var matcher = new FormUrlEncodedMatcher(patterns.Select(p => new AnyOf<string, StringPattern>(p)).ToArray(), true, MatchOperator.And);
var matcher = new FormUrlEncodedMatcher(patterns.ToAnyOfPatterns(), true, MatchOperator.And);
// Act
var score = matcher.IsMatch(contentAsString).IsPerfect();
// Assert
score.Should().Be(expected);
}
[Theory]
[InlineData(true, "*=*")]
[InlineData(false, "name=John Doe")]
[InlineData(false, "name=Stef")]
[InlineData(false, "name=John Doe&name=Stef")]
[InlineData(false, "name=*")]
[InlineData(false, "*=John Doe")]
[InlineData(false, "*=Stef")]
[InlineData(false, "*=John Doe&*=Stef")]
[InlineData(false, "email=johndoe@example.com")]
[InlineData(false, "email=*")]
[InlineData(false, "*=johndoe@example.com")]
[InlineData(false, "name=John Doe", "email=johndoe@example.com")]
[InlineData(false, "name=John Doe", "email=*")]
[InlineData(true, "name=John Doe&name=Stef", "email=*")]
[InlineData(false, "name=Stef", "email=*")]
[InlineData(true, "name=*", "email=*")]
[InlineData(false, "*=John Doe", "*=johndoe@example.com")]
[InlineData(false, "*=Stef", "*=johndoe@example.com")]
[InlineData(true, "name=John Doe&name=Stef", "*=johndoe@example.com")]
public async Task FormUrlEncodedMatcher_IsMatch_Multiple_And(bool expected, params string[] patterns)
{
// Arrange
using var content = new FormUrlEncodedContent(
[
new KeyValuePair<string, string>("name", "John Doe"),
new KeyValuePair<string, string>("name", "Stef"),
new KeyValuePair<string, string>("email", "johndoe@example.com")
]);
var contentAsString = await content.ReadAsStringAsync(_ct);
var matcher = new FormUrlEncodedMatcher(patterns.ToAnyOfPatterns(), true, MatchOperator.And);
// Act
var score = matcher.IsMatch(contentAsString).IsPerfect();
@@ -74,12 +169,13 @@ public class FormUrlEncodedMatcherTest
}
[Fact]
public async Task FormUrlEncodedMatcher_IsMatch_And_MatchAllProperties()
public async Task FormUrlEncodedMatcher_IsMatch_And_MatchAllProperties_MissingRequiredKey_ShouldNotMatch()
{
// Arrange
var content = new FormUrlEncodedContent(
using var content = new FormUrlEncodedContent(
[
new KeyValuePair<string, string>("name", "John Doe"),
new KeyValuePair<string, string>("name", "Stef"),
new KeyValuePair<string, string>("email", "johndoe@example.com")
]);
var contentAsString = await content.ReadAsStringAsync(_ct);
@@ -93,4 +189,25 @@ public class FormUrlEncodedMatcherTest
// Assert
score.Should().BeFalse();
}
[Fact]
public async Task FormUrlEncodedMatcher_IsMatch_And_MatchAllProperties_ConflictingEmailPatterns_ShouldNotMatch()
{
// Arrange
using var content = new FormUrlEncodedContent(
[
new KeyValuePair<string, string>("name", "John Doe"),
new KeyValuePair<string, string>("name", "Stef"),
new KeyValuePair<string, string>("email", "johndoe@example.com")
]);
var contentAsString = await content.ReadAsStringAsync(_ct);
var matcher = new FormUrlEncodedMatcher(["name=*", "email=*", "email=x"], matchOperator: MatchOperator.And);
// Act
var score = matcher.IsMatch(contentAsString).IsPerfect();
// Assert
score.Should().BeFalse();
}
}