| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using WireMock.Validation; |
| | | 6 | | |
| | | 7 | | namespace WireMock.Matchers.Request |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// The request ClientIP matcher. |
| | | 11 | | /// </summary> |
| | | 12 | | public class RequestMessageClientIPMatcher : IRequestMatcher |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// The matchers. |
| | | 16 | | /// </summary> |
| | 7 | 17 | | public IReadOnlyList<IStringMatcher> Matchers { get; } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// The ClientIP functions. |
| | | 21 | | /// </summary> |
| | 2 | 22 | | public Func<string, bool>[] Funcs { get; } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="RequestMessageClientIPMatcher"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="clientIPs">The clientIPs.</param> |
| | | 28 | | /// <param name="matchBehaviour">The match behaviour.</param> |
| | 5 | 29 | | public RequestMessageClientIPMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] clientIPs) : this( |
| | 2 | 30 | | { |
| | 2 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="RequestMessageClientIPMatcher"/> class. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="matchers">The matchers.</param> |
| | 3 | 37 | | public RequestMessageClientIPMatcher([NotNull] params IStringMatcher[] matchers) |
| | 3 | 38 | | { |
| | 3 | 39 | | Check.NotNull(matchers, nameof(matchers)); |
| | 3 | 40 | | Matchers = matchers; |
| | 3 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Initializes a new instance of the <see cref="RequestMessageClientIPMatcher"/> class. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="funcs">The clientIP functions.</param> |
| | 1 | 47 | | public RequestMessageClientIPMatcher([NotNull] params Func<string, bool>[] funcs) |
| | 1 | 48 | | { |
| | 1 | 49 | | Check.NotNull(funcs, nameof(funcs)); |
| | 1 | 50 | | Funcs = funcs; |
| | 1 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc cref="IRequestMatcher.GetMatchingScore"/> |
| | | 54 | | public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) |
| | 4 | 55 | | { |
| | 4 | 56 | | double score = IsMatch(requestMessage); |
| | 4 | 57 | | return requestMatchResult.AddScore(GetType(), score); |
| | 4 | 58 | | } |
| | | 59 | | |
| | | 60 | | private double IsMatch(RequestMessage requestMessage) |
| | 4 | 61 | | { |
| | 4 | 62 | | if (Matchers != null) |
| | 3 | 63 | | { |
| | 7 | 64 | | return Matchers.Max(matcher => matcher.IsMatch(requestMessage.ClientIP)); |
| | | 65 | | } |
| | | 66 | | |
| | 1 | 67 | | if (Funcs != null) |
| | 1 | 68 | | { |
| | 2 | 69 | | return MatchScores.ToScore(requestMessage.ClientIP != null && Funcs.Any(func => func(requestMessage.Clie |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | return MatchScores.Mismatch; |
| | 4 | 73 | | } |
| | | 74 | | } |
| | | 75 | | } |