using System; using System.Collections.Generic; using System.Linq; using AnyOfTypes; using Stef.Validation; using WireMock.Models; namespace WireMock.Matchers.Request; /// /// The request clientIP matcher. /// public class RequestMessageClientIPMatcher : IRequestMatcher { /// /// The matchers /// public IReadOnlyList? Matchers { get; } /// /// The clientIP functions /// public Func[]? Funcs { get; } /// /// The /// public MatchBehaviour Behaviour { get; } /// /// The /// public MatchOperator MatchOperator { get; } /// /// Initializes a new instance of the class. /// /// The match behaviour. /// The to use. /// The clientIPs. public RequestMessageClientIPMatcher( MatchBehaviour matchBehaviour, MatchOperator matchOperator, params string[] clientIPs) : this(matchBehaviour, matchOperator, clientIPs .Select(clientIP => new WildcardMatcher(matchBehaviour, new AnyOf[] { clientIP }, false, matchOperator)) .Cast().ToArray()) { Behaviour = matchBehaviour; MatchOperator = matchOperator; } /// /// Initializes a new instance of the class. /// /// The match behaviour. /// The to use. /// The matchers. public RequestMessageClientIPMatcher(MatchBehaviour matchBehaviour, MatchOperator matchOperator, params IStringMatcher[] matchers) { Matchers = Guard.NotNull(matchers); Behaviour = matchBehaviour; MatchOperator = matchOperator; } /// /// Initializes a new instance of the class. /// /// The clientIP functions. public RequestMessageClientIPMatcher(params Func[] funcs) { Funcs = Guard.NotNull(funcs); } /// public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult) { var (score, exception) = GetMatchResult(requestMessage).Expand(); return requestMatchResult.AddScore(GetType(), score, exception); } private MatchResult GetMatchResult(IRequestMessage requestMessage) { if (Matchers != null) { var results = Matchers.Select(m => m.IsMatch(requestMessage.ClientIP)).ToArray(); return MatchResult.From(results, MatchOperator); } if (Funcs != null) { var results = Funcs.Select(func => func(requestMessage.ClientIP)).ToArray(); return MatchScores.ToScore(results, MatchOperator); } return default; } }