// Copyright © WireMock.Net
using System;
using WireMock.Matchers.Helpers;
using WireMock.Models.Mime;
using WireMock.Util;
namespace WireMock.Matchers;
///
/// MimePartMatcher
///
public class MimePartMatcher : IMimePartMatcher
{
private readonly Func[] _funcs;
///
public string Name => nameof(MimePartMatcher);
///
public IStringMatcher? ContentTypeMatcher { get; }
///
public IStringMatcher? ContentDispositionMatcher { get; }
///
public IStringMatcher? ContentTransferEncodingMatcher { get; }
///
public IMatcher? ContentMatcher { get; }
///
public MatchBehaviour MatchBehaviour { get; }
///
/// Initializes a new instance of the class.
///
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
];
}
///
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);
}
///
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);
}
}