mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-05-03 22:24:32 +02:00
* feat(request matchers): Add support for early mismatch in mapping processing * test(request matchers): Add unit test for early mismatch functionality * test(grpc): Add test for grpc requests early mismatch and error logging (Issue #1442) * feat(request matchers): RequestMatcherType Add `RequestMatcherType` to request matchers for improved type identification Closes #1442 * refactor(request matchers): Request Replace `EarlyMatcherSelector` with `EarlyMatcherType` for improved clarity and consistency Closes #1442 * feat(request): conversion Add EarlyMatcherType support in request models and mapping conversion Closes #1442 * test(mapping): new tests add unit tests for EarlyMatcherType in mapping conversion and serialization Closes #1442 * refactor(request matchers): RequestMessageEarlyMatcher Replaced inline `EarlyMatcherType` logic with the new `RequestMessageEarlyMatcher` class to support cases when several matchers of the same type are present. For instance - Header, Cookie, Param Closes #1442 * test(request matchers): Early Mismatch add unit tests for early mismatch scenarios with several matchers of same type. Currently, headers and parameters Closes #1442 * refactor(mapping): RequestModel.EarlyMatcherType use fully qualified enum for EarlyMatcherType in serialization Closes #1442 * style(review): fixes - removed unused method - added missing curly brackets Closes #1442
103 lines
3.4 KiB
C#
103 lines
3.4 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using System.Linq;
|
|
using AnyOfTypes;
|
|
using Stef.Validation;
|
|
using WireMock.Models;
|
|
|
|
namespace WireMock.Matchers.Request;
|
|
|
|
/// <summary>
|
|
/// The request clientIP matcher.
|
|
/// </summary>
|
|
public class RequestMessageClientIPMatcher : IRequestMatcher
|
|
{
|
|
private const string _name = nameof(RequestMessageClientIPMatcher);
|
|
|
|
/// <summary>
|
|
/// The matchers
|
|
/// </summary>
|
|
public IReadOnlyList<IStringMatcher>? Matchers { get; }
|
|
|
|
/// <summary>
|
|
/// The clientIP functions
|
|
/// </summary>
|
|
public Func<string, bool>[]? Funcs { get; }
|
|
|
|
/// <summary>
|
|
/// The <see cref="MatchBehaviour"/>
|
|
/// </summary>
|
|
public MatchBehaviour Behaviour { get; }
|
|
|
|
/// <summary>
|
|
/// The <see cref="MatchOperator"/>
|
|
/// </summary>
|
|
public MatchOperator MatchOperator { get; }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="RequestMessageClientIPMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
|
/// <param name="matchOperator">The <see cref="MatchOperator"/> to use.</param>
|
|
/// <param name="clientIPs">The clientIPs.</param>
|
|
public RequestMessageClientIPMatcher(
|
|
MatchBehaviour matchBehaviour,
|
|
MatchOperator matchOperator,
|
|
params string[] clientIPs) :
|
|
this(matchBehaviour, matchOperator, clientIPs
|
|
.Select(clientIP => new WildcardMatcher(matchBehaviour, new AnyOf<string, StringPattern>[] { clientIP }, false, matchOperator))
|
|
.Cast<IStringMatcher>().ToArray())
|
|
{
|
|
Behaviour = matchBehaviour;
|
|
MatchOperator = matchOperator;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="RequestMessageClientIPMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
|
/// <param name="matchOperator">The <see cref="MatchOperator"/> to use.</param>
|
|
/// <param name="matchers">The matchers.</param>
|
|
public RequestMessageClientIPMatcher(MatchBehaviour matchBehaviour, MatchOperator matchOperator, params IStringMatcher[] matchers)
|
|
{
|
|
Matchers = Guard.NotNull(matchers);
|
|
Behaviour = matchBehaviour;
|
|
MatchOperator = matchOperator;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="RequestMessageClientIPMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="funcs">The clientIP functions.</param>
|
|
public RequestMessageClientIPMatcher(params Func<string, bool>[] funcs)
|
|
{
|
|
Funcs = Guard.NotNull(funcs);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public RequestMatcherType Type => RequestMatcherType.ClientIP;
|
|
|
|
/// <inheritdoc />
|
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
|
{
|
|
var matchDetail = GetMatchResult(requestMessage).ToMatchDetail();
|
|
return requestMatchResult.AddMatchDetail(matchDetail);
|
|
}
|
|
|
|
private MatchResult GetMatchResult(IRequestMessage requestMessage)
|
|
{
|
|
if (Matchers != null)
|
|
{
|
|
var results = Matchers.Select(m => m.IsMatch(requestMessage.ClientIP)).ToArray();
|
|
return MatchResult.From(_name, results, MatchOperator);
|
|
}
|
|
|
|
if (Funcs != null)
|
|
{
|
|
var results = Funcs.Select(func => func(requestMessage.ClientIP)).ToArray();
|
|
return MatchResult.From(_name, MatchScores.ToScore(results, MatchOperator));
|
|
}
|
|
|
|
return MatchResult.From(_name);
|
|
}
|
|
} |