using System; using System.Collections.Generic; using System.Linq; using Stef.Validation; using WireMock.Types; namespace WireMock.Matchers.Request; /// /// The request parameters matcher. /// public class RequestMessageParamMatcher : IRequestMatcher { private readonly MatchBehaviour _matchBehaviour; /// /// The funcs /// public Func>, bool>[]? Funcs { get; } /// /// The key /// public string? Key { get; } /// /// Defines if the key should be matched using case-ignore. /// public bool? IgnoreCase { get; } /// /// The matchers. /// public IReadOnlyList? Matchers { get; } /// /// Initializes a new instance of the class. /// /// The match behaviour. /// The key. /// Defines if the key should be matched using case-ignore. public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, string key, bool ignoreCase) : this(matchBehaviour, key, ignoreCase, (IStringMatcher[]?)null) { } /// /// Initializes a new instance of the class. /// /// The match behaviour. /// The key. /// Defines if the key should be matched using case-ignore. /// The values. public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, string key, bool ignoreCase, string[]? values) : this(matchBehaviour, key, ignoreCase, values?.Select(value => new ExactMatcher(matchBehaviour, false, MatchOperator.And, value)).Cast().ToArray()) { } /// /// Initializes a new instance of the class. /// /// The match behaviour. /// The key. /// Defines if the key should be matched using case-ignore. /// The matchers. public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, string key, bool ignoreCase, IStringMatcher[]? matchers) { _matchBehaviour = matchBehaviour; Key = Guard.NotNull(key); IgnoreCase = ignoreCase; Matchers = matchers; } /// /// Initializes a new instance of the class. /// /// The funcs. public RequestMessageParamMatcher(params Func>, bool>[] funcs) { Funcs = Guard.NotNull(funcs); } /// public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult) { double score = MatchBehaviourHelper.Convert(_matchBehaviour, IsMatch(requestMessage)); return requestMatchResult.AddScore(GetType(), score); } private double IsMatch(IRequestMessage requestMessage) { if (Funcs != null) { return MatchScores.ToScore(requestMessage.Query != null && Funcs.Any(f => f(requestMessage.Query))); } WireMockList valuesPresentInRequestMessage = ((RequestMessage)requestMessage).GetParameter(Key, IgnoreCase ?? false); if (valuesPresentInRequestMessage == null) { // Key is not present at all, just return Mismatch return MatchScores.Mismatch; } if (Matchers != null && Matchers.Any()) { // Return the score based on Matchers and valuesPresentInRequestMessage return CalculateScore(valuesPresentInRequestMessage); } if (Matchers == null || !Matchers.Any()) { // Matchers are null or not defined, and Key is present, just return Perfect. return MatchScores.Perfect; } return MatchScores.Mismatch; } private double CalculateScore(WireMockList valuesPresentInRequestMessage) { var total = new List(); // If the total patterns in all matchers > values in message, use the matcher as base if (Matchers.Sum(m => m.GetPatterns().Length) > valuesPresentInRequestMessage.Count) { foreach (var matcher in Matchers) { double score = 0d; foreach (string valuePresentInRequestMessage in valuesPresentInRequestMessage) { score += matcher.IsMatch(valuePresentInRequestMessage) / matcher.GetPatterns().Length; } total.Add(score); } } else { foreach (string valuePresentInRequestMessage in valuesPresentInRequestMessage) { double score = Matchers.Max(m => m.IsMatch(valuePresentInRequestMessage)); total.Add(score); } } return total.Any() ? MatchScores.ToScore(total, MatchOperator.Average) : MatchScores.Mismatch; } }