| | | 1 | | using System; |
| | | 2 | | using JetBrains.Annotations; |
| | | 3 | | using WireMock.Validation; |
| | | 4 | | |
| | | 5 | | namespace WireMock.Matchers.Request |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// The request body matcher. |
| | | 9 | | /// </summary> |
| | | 10 | | public class RequestMessageBodyMatcher : IRequestMatcher |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// The body as byte[]. |
| | | 14 | | /// </summary> |
| | | 15 | | private readonly byte[] _bodyData; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// The body function |
| | | 19 | | /// </summary> |
| | 0 | 20 | | public Func<string, bool> Func { get; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// The body data function |
| | | 24 | | /// </summary> |
| | 0 | 25 | | public Func<byte[], bool> DataFunc { get; } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The matcher. |
| | | 29 | | /// </summary> |
| | 26 | 30 | | public IMatcher Matcher { get; } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="body"> |
| | | 36 | | /// The body Regex pattern. |
| | | 37 | | /// </param> |
| | 2 | 38 | | public RequestMessageBodyMatcher([NotNull] string body) : this(new SimMetricsMatcher(body)) |
| | 2 | 39 | | { |
| | 2 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="body"> |
| | | 46 | | /// The body Regex pattern. |
| | | 47 | | /// </param> |
| | 0 | 48 | | public RequestMessageBodyMatcher([NotNull] byte[] body) |
| | 0 | 49 | | { |
| | 0 | 50 | | Check.NotNull(body, nameof(body)); |
| | 0 | 51 | | _bodyData = body; |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="func"> |
| | | 58 | | /// The body func. |
| | | 59 | | /// </param> |
| | 0 | 60 | | public RequestMessageBodyMatcher([NotNull] Func<string, bool> func) |
| | 0 | 61 | | { |
| | 0 | 62 | | Check.NotNull(func, nameof(func)); |
| | 0 | 63 | | Func = func; |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="func"> |
| | | 70 | | /// The body func. |
| | | 71 | | /// </param> |
| | 0 | 72 | | public RequestMessageBodyMatcher([NotNull] Func<byte[], bool> func) |
| | 0 | 73 | | { |
| | 0 | 74 | | Check.NotNull(func, nameof(func)); |
| | 0 | 75 | | DataFunc = func; |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="matcher"> |
| | | 82 | | /// The body matcher. |
| | | 83 | | /// </param> |
| | 15 | 84 | | public RequestMessageBodyMatcher([NotNull] IMatcher matcher) |
| | 15 | 85 | | { |
| | 15 | 86 | | Check.NotNull(matcher, nameof(matcher)); |
| | 15 | 87 | | Matcher = matcher; |
| | 15 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Determines whether the specified RequestMessage is match. |
| | | 92 | | /// </summary> |
| | | 93 | | /// <param name="requestMessage">The RequestMessage.</param> |
| | | 94 | | /// <param name="requestMatchResult">The RequestMatchResult.</param> |
| | | 95 | | /// <returns> |
| | | 96 | | /// A value between 0.0 - 1.0 of the similarity. |
| | | 97 | | /// </returns> |
| | | 98 | | public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) |
| | 13 | 99 | | { |
| | 13 | 100 | | double score = IsMatch(requestMessage); |
| | 13 | 101 | | requestMatchResult.TotalScore += score; |
| | | 102 | | |
| | 13 | 103 | | requestMatchResult.TotalNumber++; |
| | | 104 | | |
| | 13 | 105 | | return score; |
| | 13 | 106 | | } |
| | | 107 | | |
| | | 108 | | private double IsMatch(RequestMessage requestMessage) |
| | 13 | 109 | | { |
| | 13 | 110 | | if (Matcher != null) |
| | 13 | 111 | | return Matcher.IsMatch(requestMessage.Body); |
| | | 112 | | |
| | 0 | 113 | | if (_bodyData != null) |
| | 0 | 114 | | return MatchScores.ToScore(requestMessage.BodyAsBytes == _bodyData); |
| | | 115 | | |
| | 0 | 116 | | if (Func != null) |
| | 0 | 117 | | return MatchScores.ToScore(requestMessage.Body != null && Func(requestMessage.Body)); |
| | | 118 | | |
| | 0 | 119 | | if (DataFunc != null && requestMessage.BodyAsBytes != null) |
| | 0 | 120 | | return MatchScores.ToScore(requestMessage.BodyAsBytes != null && DataFunc(requestMessage.BodyAsBytes)); |
| | | 121 | | |
| | 0 | 122 | | return MatchScores.Mismatch; |
| | 13 | 123 | | } |
| | | 124 | | } |
| | | 125 | | } |