// Copyright © WireMock.Net
using System.Linq;
using Stef.Validation;
using WireMock.Types;
namespace WireMock.Matchers.Request;
///
/// The request parameters matcher.
///
public class RequestMessageParamMatcher : IRequestMatcher
{
///
/// MatchBehaviour
///
public MatchBehaviour MatchBehaviour { get; }
///
/// The funcs
///
public Func>, bool>[]? Funcs { get; }
///
/// The key
///
public string Key { get; } = string.Empty;
///
/// 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, params string[]? values) :
this(matchBehaviour, key, ignoreCase, values?.Select(value => new ExactMatcher(matchBehaviour, ignoreCase, 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, params 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 RequestMatcherType Type => RequestMatcherType.Param;
///
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
{
var score = GetMatchScore(requestMessage);
return requestMatchResult.AddScore(GetType(), MatchBehaviourHelper.Convert(MatchBehaviour, score), null);
}
private double GetMatchScore(IRequestMessage requestMessage)
{
if (Funcs != null)
{
return MatchScores.ToScore(requestMessage.Query != null && Funcs.Any(f => f(requestMessage.Query)));
}
var valuesPresentInRequestMessage = ((RequestMessage)requestMessage).GetParameter(Key, IgnoreCase);
if (valuesPresentInRequestMessage == null)
{
// Key is not present at all, just return Mismatch
return MatchScores.Mismatch;
}
if (Matchers == null || !Matchers.Any())
{
// Matchers are null or not defined, and Key is present, just return Perfect.
return MatchScores.Perfect;
}
// Return the score based on Matchers and valuesPresentInRequestMessage
return CalculateScore(Matchers, valuesPresentInRequestMessage);
}
private static double CalculateScore(IReadOnlyList matchers, 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).Score / matcher.GetPatterns().Length;
}
total.Add(score);
}
}
else
{
foreach (string valuePresentInRequestMessage in valuesPresentInRequestMessage)
{
var score = matchers.Max(m => m.IsMatch(valuePresentInRequestMessage).Score);
total.Add(score);
}
}
return total.Any() ? MatchScores.ToScore(total, MatchOperator.Average) : 0;
}
}