-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* FormUrlEncodedMatcher * . * Fix * new * support wildcard
- Loading branch information
Showing
10 changed files
with
530 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// Copyright © WireMock.Net | ||
|
||
using System.Collections.Generic; | ||
using AnyOfTypes; | ||
using Stef.Validation; | ||
using WireMock.Models; | ||
using WireMock.Util; | ||
|
||
namespace WireMock.Matchers; | ||
|
||
/// <summary> | ||
/// FormUrl Encoded fields Matcher | ||
/// </summary> | ||
/// <inheritdoc cref="IStringMatcher"/> | ||
/// <inheritdoc cref="IIgnoreCaseMatcher"/> | ||
public class FormUrlEncodedMatcher : IStringMatcher, IIgnoreCaseMatcher | ||
{ | ||
private readonly AnyOf<string, StringPattern>[] _patterns; | ||
|
||
/// <inheritdoc /> | ||
public MatchBehaviour MatchBehaviour { get; } | ||
|
||
private readonly List<(WildcardMatcher Key, WildcardMatcher? Value)> _pairs = []; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FormUrlEncodedMatcher"/> class. | ||
/// </summary> | ||
/// <param name="pattern">The pattern.</param> | ||
/// <param name="ignoreCase">Ignore the case from the pattern.</param> | ||
/// <param name="matchOperator">The <see cref="Matchers.MatchOperator"/> to use. (default = "Or")</param> | ||
public FormUrlEncodedMatcher( | ||
AnyOf<string, StringPattern> pattern, | ||
bool ignoreCase = false, | ||
MatchOperator matchOperator = MatchOperator.Or) : | ||
this(MatchBehaviour.AcceptOnMatch, [pattern], ignoreCase, matchOperator) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FormUrlEncodedMatcher"/> class. | ||
/// </summary> | ||
/// <param name="matchBehaviour">The match behaviour.</param> | ||
/// <param name="pattern">The pattern.</param> | ||
/// <param name="ignoreCase">Ignore the case from the pattern.</param> | ||
/// <param name="matchOperator">The <see cref="Matchers.MatchOperator"/> to use. (default = "Or")</param> | ||
public FormUrlEncodedMatcher( | ||
MatchBehaviour matchBehaviour, | ||
AnyOf<string, StringPattern> pattern, | ||
bool ignoreCase = false, | ||
MatchOperator matchOperator = MatchOperator.Or) : | ||
this(matchBehaviour, [pattern], ignoreCase, matchOperator) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FormUrlEncodedMatcher"/> class. | ||
/// </summary> | ||
/// <param name="patterns">The patterns.</param> | ||
/// <param name="ignoreCase">Ignore the case from the pattern.</param> | ||
/// <param name="matchOperator">The <see cref="Matchers.MatchOperator"/> to use. (default = "Or")</param> | ||
public FormUrlEncodedMatcher( | ||
AnyOf<string, StringPattern>[] patterns, | ||
bool ignoreCase = false, | ||
MatchOperator matchOperator = MatchOperator.Or) : | ||
this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase, matchOperator) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FormUrlEncodedMatcher"/> class. | ||
/// </summary> | ||
/// <param name="matchBehaviour">The match behaviour.</param> | ||
/// <param name="patterns">The patterns.</param> | ||
/// <param name="ignoreCase">Ignore the case from the pattern.</param> | ||
/// <param name="matchOperator">The <see cref="Matchers.MatchOperator"/> to use. (default = "Or")</param> | ||
public FormUrlEncodedMatcher( | ||
MatchBehaviour matchBehaviour, | ||
AnyOf<string, StringPattern>[] patterns, | ||
bool ignoreCase = false, | ||
MatchOperator matchOperator = MatchOperator.Or) | ||
{ | ||
_patterns = Guard.NotNull(patterns); | ||
IgnoreCase = ignoreCase; | ||
MatchBehaviour = matchBehaviour; | ||
MatchOperator = matchOperator; | ||
|
||
foreach (var pattern in _patterns) | ||
{ | ||
if (QueryStringParser.TryParse(pattern, IgnoreCase, out var nameValueCollection)) | ||
{ | ||
foreach (var nameValue in nameValueCollection) | ||
{ | ||
var keyMatcher = new WildcardMatcher(MatchBehaviour.AcceptOnMatch, [nameValue.Key], ignoreCase, MatchOperator); | ||
var valueMatcher = new WildcardMatcher(MatchBehaviour.AcceptOnMatch, [nameValue.Value], ignoreCase, MatchOperator); | ||
_pairs.Add((keyMatcher, valueMatcher)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public MatchResult IsMatch(string? input) | ||
{ | ||
// Input is null or empty and if no patterns defined, return Perfect match. | ||
if (string.IsNullOrEmpty(input) && _patterns.Length == 0) | ||
{ | ||
return new MatchResult(MatchScores.Perfect); | ||
} | ||
|
||
if (!QueryStringParser.TryParse(input, IgnoreCase, out var inputNameValueCollection)) | ||
{ | ||
return new MatchResult(MatchScores.Mismatch); | ||
} | ||
|
||
var matches = new List<bool>(); | ||
foreach (var inputKeyValuePair in inputNameValueCollection) | ||
{ | ||
var match = false; | ||
foreach (var pair in _pairs) | ||
{ | ||
var keyMatchResult = pair.Key.IsMatch(inputKeyValuePair.Key).IsPerfect(); | ||
if (keyMatchResult) | ||
{ | ||
match = pair.Value?.IsMatch(inputKeyValuePair.Value).IsPerfect() ?? false; | ||
if (match) | ||
{ | ||
break; | ||
} | ||
} | ||
} | ||
|
||
matches.Add(match); | ||
} | ||
|
||
var score = MatchScores.ToScore(matches.ToArray(), MatchOperator); | ||
return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual AnyOf<string, StringPattern>[] GetPatterns() | ||
{ | ||
return _patterns; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public virtual string Name => nameof(FormUrlEncodedMatcher); | ||
|
||
/// <inheritdoc /> | ||
public bool IgnoreCase { get; } | ||
|
||
/// <inheritdoc /> | ||
public MatchOperator MatchOperator { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[ | ||
{ | ||
Guid: Guid_1, | ||
Guid: 41372914-1838-4c67-916b-b9aacdd096ce, | ||
UpdatedAt: 2023-01-14 15:16:17, | ||
Request: { | ||
Path: { | ||
|
@@ -33,7 +33,7 @@ | |
} | ||
}, | ||
{ | ||
Guid: Guid_2, | ||
Guid: 98fae52e-76df-47d9-876f-2ee32e931002, | ||
UpdatedAt: 2023-01-14 15:16:17, | ||
Request: { | ||
Path: { | ||
|
@@ -61,5 +61,77 @@ | |
} | ||
}, | ||
Response: {} | ||
}, | ||
{ | ||
Guid: 98fae52e-76df-47d9-876f-2ee32e931003, | ||
UpdatedAt: 2023-01-14 15:16:17, | ||
Request: { | ||
Path: { | ||
Matchers: [ | ||
{ | ||
Name: WildcardMatcher, | ||
Pattern: /form-urlencoded, | ||
IgnoreCase: false | ||
} | ||
] | ||
}, | ||
Methods: [ | ||
POST | ||
], | ||
Headers: [ | ||
{ | ||
Name: Content-Type, | ||
Matchers: [ | ||
{ | ||
Name: WildcardMatcher, | ||
Pattern: application/x-www-form-urlencoded, | ||
IgnoreCase: true | ||
} | ||
], | ||
IgnoreCase: true | ||
} | ||
], | ||
Body: { | ||
Matcher: { | ||
Name: FormUrlEncodedMatcher, | ||
Patterns: [ | ||
name=John Doe, | ||
[email protected] | ||
], | ||
IgnoreCase: false, | ||
MatchOperator: Or | ||
} | ||
} | ||
}, | ||
Response: {} | ||
}, | ||
{ | ||
Guid: 98fae52e-76df-47d9-876f-2ee32e931001, | ||
UpdatedAt: 2023-01-14 15:16:17, | ||
Request: { | ||
Path: { | ||
Matchers: [ | ||
{ | ||
Name: WildcardMatcher, | ||
Pattern: /users/post1, | ||
IgnoreCase: false | ||
} | ||
] | ||
}, | ||
Methods: [ | ||
POST | ||
], | ||
Body: { | ||
Matcher: { | ||
Name: JsonMatcher, | ||
Pattern: { | ||
Request: Hello? | ||
}, | ||
IgnoreCase: false, | ||
Regex: false | ||
} | ||
} | ||
}, | ||
Response: {} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.