mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 16:23:42 +01:00
JsonPartialMatcher - support Regex (#771)
* JsonPartialMatcher - support Regex * . * . * more tests * . * .
This commit is contained in:
@@ -1,48 +1,52 @@
|
||||
namespace WireMock.Admin.Mappings
|
||||
namespace WireMock.Admin.Mappings;
|
||||
|
||||
/// <summary>
|
||||
/// MatcherModel
|
||||
/// </summary>
|
||||
[FluentBuilder.AutoGenerateBuilder]
|
||||
public class MatcherModel
|
||||
{
|
||||
/// <summary>
|
||||
/// MatcherModel
|
||||
/// Gets or sets the name.
|
||||
/// </summary>
|
||||
[FluentBuilder.AutoGenerateBuilder]
|
||||
public class MatcherModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the name.
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
public string Name { get; set; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the pattern. Can be a string (default) or an object.
|
||||
/// </summary>
|
||||
public object? Pattern { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the pattern. Can be a string (default) or an object.
|
||||
/// </summary>
|
||||
public object? Pattern { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the patterns. Can be array of strings (default) or an array of objects.
|
||||
/// </summary>
|
||||
public object[]? Patterns { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the patterns. Can be array of strings (default) or an array of objects.
|
||||
/// </summary>
|
||||
public object[]? Patterns { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the pattern as a file.
|
||||
/// </summary>
|
||||
public string? PatternAsFile { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the pattern as a file.
|
||||
/// </summary>
|
||||
public string? PatternAsFile { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the ignore case.
|
||||
/// </summary>
|
||||
public bool? IgnoreCase { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the ignore case.
|
||||
/// </summary>
|
||||
public bool? IgnoreCase { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Reject on match.
|
||||
/// </summary>
|
||||
public bool? RejectOnMatch { get; set; }
|
||||
/// <summary>
|
||||
/// Reject on match.
|
||||
/// </summary>
|
||||
public bool? RejectOnMatch { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The Operator to use when multiple patterns are defined. Optional.
|
||||
/// - null = Same as "or".
|
||||
/// - "or" = Only one pattern should match.
|
||||
/// - "and" = All patterns should match.
|
||||
/// - "average" = The average value from all patterns.
|
||||
/// </summary>
|
||||
public string? MatchOperator { get; set; }
|
||||
}
|
||||
/// <summary>
|
||||
/// The Operator to use when multiple patterns are defined. Optional.
|
||||
/// - null = Same as "or".
|
||||
/// - "or" = Only one pattern should match.
|
||||
/// - "and" = All patterns should match.
|
||||
/// - "average" = The average value from all patterns.
|
||||
/// </summary>
|
||||
public string? MatchOperator { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Support Regex, only used for JsonPartialMatcher.
|
||||
/// </summary>
|
||||
public bool? Regex { get; set; }
|
||||
}
|
||||
@@ -1,20 +1,17 @@
|
||||
using JetBrains.Annotations;
|
||||
namespace WireMock.Matchers.Request;
|
||||
|
||||
namespace WireMock.Matchers.Request
|
||||
/// <summary>
|
||||
/// The RequestMatcher interface.
|
||||
/// </summary>
|
||||
public interface IRequestMatcher
|
||||
{
|
||||
/// <summary>
|
||||
/// The RequestMatcher interface.
|
||||
/// Determines whether the specified RequestMessage is match.
|
||||
/// </summary>
|
||||
public interface IRequestMatcher
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines whether the specified RequestMessage is match.
|
||||
/// </summary>
|
||||
/// <param name="requestMessage">The RequestMessage.</param>
|
||||
/// <param name="requestMatchResult">The RequestMatchResult.</param>
|
||||
/// <returns>
|
||||
/// A value between 0.0 - 1.0 of the similarity.
|
||||
/// </returns>
|
||||
double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult);
|
||||
}
|
||||
/// <param name="requestMessage">The RequestMessage.</param>
|
||||
/// <param name="requestMatchResult">The RequestMatchResult.</param>
|
||||
/// <returns>
|
||||
/// A value between 0.0 - 1.0 of the similarity.
|
||||
/// </returns>
|
||||
double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Matchers;
|
||||
|
||||
@@ -9,15 +10,22 @@ namespace WireMock.Matchers;
|
||||
/// </summary>
|
||||
public abstract class AbstractJsonPartialMatcher : JsonMatcher
|
||||
{
|
||||
/// <summary>
|
||||
/// Support Regex
|
||||
/// </summary>
|
||||
public bool Regex { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AbstractJsonPartialMatcher"/> class.
|
||||
/// </summary>
|
||||
/// <param name="value">The string value to check for equality.</param>
|
||||
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
||||
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
|
||||
protected AbstractJsonPartialMatcher(string value, bool ignoreCase = false, bool throwException = false)
|
||||
/// <param name="regex">Support Regex.</param>
|
||||
protected AbstractJsonPartialMatcher(string value, bool ignoreCase = false, bool throwException = false, bool regex = false)
|
||||
: base(value, ignoreCase, throwException)
|
||||
{
|
||||
Regex = regex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -26,9 +34,11 @@ public abstract class AbstractJsonPartialMatcher : JsonMatcher
|
||||
/// <param name="value">The object value to check for equality.</param>
|
||||
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
||||
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
|
||||
protected AbstractJsonPartialMatcher(object value, bool ignoreCase = false, bool throwException = false)
|
||||
/// <param name="regex">Support Regex.</param>
|
||||
protected AbstractJsonPartialMatcher(object value, bool ignoreCase = false, bool throwException = false, bool regex = false)
|
||||
: base(value, ignoreCase, throwException)
|
||||
{
|
||||
Regex = regex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -38,9 +48,11 @@ public abstract class AbstractJsonPartialMatcher : JsonMatcher
|
||||
/// <param name="value">The value to check for equality.</param>
|
||||
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
|
||||
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
|
||||
protected AbstractJsonPartialMatcher(MatchBehaviour matchBehaviour, object value, bool ignoreCase = false, bool throwException = false)
|
||||
/// <param name="regex">Support Regex.</param>
|
||||
protected AbstractJsonPartialMatcher(MatchBehaviour matchBehaviour, object value, bool ignoreCase = false, bool throwException = false, bool regex = false)
|
||||
: base(matchBehaviour, value, ignoreCase, throwException)
|
||||
{
|
||||
Regex = regex;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -51,6 +63,17 @@ public abstract class AbstractJsonPartialMatcher : JsonMatcher
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Regex && value.Type == JTokenType.String && input != null)
|
||||
{
|
||||
var valueAsString = value.ToString();
|
||||
|
||||
var (valid, result) = RegexUtils.MatchRegex(valueAsString, input.ToString());
|
||||
if (valid)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
if (input == null || value.Type != input.Type)
|
||||
{
|
||||
return false;
|
||||
|
||||
@@ -13,12 +13,12 @@ namespace WireMock.Matchers;
|
||||
/// </summary>
|
||||
public class JsonMatcher : IValueMatcher, IIgnoreCaseMatcher
|
||||
{
|
||||
/// <inheritdoc cref="IValueMatcher.Value"/>
|
||||
public object Value { get; }
|
||||
|
||||
/// <inheritdoc cref="IMatcher.Name"/>
|
||||
public virtual string Name => "JsonMatcher";
|
||||
|
||||
/// <inheritdoc cref="IValueMatcher.Value"/>
|
||||
public object Value { get; }
|
||||
|
||||
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
|
||||
public MatchBehaviour MatchBehaviour { get; }
|
||||
|
||||
|
||||
@@ -9,20 +9,20 @@ public class JsonPartialMatcher : AbstractJsonPartialMatcher
|
||||
public override string Name => nameof(JsonPartialMatcher);
|
||||
|
||||
/// <inheritdoc />
|
||||
public JsonPartialMatcher(string value, bool ignoreCase = false, bool throwException = false)
|
||||
: base(value, ignoreCase, throwException)
|
||||
public JsonPartialMatcher(string value, bool ignoreCase = false, bool throwException = false, bool regex = false)
|
||||
: base(value, ignoreCase, throwException, regex)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public JsonPartialMatcher(object value, bool ignoreCase = false, bool throwException = false)
|
||||
: base(value, ignoreCase, throwException)
|
||||
public JsonPartialMatcher(object value, bool ignoreCase = false, bool throwException = false, bool regex = false)
|
||||
: base(value, ignoreCase, throwException, regex)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public JsonPartialMatcher(MatchBehaviour matchBehaviour, object value, bool ignoreCase = false, bool throwException = false)
|
||||
: base(matchBehaviour, value, ignoreCase, throwException)
|
||||
public JsonPartialMatcher(MatchBehaviour matchBehaviour, object value, bool ignoreCase = false, bool throwException = false, bool regex = false)
|
||||
: base(matchBehaviour, value, ignoreCase, throwException, regex)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace WireMock.Matchers;
|
||||
|
||||
/// <summary>
|
||||
@@ -11,20 +9,20 @@ public class JsonPartialWildcardMatcher : AbstractJsonPartialMatcher
|
||||
public override string Name => nameof(JsonPartialWildcardMatcher);
|
||||
|
||||
/// <inheritdoc />
|
||||
public JsonPartialWildcardMatcher(string value, bool ignoreCase = false, bool throwException = false)
|
||||
: base(value, ignoreCase, throwException)
|
||||
public JsonPartialWildcardMatcher(string value, bool ignoreCase = false, bool throwException = false, bool regex = false)
|
||||
: base(value, ignoreCase, throwException, regex)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public JsonPartialWildcardMatcher(object value, bool ignoreCase = false, bool throwException = false)
|
||||
: base(value, ignoreCase, throwException)
|
||||
public JsonPartialWildcardMatcher(object value, bool ignoreCase = false, bool throwException = false, bool regex = false)
|
||||
: base(value, ignoreCase, throwException, regex)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public JsonPartialWildcardMatcher(MatchBehaviour matchBehaviour, object value, bool ignoreCase = false, bool throwException = false)
|
||||
: base(matchBehaviour, value, ignoreCase, throwException)
|
||||
public JsonPartialWildcardMatcher(MatchBehaviour matchBehaviour, object value, bool ignoreCase = false, bool throwException = false, bool regex = false)
|
||||
: base(matchBehaviour, value, ignoreCase, throwException, regex)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ public class RequestMessageHeaderMatcher : IRequestMatcher
|
||||
/// <summary>
|
||||
/// The name
|
||||
/// </summary>
|
||||
public string? Name { get; }
|
||||
public string Name { get; }
|
||||
|
||||
/// <value>
|
||||
/// The matchers.
|
||||
@@ -94,6 +94,7 @@ public class RequestMessageHeaderMatcher : IRequestMatcher
|
||||
public RequestMessageHeaderMatcher(params Func<IDictionary<string, string[]>, bool>[] funcs)
|
||||
{
|
||||
Funcs = Guard.NotNull(funcs);
|
||||
Name = string.Empty; // Not used when Func, but set to a non-null valid value.
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -121,7 +122,7 @@ public class RequestMessageHeaderMatcher : IRequestMatcher
|
||||
|
||||
if (Matchers != null)
|
||||
{
|
||||
if (!headers.ContainsKey(Name!))
|
||||
if (!headers.ContainsKey(Name))
|
||||
{
|
||||
return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch);
|
||||
}
|
||||
@@ -129,7 +130,7 @@ public class RequestMessageHeaderMatcher : IRequestMatcher
|
||||
var results = new List<double>();
|
||||
foreach (var matcher in Matchers)
|
||||
{
|
||||
var resultsPerMatcher = headers[Name!].Select(v => matcher.IsMatch(v)).ToArray();
|
||||
var resultsPerMatcher = headers[Name].Select(v => matcher.IsMatch(v)).ToArray();
|
||||
|
||||
results.Add(MatchScores.ToScore(resultsPerMatcher, MatchOperator.And));
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ public class RequestMessageParamMatcher : IRequestMatcher
|
||||
return MatchScores.ToScore(requestMessage.Query != null && Funcs.Any(f => f(requestMessage.Query)));
|
||||
}
|
||||
|
||||
WireMockList<string> valuesPresentInRequestMessage = ((RequestMessage)requestMessage).GetParameter(Key, IgnoreCase ?? false);
|
||||
var valuesPresentInRequestMessage = ((RequestMessage)requestMessage).GetParameter(Key!, IgnoreCase ?? false);
|
||||
if (valuesPresentInRequestMessage == null)
|
||||
{
|
||||
// Key is not present at all, just return Mismatch
|
||||
@@ -102,7 +102,7 @@ public class RequestMessageParamMatcher : IRequestMatcher
|
||||
if (Matchers != null && Matchers.Any())
|
||||
{
|
||||
// Return the score based on Matchers and valuesPresentInRequestMessage
|
||||
return CalculateScore(valuesPresentInRequestMessage);
|
||||
return CalculateScore(Matchers, valuesPresentInRequestMessage);
|
||||
}
|
||||
|
||||
if (Matchers == null || !Matchers.Any())
|
||||
@@ -114,14 +114,14 @@ public class RequestMessageParamMatcher : IRequestMatcher
|
||||
return MatchScores.Mismatch;
|
||||
}
|
||||
|
||||
private double CalculateScore(WireMockList<string> valuesPresentInRequestMessage)
|
||||
private double CalculateScore(IReadOnlyList<IStringMatcher> matchers, WireMockList<string> valuesPresentInRequestMessage)
|
||||
{
|
||||
var total = new List<double>();
|
||||
|
||||
// If the total patterns in all matchers > values in message, use the matcher as base
|
||||
if (Matchers.Sum(m => m.GetPatterns().Length) > valuesPresentInRequestMessage.Count)
|
||||
if (matchers.Sum(m => m.GetPatterns().Length) > valuesPresentInRequestMessage.Count)
|
||||
{
|
||||
foreach (var matcher in Matchers)
|
||||
foreach (var matcher in matchers)
|
||||
{
|
||||
double score = 0d;
|
||||
foreach (string valuePresentInRequestMessage in valuesPresentInRequestMessage)
|
||||
@@ -136,7 +136,7 @@ public class RequestMessageParamMatcher : IRequestMatcher
|
||||
{
|
||||
foreach (string valuePresentInRequestMessage in valuesPresentInRequestMessage)
|
||||
{
|
||||
double score = Matchers.Max(m => m.IsMatch(valuePresentInRequestMessage));
|
||||
double score = matchers.Max(m => m.IsMatch(valuePresentInRequestMessage));
|
||||
total.Add(score);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,88 +3,87 @@ using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using Stef.Validation;
|
||||
|
||||
namespace WireMock.RegularExpressions
|
||||
namespace WireMock.RegularExpressions;
|
||||
|
||||
/// <summary>
|
||||
/// Extension to the <see cref="Regex"/> object, adding support for GUID tokens for matching on.
|
||||
/// </summary>
|
||||
#if !NETSTANDARD1_3
|
||||
[Serializable]
|
||||
#endif
|
||||
internal class RegexExtended : Regex
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension to the <see cref="Regex"/> object, adding support for GUID tokens for matching on.
|
||||
/// </summary>
|
||||
#if !NETSTANDARD1_3
|
||||
[Serializable]
|
||||
#endif
|
||||
internal class RegexExtended : Regex
|
||||
/// <inheritdoc cref="Regex"/>
|
||||
public RegexExtended(string pattern) : this(pattern, RegexOptions.None)
|
||||
{
|
||||
/// <inheritdoc cref="Regex"/>
|
||||
public RegexExtended(string pattern) : this(pattern, RegexOptions.None)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Regex"/>
|
||||
public RegexExtended(string pattern, RegexOptions options) :
|
||||
this(pattern, options, Regex.InfiniteMatchTimeout)
|
||||
{
|
||||
}
|
||||
/// <inheritdoc cref="Regex"/>
|
||||
public RegexExtended(string pattern, RegexOptions options) :
|
||||
this(pattern, options, Regex.InfiniteMatchTimeout)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Regex"/>
|
||||
public RegexExtended(string pattern, RegexOptions options, TimeSpan matchTimeout) :
|
||||
base(ReplaceGuidPattern(pattern), options, matchTimeout)
|
||||
{
|
||||
}
|
||||
/// <inheritdoc cref="Regex"/>
|
||||
public RegexExtended(string pattern, RegexOptions options, TimeSpan matchTimeout) :
|
||||
base(ReplaceGuidPattern(pattern), options, matchTimeout)
|
||||
{
|
||||
}
|
||||
|
||||
#if !NETSTANDARD1_3
|
||||
/// <inheritdoc cref="Regex"/>
|
||||
protected RegexExtended(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) :
|
||||
base(info, context)
|
||||
{
|
||||
}
|
||||
/// <inheritdoc cref="Regex"/>
|
||||
protected RegexExtended(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) :
|
||||
base(info, context)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
// Dictionary of various Guid tokens with a corresponding regular expression pattern to use instead.
|
||||
private static readonly Dictionary<string, string> GuidTokenPatterns = new Dictionary<string, string>
|
||||
// Dictionary of various Guid tokens with a corresponding regular expression pattern to use instead.
|
||||
private static readonly Dictionary<string, string> GuidTokenPatterns = new()
|
||||
{
|
||||
// Lower case format `B` Guid pattern
|
||||
{ @"\guidb", @"(\{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\})" },
|
||||
|
||||
// Upper case format `B` Guid pattern
|
||||
{ @"\GUIDB", @"(\{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}\})" },
|
||||
|
||||
// Lower case format `D` Guid pattern
|
||||
{ @"\guidd", "([a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12})" },
|
||||
|
||||
// Upper case format `D` Guid pattern
|
||||
{ @"\GUIDD", "([A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12})" },
|
||||
|
||||
// Lower case format `N` Guid pattern
|
||||
{ @"\guidn", "([a-z0-9]{32})" },
|
||||
|
||||
// Upper case format `N` Guid pattern
|
||||
{ @"\GUIDN", "([A-Z0-9]{32})" },
|
||||
|
||||
// Lower case format `P` Guid pattern
|
||||
{ @"\guidp", @"(\([a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\))" },
|
||||
|
||||
// Upper case format `P` Guid pattern
|
||||
{ @"\GUIDP", @"(\([A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}\))" },
|
||||
|
||||
// Lower case format `X` Guid pattern
|
||||
{ @"\guidx", @"(\{0x[a-f0-9]{8},0x[a-f0-9]{4},0x[a-f0-9]{4},\{(0x[a-f0-9]{2},){7}(0x[a-f0-9]{2})\}\})" },
|
||||
|
||||
// Upper case format `X` Guid pattern
|
||||
{ @"\GUIDX", @"(\{0x[A-F0-9]{8},0x[A-F0-9]{4},0x[A-F0-9]{4},\{(0x[A-F0-9]{2},){7}(0x[A-F0-9]{2})\}\})" },
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Replaces all instances of valid GUID tokens with the correct regular expression to match.
|
||||
/// </summary>
|
||||
/// <param name="pattern">Pattern to replace token for.</param>
|
||||
private static string ReplaceGuidPattern(string pattern)
|
||||
{
|
||||
Guard.NotNull(pattern, nameof(pattern));
|
||||
|
||||
foreach (var tokenPattern in GuidTokenPatterns)
|
||||
{
|
||||
// Lower case format `B` Guid pattern
|
||||
{ @"\guidb", @"(\{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\})" },
|
||||
|
||||
// Upper case format `B` Guid pattern
|
||||
{ @"\GUIDB", @"(\{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}\})" },
|
||||
|
||||
// Lower case format `D` Guid pattern
|
||||
{ @"\guidd", "([a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12})" },
|
||||
|
||||
// Upper case format `D` Guid pattern
|
||||
{ @"\GUIDD", "([A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12})" },
|
||||
|
||||
// Lower case format `N` Guid pattern
|
||||
{ @"\guidn", "([a-z0-9]{32})" },
|
||||
|
||||
// Upper case format `N` Guid pattern
|
||||
{ @"\GUIDN", "([A-Z0-9]{32})" },
|
||||
|
||||
// Lower case format `P` Guid pattern
|
||||
{ @"\guidp", @"(\([a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\))" },
|
||||
|
||||
// Upper case format `P` Guid pattern
|
||||
{ @"\GUIDP", @"(\([A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}\))" },
|
||||
|
||||
// Lower case format `X` Guid pattern
|
||||
{ @"\guidx", @"(\{0x[a-f0-9]{8},0x[a-f0-9]{4},0x[a-f0-9]{4},\{(0x[a-f0-9]{2},){7}(0x[a-f0-9]{2})\}\})" },
|
||||
|
||||
// Upper case format `X` Guid pattern
|
||||
{ @"\GUIDX", @"(\{0x[A-F0-9]{8},0x[A-F0-9]{4},0x[A-F0-9]{4},\{(0x[A-F0-9]{2},){7}(0x[A-F0-9]{2})\}\})" },
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Replaces all instances of valid GUID tokens with the correct regular expression to match.
|
||||
/// </summary>
|
||||
/// <param name="pattern">Pattern to replace token for.</param>
|
||||
private static string ReplaceGuidPattern(string pattern)
|
||||
{
|
||||
Guard.NotNull(pattern, nameof(pattern));
|
||||
|
||||
foreach (var tokenPattern in GuidTokenPatterns)
|
||||
{
|
||||
pattern = pattern.Replace(tokenPattern.Key, tokenPattern.Value);
|
||||
}
|
||||
|
||||
return pattern;
|
||||
pattern = pattern.Replace(tokenPattern.Key, tokenPattern.Value);
|
||||
}
|
||||
|
||||
return pattern;
|
||||
}
|
||||
}
|
||||
@@ -1,159 +1,157 @@
|
||||
// This source file is based on mock4net by Alexandre Victoor which is licensed under the Apache 2.0 License.
|
||||
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
|
||||
using JetBrains.Annotations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using Stef.Validation;
|
||||
using WireMock.Models;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
using Stef.Validation;
|
||||
|
||||
namespace WireMock
|
||||
namespace WireMock;
|
||||
|
||||
/// <summary>
|
||||
/// The RequestMessage.
|
||||
/// </summary>
|
||||
public class RequestMessage : IRequestMessage
|
||||
{
|
||||
/// <inheritdoc cref="IRequestMessage.ClientIP" />
|
||||
public string ClientIP { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Url" />
|
||||
public string Url { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.AbsoluteUrl" />
|
||||
public string AbsoluteUrl { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.ProxyUrl" />
|
||||
public string ProxyUrl { get; set; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.DateTime" />
|
||||
public DateTime DateTime { get; set; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Path" />
|
||||
public string Path { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.AbsolutePath" />
|
||||
public string AbsolutePath { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.PathSegments" />
|
||||
public string[] PathSegments { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.AbsolutePathSegments" />
|
||||
public string[] AbsolutePathSegments { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Method" />
|
||||
public string Method { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Headers" />
|
||||
public IDictionary<string, WireMockList<string>>? Headers { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Cookies" />
|
||||
public IDictionary<string, string>? Cookies { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Query" />
|
||||
public IDictionary<string, WireMockList<string>>? Query { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.RawQuery" />
|
||||
public string RawQuery { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.BodyData" />
|
||||
public IBodyData? BodyData { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Body" />
|
||||
public string Body { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.BodyAsJson" />
|
||||
public object BodyAsJson { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.BodyAsBytes" />
|
||||
public byte[] BodyAsBytes { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.DetectedBodyType" />
|
||||
public string DetectedBodyType { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.DetectedBodyTypeFromContentType" />
|
||||
public string DetectedBodyTypeFromContentType { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.DetectedCompression" />
|
||||
public string DetectedCompression { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Host" />
|
||||
public string Host { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Protocol" />
|
||||
public string Protocol { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Port" />
|
||||
public int Port { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Origin" />
|
||||
public string Origin { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The RequestMessage.
|
||||
/// Initializes a new instance of the <see cref="RequestMessage"/> class.
|
||||
/// </summary>
|
||||
public class RequestMessage : IRequestMessage
|
||||
/// <param name="urlDetails">The original url details.</param>
|
||||
/// <param name="method">The HTTP method.</param>
|
||||
/// <param name="clientIP">The client IP Address.</param>
|
||||
/// <param name="bodyData">The BodyData.</param>
|
||||
/// <param name="headers">The headers.</param>
|
||||
/// <param name="cookies">The cookies.</param>
|
||||
public RequestMessage(UrlDetails urlDetails, string method, string clientIP, IBodyData? bodyData = null, IDictionary<string, string[]>? headers = null, IDictionary<string, string>? cookies = null)
|
||||
{
|
||||
/// <inheritdoc cref="IRequestMessage.ClientIP" />
|
||||
public string ClientIP { get; }
|
||||
Guard.NotNull(urlDetails, nameof(urlDetails));
|
||||
Guard.NotNull(method, nameof(method));
|
||||
Guard.NotNull(clientIP, nameof(clientIP));
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Url" />
|
||||
public string Url { get; }
|
||||
AbsoluteUrl = urlDetails.AbsoluteUrl.ToString();
|
||||
Url = urlDetails.Url.ToString();
|
||||
Protocol = urlDetails.Url.Scheme;
|
||||
Host = urlDetails.Url.Host;
|
||||
Port = urlDetails.Url.Port;
|
||||
Origin = $"{Protocol}://{Host}:{Port}";
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.AbsoluteUrl" />
|
||||
public string AbsoluteUrl { get; }
|
||||
AbsolutePath = WebUtility.UrlDecode(urlDetails.AbsoluteUrl.AbsolutePath);
|
||||
Path = WebUtility.UrlDecode(urlDetails.Url.AbsolutePath);
|
||||
PathSegments = Path.Split('/').Skip(1).ToArray();
|
||||
AbsolutePathSegments = AbsolutePath.Split('/').Skip(1).ToArray();
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.ProxyUrl" />
|
||||
public string ProxyUrl { get; set; }
|
||||
Method = method;
|
||||
ClientIP = clientIP;
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.DateTime" />
|
||||
public DateTime DateTime { get; set; }
|
||||
BodyData = bodyData;
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Path" />
|
||||
public string Path { get; }
|
||||
// Convenience getters for e.g. Handlebars
|
||||
Body = BodyData?.BodyAsString;
|
||||
BodyAsJson = BodyData?.BodyAsJson;
|
||||
BodyAsBytes = BodyData?.BodyAsBytes;
|
||||
DetectedBodyType = BodyData?.DetectedBodyType.ToString();
|
||||
DetectedBodyTypeFromContentType = BodyData?.DetectedBodyTypeFromContentType.ToString();
|
||||
DetectedCompression = BodyData?.DetectedCompression;
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.AbsolutePath" />
|
||||
public string AbsolutePath { get; }
|
||||
Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList<string>(header.Value));
|
||||
Cookies = cookies;
|
||||
RawQuery = urlDetails.Url.Query;
|
||||
Query = QueryStringParser.Parse(RawQuery);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.PathSegments" />
|
||||
public string[] PathSegments { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.AbsolutePathSegments" />
|
||||
public string[] AbsolutePathSegments { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Method" />
|
||||
public string Method { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Headers" />
|
||||
public IDictionary<string, WireMockList<string>>? Headers { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Cookies" />
|
||||
public IDictionary<string, string>? Cookies { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Query" />
|
||||
public IDictionary<string, WireMockList<string>>? Query { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.RawQuery" />
|
||||
public string RawQuery { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.BodyData" />
|
||||
public IBodyData? BodyData { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Body" />
|
||||
public string Body { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.BodyAsJson" />
|
||||
public object BodyAsJson { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.BodyAsBytes" />
|
||||
public byte[] BodyAsBytes { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.DetectedBodyType" />
|
||||
public string DetectedBodyType { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.DetectedBodyTypeFromContentType" />
|
||||
public string DetectedBodyTypeFromContentType { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.DetectedCompression" />
|
||||
public string DetectedCompression { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Host" />
|
||||
public string Host { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Protocol" />
|
||||
public string Protocol { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Port" />
|
||||
public int Port { get; }
|
||||
|
||||
/// <inheritdoc cref="IRequestMessage.Origin" />
|
||||
public string Origin { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RequestMessage"/> class.
|
||||
/// </summary>
|
||||
/// <param name="urlDetails">The original url details.</param>
|
||||
/// <param name="method">The HTTP method.</param>
|
||||
/// <param name="clientIP">The client IP Address.</param>
|
||||
/// <param name="bodyData">The BodyData.</param>
|
||||
/// <param name="headers">The headers.</param>
|
||||
/// <param name="cookies">The cookies.</param>
|
||||
public RequestMessage(UrlDetails urlDetails, string method, string clientIP, IBodyData? bodyData = null, IDictionary<string, string[]>? headers = null, IDictionary<string, string>? cookies = null)
|
||||
/// <summary>
|
||||
/// Get a query parameter.
|
||||
/// </summary>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <param name="ignoreCase">Defines if the key should be matched using case-ignore.</param>
|
||||
/// <returns>The query parameter.</returns>
|
||||
public WireMockList<string>? GetParameter(string key, bool ignoreCase = false)
|
||||
{
|
||||
if (Query == null)
|
||||
{
|
||||
Guard.NotNull(urlDetails, nameof(urlDetails));
|
||||
Guard.NotNull(method, nameof(method));
|
||||
Guard.NotNull(clientIP, nameof(clientIP));
|
||||
|
||||
AbsoluteUrl = urlDetails.AbsoluteUrl.ToString();
|
||||
Url = urlDetails.Url.ToString();
|
||||
Protocol = urlDetails.Url.Scheme;
|
||||
Host = urlDetails.Url.Host;
|
||||
Port = urlDetails.Url.Port;
|
||||
Origin = $"{Protocol}://{Host}:{Port}";
|
||||
|
||||
AbsolutePath = WebUtility.UrlDecode(urlDetails.AbsoluteUrl.AbsolutePath);
|
||||
Path = WebUtility.UrlDecode(urlDetails.Url.AbsolutePath);
|
||||
PathSegments = Path.Split('/').Skip(1).ToArray();
|
||||
AbsolutePathSegments = AbsolutePath.Split('/').Skip(1).ToArray();
|
||||
|
||||
Method = method;
|
||||
ClientIP = clientIP;
|
||||
|
||||
BodyData = bodyData;
|
||||
|
||||
// Convenience getters for e.g. Handlebars
|
||||
Body = BodyData?.BodyAsString;
|
||||
BodyAsJson = BodyData?.BodyAsJson;
|
||||
BodyAsBytes = BodyData?.BodyAsBytes;
|
||||
DetectedBodyType = BodyData?.DetectedBodyType.ToString();
|
||||
DetectedBodyTypeFromContentType = BodyData?.DetectedBodyTypeFromContentType.ToString();
|
||||
DetectedCompression = BodyData?.DetectedCompression;
|
||||
|
||||
Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList<string>(header.Value));
|
||||
Cookies = cookies;
|
||||
RawQuery = urlDetails.Url.Query;
|
||||
Query = QueryStringParser.Parse(RawQuery);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a query parameter.
|
||||
/// </summary>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <param name="ignoreCase">Defines if the key should be matched using case-ignore.</param>
|
||||
/// <returns>The query parameter.</returns>
|
||||
public WireMockList<string>? GetParameter(string? key, bool ignoreCase = false)
|
||||
{
|
||||
if (Query == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var query = !ignoreCase ? Query : new Dictionary<string, WireMockList<string>>(Query, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var query = !ignoreCase ? Query : new Dictionary<string, WireMockList<string>>(Query, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
return query.ContainsKey(key) ? query[key] : null;
|
||||
}
|
||||
return query.ContainsKey(key) ? query[key] : null;
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,7 @@ internal class MatcherMapper
|
||||
bool ignoreCase = matcher.IgnoreCase == true;
|
||||
bool throwExceptionWhenMatcherFails = _settings.ThrowExceptionWhenMatcherFails == true;
|
||||
bool useRegexExtended = _settings.UseRegexExtended == true;
|
||||
bool useRegex = matcher.Regex == true;
|
||||
|
||||
switch (matcherName)
|
||||
{
|
||||
@@ -79,11 +80,11 @@ internal class MatcherMapper
|
||||
|
||||
case nameof(JsonPartialMatcher):
|
||||
var valueForJsonPartialMatcher = matcher.Pattern ?? matcher.Patterns;
|
||||
return new JsonPartialMatcher(matchBehaviour, valueForJsonPartialMatcher!, ignoreCase, throwExceptionWhenMatcherFails);
|
||||
return new JsonPartialMatcher(matchBehaviour, valueForJsonPartialMatcher!, ignoreCase, throwExceptionWhenMatcherFails, useRegex);
|
||||
|
||||
case nameof(JsonPartialWildcardMatcher):
|
||||
var valueForJsonPartialWildcardMatcher = matcher.Pattern ?? matcher.Patterns;
|
||||
return new JsonPartialWildcardMatcher(matchBehaviour, valueForJsonPartialWildcardMatcher!, ignoreCase, throwExceptionWhenMatcherFails);
|
||||
return new JsonPartialWildcardMatcher(matchBehaviour, valueForJsonPartialWildcardMatcher!, ignoreCase, throwExceptionWhenMatcherFails, useRegex);
|
||||
|
||||
case nameof(JsonPathMatcher):
|
||||
return new JsonPathMatcher(matchBehaviour, throwExceptionWhenMatcherFails, matchOperator, stringPatterns);
|
||||
@@ -146,6 +147,17 @@ internal class MatcherMapper
|
||||
Name = matcher.Name
|
||||
};
|
||||
|
||||
switch (matcher)
|
||||
{
|
||||
case JsonPartialMatcher jsonPartialMatcher:
|
||||
model.Regex = jsonPartialMatcher.Regex;
|
||||
break;
|
||||
|
||||
case JsonPartialWildcardMatcher jsonPartialWildcardMatcher:
|
||||
model.Regex = jsonPartialWildcardMatcher.Regex;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (matcher)
|
||||
{
|
||||
// If the matcher is a IStringMatcher, get the operator & patterns.
|
||||
|
||||
@@ -293,7 +293,7 @@ public partial class WireMockServer
|
||||
|
||||
private IResponseMessage SettingsUpdate(IRequestMessage requestMessage)
|
||||
{
|
||||
var settings = DeserializeObject<SettingsModel>(requestMessage);
|
||||
var settings = DeserializeObject<SettingsModel>(requestMessage)!;
|
||||
|
||||
// _settings
|
||||
_settings.AllowBodyForAllHttpMethods = settings.AllowBodyForAllHttpMethods;
|
||||
|
||||
@@ -9,9 +9,9 @@ using WireMock.Handlers;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.RegularExpressions;
|
||||
using WireMock.Types;
|
||||
#if USE_ASPNETCORE
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using WireMock.Types;
|
||||
#endif
|
||||
|
||||
namespace WireMock.Settings
|
||||
|
||||
@@ -1,24 +1,53 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using WireMock.RegularExpressions;
|
||||
|
||||
namespace WireMock.Util
|
||||
namespace WireMock.Util;
|
||||
|
||||
internal static class RegexUtils
|
||||
{
|
||||
internal static class RegexUtils
|
||||
private static readonly TimeSpan RegexTimeOut = new(0, 0, 10);
|
||||
|
||||
public static Dictionary<string, string> GetNamedGroups(Regex regex, string input)
|
||||
{
|
||||
public static Dictionary<string, string> GetNamedGroups(Regex regex, string input)
|
||||
var namedGroupsDictionary = new Dictionary<string, string>();
|
||||
|
||||
GroupCollection groups = regex.Match(input).Groups;
|
||||
foreach (string groupName in regex.GetGroupNames())
|
||||
{
|
||||
var namedGroupsDictionary = new Dictionary<string, string>();
|
||||
|
||||
GroupCollection groups = regex.Match(input).Groups;
|
||||
foreach (string groupName in regex.GetGroupNames())
|
||||
if (groups[groupName].Captures.Count > 0)
|
||||
{
|
||||
if (groups[groupName].Captures.Count > 0)
|
||||
{
|
||||
namedGroupsDictionary.Add(groupName, groups[groupName].Value);
|
||||
}
|
||||
namedGroupsDictionary.Add(groupName, groups[groupName].Value);
|
||||
}
|
||||
}
|
||||
|
||||
return namedGroupsDictionary;
|
||||
return namedGroupsDictionary;
|
||||
}
|
||||
|
||||
public static (bool IsValid, bool Result) MatchRegex(string pattern, string input, bool useRegexExtended = true)
|
||||
{
|
||||
if (string.IsNullOrEmpty(pattern))
|
||||
{
|
||||
return (false, false);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (useRegexExtended)
|
||||
{
|
||||
var r = new RegexExtended(pattern, RegexOptions.None, RegexTimeOut);
|
||||
return (true, r.IsMatch(input));
|
||||
}
|
||||
else
|
||||
{
|
||||
var r = new Regex(pattern, RegexOptions.None, RegexTimeOut);
|
||||
return (true, r.IsMatch(input));
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return (false, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user