mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-26 18:28:27 +02:00
Add MultiPart/MimePart Request Matcher (#981)
* wip * . * mm * x * . * . * . * tests * . * more tests * trans * x * win * fix * . * tests
This commit is contained in:
42
src/WireMock.Net/Util/MimeKitUtils.cs
Normal file
42
src/WireMock.Net/Util/MimeKitUtils.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
#if MIMEKIT
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using MimeKit;
|
||||
using WireMock.Http;
|
||||
using WireMock.Types;
|
||||
|
||||
namespace WireMock.Util;
|
||||
|
||||
internal static class MimeKitUtils
|
||||
{
|
||||
public static MimeMessage GetMimeMessage(IBodyData? bodyData, string contentTypeHeaderValue)
|
||||
{
|
||||
var bytes = bodyData?.DetectedBodyType switch
|
||||
{
|
||||
// If the body is bytes, use the BodyAsBytes to match on.
|
||||
BodyType.Bytes => bodyData.BodyAsBytes!,
|
||||
|
||||
// If the body is a String or MultiPart, use the BodyAsString to match on.
|
||||
BodyType.String or BodyType.MultiPart => Encoding.UTF8.GetBytes(bodyData.BodyAsString!),
|
||||
|
||||
_ => throw new NotSupportedException()
|
||||
};
|
||||
|
||||
var fixedBytes = FixBytes(bytes, contentTypeHeaderValue);
|
||||
return MimeMessage.Load(new MemoryStream(fixedBytes));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user