// Copyright © WireMock.Net using System; using System.Collections.Generic; using System.Linq; using AnyOfTypes; using Stef.Validation; using WireMock.Models; namespace WireMock.Matchers.Request; /// /// The request url matcher. /// public class RequestMessageUrlMatcher : IRequestMatcher { /// /// The matchers /// public IReadOnlyList? Matchers { get; } /// /// The url 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 urls. public RequestMessageUrlMatcher( MatchBehaviour matchBehaviour, MatchOperator matchOperator, params string[] urls) : this(matchBehaviour, matchOperator, urls .Select(url => new WildcardMatcher(matchBehaviour, new AnyOf[] { url }, false, matchOperator)) .Cast().ToArray()) { Behaviour = matchBehaviour; MatchOperator = matchOperator; } /// /// Initializes a new instance of the class. /// /// The match behaviour. /// The to use. (default = "Or") /// The matchers. public RequestMessageUrlMatcher(MatchBehaviour matchBehaviour, MatchOperator matchOperator, params IStringMatcher[] matchers) { Matchers = Guard.NotNull(matchers); Behaviour = matchBehaviour; MatchOperator = matchOperator; } /// /// Initializes a new instance of the class. /// /// The url functions. public RequestMessageUrlMatcher(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.Url)).ToArray(); return MatchResult.From(nameof(RequestMessageUrlMatcher), results, MatchOperator); } if (Funcs != null) { var results = Funcs.Select(func => func(requestMessage.Url)).ToArray(); var score = MatchScores.ToScore(results, MatchOperator); return MatchResult.From(nameof(RequestMessageUrlMatcher), score); } return MatchResult.From(nameof(RequestMessageUrlMatcher)); } }