mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-05-19 05:16:53 +02:00
a292f28dda
* Version 2.x * Setup .NET 9 * 12 * cleanup some #if for NETSTANDARD1_3 * cleanup + fix tests for net8 * openapi * NO ConfigureAwait(false) + cleanup * . * #endif * HashSet * WireMock.Net.NUnit * HttpContext * Add WebSockets (#1423) * Add WebSockets * Add tests * fix * more tests * Add tests * ... * remove IOwin * - * tests * fluent * ok * match * . * byte[] * x * func * func * byte * trans * ... * frameworks......... * jmes * xxx * sc * using var httpClient = new HttpClient(); * usings * maxRetries * up * xunit v3 * ct * --- * ct * ct2 * T Unit * WireMock.Net.TUnitTests / 10 * t unit first * --project * no tunit * t2 * --project * --project * ci - --project * publish ./test/wiremock-coverage.xml * windows * . * log * ... * log * goed * BodyType * . * . * --scenario * ... * pact * ct * . * WireMock.Net.RestClient.AwesomeAssertions (#1427) * WireMock.Net.RestClient.AwesomeAssertions * ok * atpath * fix test * sonar fixes * ports * proxy test * FIX? * --- * await Task.Delay(100, _ct); * ? * --project * Aspire: use IDistributedApplicationEventingSubscriber (#1428) * broadcast * ok * more tsts * . * Collection * up * . * 2 * remove nfluent * <VersionPrefix>2.0.0-preview-02</VersionPrefix> * ... * . * nuget icon * . * <PackageReference Include="JmesPath.Net" Version="1.1.0" /> * x * 500 * . * fix some warnings * ws
114 lines
3.6 KiB
C#
114 lines
3.6 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using System.Linq;
|
|
using Stef.Validation;
|
|
using WireMock.Util;
|
|
|
|
namespace WireMock.Matchers.Request;
|
|
|
|
/// <summary>
|
|
/// The request body MultiPart matcher.
|
|
/// </summary>
|
|
public class RequestMessageMultiPartMatcher : IRequestMatcher
|
|
{
|
|
/// <summary>
|
|
/// The name of this matcher.
|
|
/// </summary>
|
|
public const string Name = "MultiPartMatcher";
|
|
|
|
private readonly IMimeKitUtils _mimeKitUtils = LoadMimeKitUtils();
|
|
|
|
/// <summary>
|
|
/// The matchers.
|
|
/// </summary>
|
|
public IMatcher[]? Matchers { get; }
|
|
|
|
/// <summary>
|
|
/// The <see cref="MatchOperator"/>
|
|
/// </summary>
|
|
public MatchOperator MatchOperator { get; } = MatchOperator.And;
|
|
|
|
/// <summary>
|
|
/// The <see cref="MatchBehaviour"/>
|
|
/// </summary>
|
|
public MatchBehaviour MatchBehaviour { get; }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="RequestMessageMultiPartMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="matchers">The matchers.</param>
|
|
public RequestMessageMultiPartMatcher(params IMatcher[] matchers)
|
|
{
|
|
Matchers = Guard.NotNull(matchers);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="RequestMessageMultiPartMatcher"/> 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 RequestMessageMultiPartMatcher(MatchBehaviour matchBehaviour, MatchOperator matchOperator, params IMatcher[] matchers)
|
|
{
|
|
Matchers = Guard.NotNull(matchers);
|
|
MatchBehaviour = matchBehaviour;
|
|
MatchOperator = matchOperator;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
|
|
{
|
|
var matchDetail = MatchResult.From(Name).ToMatchDetail();
|
|
Exception? exception = null;
|
|
|
|
if (Matchers == null)
|
|
{
|
|
return requestMatchResult.AddMatchDetail(matchDetail);
|
|
}
|
|
|
|
if (!_mimeKitUtils.TryGetMimeMessage(requestMessage, out var message))
|
|
{
|
|
return requestMatchResult.AddMatchDetail(matchDetail);
|
|
}
|
|
|
|
double score = MatchScores.Mismatch;
|
|
try
|
|
{
|
|
foreach (var mimePartMatcher in Matchers.OfType<IMimePartMatcher>().ToArray())
|
|
{
|
|
score = MatchScores.Mismatch;
|
|
|
|
foreach (var mimeBodyPart in message.BodyParts)
|
|
{
|
|
var matchResult = mimePartMatcher.IsMatch(mimeBodyPart);
|
|
if (matchResult.IsPerfect())
|
|
{
|
|
score = MatchScores.Perfect;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ((MatchOperator == MatchOperator.Or && MatchScores.IsPerfect(score)) || (MatchOperator == MatchOperator.And && !MatchScores.IsPerfect(score)))
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
exception = ex;
|
|
}
|
|
|
|
return requestMatchResult.AddMatchDetail(MatchResult.From(Name, score, exception).ToMatchDetail());
|
|
}
|
|
|
|
private static IMimeKitUtils LoadMimeKitUtils()
|
|
{
|
|
if (TypeLoader.TryLoadStaticInstance<IMimeKitUtils>(out var mimeKitUtils))
|
|
{
|
|
return mimeKitUtils;
|
|
}
|
|
|
|
throw new InvalidOperationException("MimeKit is required for RequestMessageMultiPartMatcher. Please install the WireMock.Net.MimePart package.");
|
|
}
|
|
} |