mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 08:13:53 +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>
74 lines
2.4 KiB
C#
74 lines
2.4 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using MimeKit;
|
|
using Stef.Validation;
|
|
using WireMock.Http;
|
|
using WireMock.Models;
|
|
using WireMock.Models.Mime;
|
|
using WireMock.Types;
|
|
|
|
namespace WireMock.Util;
|
|
|
|
internal class MimeKitUtils : IMimeKitUtils
|
|
{
|
|
/// <inheritdoc />
|
|
public IMimeMessageData LoadFromStream(Stream stream)
|
|
{
|
|
return new MimeMessageDataWrapper(MimeMessage.Load(Guard.NotNull(stream)));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public bool TryGetMimeMessage(IRequestMessage requestMessage, [NotNullWhen(true)] out IMimeMessageData? mimeMessageData)
|
|
{
|
|
Guard.NotNull(requestMessage);
|
|
|
|
if (requestMessage.BodyData != null &&
|
|
requestMessage.Headers?.TryGetValue(HttpKnownHeaderNames.ContentType, out var contentTypeHeader) == true &&
|
|
StartsWithMultiPart(contentTypeHeader)
|
|
)
|
|
{
|
|
var bytes = requestMessage.BodyData?.DetectedBodyType switch
|
|
{
|
|
// If the body is bytes, use the BodyAsBytes to match on.
|
|
BodyType.Bytes => requestMessage.BodyData.BodyAsBytes!,
|
|
|
|
// If the body is a String or MultiPart, use the BodyAsString to match on.
|
|
BodyType.String or BodyType.MultiPart => Encoding.UTF8.GetBytes(requestMessage.BodyData.BodyAsString!),
|
|
|
|
_ => throw new NotSupportedException()
|
|
};
|
|
|
|
var fixedBytes = FixBytes(bytes, contentTypeHeader[0]);
|
|
|
|
mimeMessageData = LoadFromStream(new MemoryStream(fixedBytes));
|
|
return true;
|
|
}
|
|
|
|
mimeMessageData = null;
|
|
return false;
|
|
}
|
|
|
|
|
|
private static bool StartsWithMultiPart(WireMockList<string> contentTypeHeader)
|
|
{
|
|
return contentTypeHeader.Any(ct => ct.TrimStart().StartsWith("multipart/", StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
private static byte[] FixBytes(byte[] bytes, WireMockList<string> contentType)
|
|
{
|
|
var contentTypeBytes = Encoding.UTF8.GetBytes($"{HttpKnownHeaderNames.ContentType}: {contentType}\r\n\r\n");
|
|
|
|
var result = new byte[contentTypeBytes.Length + bytes.Length];
|
|
|
|
Buffer.BlockCopy(contentTypeBytes, 0, result, 0, contentTypeBytes.Length);
|
|
Buffer.BlockCopy(bytes, 0, result, contentTypeBytes.Length, bytes.Length);
|
|
|
|
return result;
|
|
}
|
|
} |