// Copyright © WireMock.Net using System; using System.Collections.Generic; using System.Linq; using Stef.Validation; using WireMock.Matchers.Helpers; using WireMock.Util; namespace WireMock.Matchers.Request; /// /// The request body matcher. /// public class RequestMessageBodyMatcher : IRequestMatcher { /// /// The body function /// public Func? MatchOnBodyAsStringFunc { get; } /// /// The body data function for byte[] /// public Func? MatchOnBodyAsBytesFunc { get; } /// /// The body data function for json /// public Func? MatchOnBodyAsJsonFunc { get; } /// /// The body data function for BodyData /// public Func? MatchOnBodyAsBodyDataFunc { get; } /// /// The body data function for FormUrlEncoded /// public Func?, bool>? MatchOnBodyAsFormUrlEncodedFunc { get; } /// /// The matchers. /// public IMatcher[]? Matchers { get; } /// /// The /// public MatchOperator MatchOperator { get; } = MatchOperator.Or; /// /// Initializes a new instance of the class. /// /// The match behaviour. /// The body. public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, string body) : this(new[] { new WildcardMatcher(matchBehaviour, body) }.Cast().ToArray()) { } /// /// Initializes a new instance of the class. /// /// The match behaviour. /// The body. public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, byte[] body) : this(new[] { new ExactObjectMatcher(matchBehaviour, body) }.Cast().ToArray()) { } /// /// Initializes a new instance of the class. /// /// The match behaviour. /// The body. public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, object body) : this(new[] { new ExactObjectMatcher(matchBehaviour, body) }.Cast().ToArray()) { } /// /// Initializes a new instance of the class. /// /// The function. public RequestMessageBodyMatcher(Func func) { MatchOnBodyAsStringFunc = Guard.NotNull(func); } /// /// Initializes a new instance of the class. /// /// The function. public RequestMessageBodyMatcher(Func func) { MatchOnBodyAsBytesFunc = Guard.NotNull(func); } /// /// Initializes a new instance of the class. /// /// The function. public RequestMessageBodyMatcher(Func func) { MatchOnBodyAsJsonFunc = Guard.NotNull(func); } /// /// Initializes a new instance of the class. /// /// The function. public RequestMessageBodyMatcher(Func func) { MatchOnBodyAsBodyDataFunc = Guard.NotNull(func); } /// /// Initializes a new instance of the class. /// /// The function. public RequestMessageBodyMatcher(Func?, bool> func) { MatchOnBodyAsFormUrlEncodedFunc = Guard.NotNull(func); } /// /// Initializes a new instance of the class. /// /// The matchers. public RequestMessageBodyMatcher(params IMatcher[] matchers) { Matchers = Guard.NotNull(matchers); } /// /// Initializes a new instance of the class. /// /// The matchers. /// The to use. public RequestMessageBodyMatcher(MatchOperator matchOperator, params IMatcher[] matchers) { Matchers = Guard.NotNull(matchers); MatchOperator = matchOperator; } /// public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult) { var (score, exception) = CalculateMatchResult(requestMessage).Expand(); return requestMatchResult.AddScore(GetType(), score, exception); } private MatchResult CalculateMatchResult(IRequestMessage requestMessage) { if (Matchers != null && Matchers.Any()) { var results = Matchers.Select(matcher => BodyDataMatchScoreCalculator.CalculateMatchScore(requestMessage.BodyData, matcher)).ToArray(); return MatchResult.From(nameof(RequestMessageBodyMatcher), results, MatchOperator); } if (MatchOnBodyAsStringFunc != null) { return MatchResult.From($"{nameof(RequestMessageBodyMatcher)}:{nameof(MatchOnBodyAsStringFunc)}", MatchScores.ToScore(MatchOnBodyAsStringFunc(requestMessage.BodyData?.BodyAsString))); } if (MatchOnBodyAsFormUrlEncodedFunc != null) { return MatchResult.From($"{nameof(RequestMessageBodyMatcher)}:{nameof(MatchOnBodyAsFormUrlEncodedFunc)}", MatchScores.ToScore(MatchOnBodyAsFormUrlEncodedFunc(requestMessage.BodyData?.BodyAsFormUrlEncoded))); } if (MatchOnBodyAsJsonFunc != null) { return MatchResult.From($"{nameof(RequestMessageBodyMatcher)}:{nameof(MatchOnBodyAsJsonFunc)}", MatchScores.ToScore(MatchOnBodyAsJsonFunc(requestMessage.BodyData?.BodyAsJson))); } if (MatchOnBodyAsBytesFunc != null) { return MatchResult.From($"{nameof(RequestMessageBodyMatcher)}:{nameof(MatchOnBodyAsBytesFunc)}", MatchScores.ToScore(MatchOnBodyAsBytesFunc(requestMessage.BodyData?.BodyAsBytes))); } if (MatchOnBodyAsBodyDataFunc != null) { return MatchResult.From($"{nameof(RequestMessageBodyMatcher)}:{nameof(MatchOnBodyAsBodyDataFunc)}", MatchScores.ToScore(MatchOnBodyAsBodyDataFunc(requestMessage.BodyData))); } return MatchResult.From(nameof(RequestMessageBodyMatcher)); } }