// 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 path matcher.
///
public class RequestMessagePathMatcher : IRequestMatcher
{
///
/// The matchers
///
public IReadOnlyList? Matchers { get; }
///
/// The path 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 paths.
public RequestMessagePathMatcher(
MatchBehaviour matchBehaviour,
MatchOperator matchOperator,
params string[] paths) :
this(matchBehaviour, matchOperator, paths
.Select(path => new WildcardMatcher(matchBehaviour, new AnyOf[] { path }, false, matchOperator))
.Cast().ToArray())
{
Behaviour = matchBehaviour;
MatchOperator = matchOperator;
}
///
/// Initializes a new instance of the class.
///
/// The match behaviour.
/// The to use.
/// The matchers.
public RequestMessagePathMatcher(MatchBehaviour matchBehaviour, MatchOperator matchOperator, params IStringMatcher[] matchers)
{
Matchers = Guard.NotNull(matchers);
Behaviour = matchBehaviour;
MatchOperator = matchOperator;
}
///
/// Initializes a new instance of the class.
///
/// The path functions.
public RequestMessagePathMatcher(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.Path)).ToArray();
return MatchResult.From(nameof(RequestMessagePathMatcher), results, MatchOperator);
}
if (Funcs != null)
{
var results = Funcs.Select(func => func(requestMessage.Path)).ToArray();
var score = MatchScores.ToScore(results, MatchOperator);
return MatchResult.From(nameof(RequestMessagePathMatcher), score);
}
return MatchResult.From(nameof(RequestMessagePathMatcher));
}
}