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