mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-17 23:03:46 +01:00
* Implement IMimeMessageData * 1 * Update src/WireMock.Net.MimePart/Util/MimeKitUtils.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * v1 * v2 * e * ? * fix * if (Array.TrueForAll(_funcs, func => func(value).IsPerfect())) * Update src/WireMock.Net.Shared/Util/IMimeKitUtils.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/WireMock.Net.Minimal/Matchers/Request/RequestMessageMultiPartMatcher.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/WireMock.Net.MimePart/Models/MimeEntityDataWrapper.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Models.Mime.IMimeMessageData? BodyAsMimeMessage { get; } * Update src/WireMock.Net.MimePart/Util/MimeKitUtils.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/WireMock.Net.MimePart/Models/MimePartDataWrapper.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/WireMock.Net.MimePart/Models/MimeMessageDataWrapper.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/WireMock.Net.Shared/Util/IMimeKitUtils.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * . --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
112 lines
3.4 KiB
C#
112 lines
3.4 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using System;
|
|
using WireMock.Matchers.Helpers;
|
|
using WireMock.Models.Mime;
|
|
using WireMock.Util;
|
|
|
|
namespace WireMock.Matchers;
|
|
|
|
/// <summary>
|
|
/// MimePartMatcher
|
|
/// </summary>
|
|
public class MimePartMatcher : IMimePartMatcher
|
|
{
|
|
private readonly Func<IMimePartData, MatchResult>[] _funcs;
|
|
|
|
/// <inheritdoc />
|
|
public string Name => nameof(MimePartMatcher);
|
|
|
|
/// <inheritdoc />
|
|
public IStringMatcher? ContentTypeMatcher { get; }
|
|
|
|
/// <inheritdoc />
|
|
public IStringMatcher? ContentDispositionMatcher { get; }
|
|
|
|
/// <inheritdoc />
|
|
public IStringMatcher? ContentTransferEncodingMatcher { get; }
|
|
|
|
/// <inheritdoc />
|
|
public IMatcher? ContentMatcher { get; }
|
|
|
|
/// <inheritdoc />
|
|
public MatchBehaviour MatchBehaviour { get; }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="MimePartMatcher"/> class.
|
|
/// </summary>
|
|
public MimePartMatcher(
|
|
MatchBehaviour matchBehaviour,
|
|
IStringMatcher? contentTypeMatcher,
|
|
IStringMatcher? contentDispositionMatcher,
|
|
IStringMatcher? contentTransferEncodingMatcher,
|
|
IMatcher? contentMatcher
|
|
)
|
|
{
|
|
MatchBehaviour = matchBehaviour;
|
|
ContentTypeMatcher = contentTypeMatcher;
|
|
ContentDispositionMatcher = contentDispositionMatcher;
|
|
ContentTransferEncodingMatcher = contentTransferEncodingMatcher;
|
|
ContentMatcher = contentMatcher;
|
|
|
|
_funcs =
|
|
[
|
|
mp => ContentTypeMatcher?.IsMatch(GetContentTypeAsString(mp.ContentType)) ?? MatchScores.Perfect,
|
|
mp => ContentDispositionMatcher?.IsMatch(mp.ContentDisposition?.ToString()?.Replace("Content-Disposition: ", string.Empty)) ?? MatchScores.Perfect,
|
|
mp => ContentTransferEncodingMatcher?.IsMatch(mp.ContentTransferEncoding.ToLowerInvariant()) ?? MatchScores.Perfect,
|
|
MatchOnContent
|
|
];
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public MatchResult IsMatch(IMimePartData value)
|
|
{
|
|
var score = MatchScores.Mismatch;
|
|
Exception? exception = null;
|
|
|
|
try
|
|
{
|
|
if (Array.TrueForAll(_funcs, func => func(value).IsPerfect()))
|
|
{
|
|
score = MatchScores.Perfect;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
exception = ex;
|
|
}
|
|
|
|
return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score), exception);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public string GetCSharpCodeArguments()
|
|
{
|
|
return "NotImplemented";
|
|
}
|
|
|
|
private MatchResult MatchOnContent(IMimePartData mimePart)
|
|
{
|
|
if (ContentMatcher == null)
|
|
{
|
|
return MatchScores.Perfect;
|
|
}
|
|
|
|
var bodyParserSettings = new BodyParserSettings
|
|
{
|
|
Stream = mimePart.Open(),
|
|
ContentType = GetContentTypeAsString(mimePart.ContentType),
|
|
DeserializeJson = true,
|
|
ContentEncoding = null, // mimePart.ContentType?.CharsetEncoding.ToString(),
|
|
DecompressGZipAndDeflate = true
|
|
};
|
|
|
|
var bodyData = BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false).GetAwaiter().GetResult();
|
|
return BodyDataMatchScoreCalculator.CalculateMatchScore(bodyData, ContentMatcher);
|
|
}
|
|
|
|
private static string? GetContentTypeAsString(IContentTypeData? contentType)
|
|
{
|
|
return contentType?.ToString()?.Replace("Content-Type: ", string.Empty);
|
|
}
|
|
} |