| | | 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 function |
| | | 14 | | /// </summary> |
| | 4 | 15 | | public Func<string, bool> Func { get; } |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// The body data function for byte[] |
| | | 19 | | /// </summary> |
| | 3 | 20 | | public Func<byte[], bool> DataFunc { get; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// The body data function for json |
| | | 24 | | /// </summary> |
| | 2 | 25 | | public Func<object, bool> JsonFunc { get; } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The matcher. |
| | | 29 | | /// </summary> |
| | 17 | 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">The body.</param> |
| | 2 | 36 | | public RequestMessageBodyMatcher([NotNull] string body) : this(new SimMetricsMatcher(body)) |
| | 2 | 37 | | { |
| | 2 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="body">The body.</param> |
| | 1 | 44 | | public RequestMessageBodyMatcher([NotNull] byte[] body) : this(new ExactObjectMatcher(body)) |
| | 1 | 45 | | { |
| | 1 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="body">The body.</param> |
| | 1 | 52 | | public RequestMessageBodyMatcher([NotNull] object body) : this(new ExactObjectMatcher(body)) |
| | 1 | 53 | | { |
| | 1 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="func">The function.</param> |
| | 1 | 60 | | public RequestMessageBodyMatcher([NotNull] Func<string, bool> func) |
| | 1 | 61 | | { |
| | 1 | 62 | | Check.NotNull(func, nameof(func)); |
| | 1 | 63 | | Func = func; |
| | 1 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="func">The function.</param> |
| | 1 | 70 | | public RequestMessageBodyMatcher([NotNull] Func<byte[], bool> func) |
| | 1 | 71 | | { |
| | 1 | 72 | | Check.NotNull(func, nameof(func)); |
| | 1 | 73 | | DataFunc = func; |
| | 1 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="func">The function.</param> |
| | 1 | 80 | | public RequestMessageBodyMatcher([NotNull] Func<object, bool> func) |
| | 1 | 81 | | { |
| | 1 | 82 | | Check.NotNull(func, nameof(func)); |
| | 1 | 83 | | JsonFunc = func; |
| | 1 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="matcher">The matcher.</param> |
| | 17 | 90 | | public RequestMessageBodyMatcher([NotNull] IMatcher matcher) |
| | 17 | 91 | | { |
| | 17 | 92 | | Check.NotNull(matcher, nameof(matcher)); |
| | 17 | 93 | | Matcher = matcher; |
| | 17 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <see cref="IRequestMatcher.GetMatchingScore"/> |
| | | 97 | | public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) |
| | 16 | 98 | | { |
| | 16 | 99 | | double score = IsMatch(requestMessage); |
| | 16 | 100 | | return requestMatchResult.AddScore(GetType(), score); |
| | 16 | 101 | | } |
| | | 102 | | |
| | | 103 | | private double IsMatch(RequestMessage requestMessage) |
| | 16 | 104 | | { |
| | 16 | 105 | | if (requestMessage.Body != null) |
| | 9 | 106 | | { |
| | 9 | 107 | | if (Matcher is IStringMatcher stringMatcher) |
| | 8 | 108 | | { |
| | 8 | 109 | | return stringMatcher.IsMatch(requestMessage.Body); |
| | | 110 | | } |
| | 1 | 111 | | } |
| | | 112 | | |
| | 8 | 113 | | if (Matcher is IObjectMatcher objectMatcher) |
| | 5 | 114 | | { |
| | 5 | 115 | | if (requestMessage.BodyAsJson != null) |
| | 4 | 116 | | { |
| | 4 | 117 | | return objectMatcher.IsMatch(requestMessage.BodyAsJson); |
| | | 118 | | } |
| | | 119 | | |
| | 1 | 120 | | if (requestMessage.BodyAsBytes != null) |
| | 1 | 121 | | { |
| | 1 | 122 | | return objectMatcher.IsMatch(requestMessage.BodyAsBytes); |
| | | 123 | | } |
| | 0 | 124 | | } |
| | | 125 | | |
| | 3 | 126 | | if (Func != null) |
| | 1 | 127 | | { |
| | 1 | 128 | | return MatchScores.ToScore(requestMessage.Body != null && Func(requestMessage.Body)); |
| | | 129 | | } |
| | | 130 | | |
| | 2 | 131 | | if (DataFunc != null) |
| | 1 | 132 | | { |
| | 1 | 133 | | return MatchScores.ToScore(requestMessage.BodyAsBytes != null && DataFunc(requestMessage.BodyAsBytes)); |
| | | 134 | | } |
| | | 135 | | |
| | 1 | 136 | | if (JsonFunc != null) |
| | 1 | 137 | | { |
| | 1 | 138 | | return MatchScores.ToScore(requestMessage.BodyAsJson != null && JsonFunc(requestMessage.BodyAsJson)); |
| | | 139 | | } |
| | | 140 | | |
| | 0 | 141 | | return MatchScores.Mismatch; |
| | 16 | 142 | | } |
| | | 143 | | } |
| | | 144 | | } |