Skip to content
Open
2 changes: 1 addition & 1 deletion examples/WireMock.Net.Console.NET8/MainApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public static async Task RunAsync()
}
}
System.Console.WriteLine("X = {0} ; default = {1} ; pX = {2:0.00} ; valueX = {3:0.00}", xCount, defaultCount, pX, 1.0 * xCount / tot);
return;

using var httpAndHttpsWithPort = WireMockServer.Start(new WireMockServerSettings
{
HostingScheme = HostingScheme.HttpAndHttps,
Expand Down
7 changes: 5 additions & 2 deletions src/WireMock.Net.MimePart/Util/MimeKitUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public bool TryGetMimeMessage(IRequestMessage requestMessage, [NotNullWhen(true)
return false;
}

var fixedBytes = FixBytes(bytes, contentTypeHeader[0]);
var fixedBytes = PrependContentTypeHeader(bytes, contentTypeHeader[0]);

mimeMessageData = LoadFromStream(new MemoryStream(fixedBytes));
return true;
Expand All @@ -68,7 +68,10 @@ 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)
/// <summary>
/// Prepends the Content-Type header to the byte array to make it a valid MIME message for MimeKit.
/// </summary>
private static byte[] PrependContentTypeHeader(byte[] bytes, WireMockList<string> contentType)
{
var contentTypeBytes = Encoding.UTF8.GetBytes($"{HttpKnownHeaderNames.ContentType}: {contentType}\r\n\r\n");

Expand Down
45 changes: 22 additions & 23 deletions test/WireMock.Net.Tests/Matchers/MimePartMatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,31 @@ public class MimePartMatcherTests

private const string TestMultiPart =
"""
From:
Date: Sun, 23 Jul 2023 16:13:13 +0200
Subject:
Message-Id: <HZ3K1HEAJKU4.IO57XCVO4BWV@desktop-6dd5qi2>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-5XgmpXt0XOfzdtcgNJc2ZQ=="
Content-Type: multipart/mixed; boundary=----MyBoundary123

--=-5XgmpXt0XOfzdtcgNJc2ZQ==
Content-Type: text/plain; charset=utf-8
------MyBoundary123
Content-Type: text/plain
Content-Disposition: form-data; name="textPart"

This is some plain text
--=-5XgmpXt0XOfzdtcgNJc2ZQ==
Content-Type: text/json; charset=utf-8
This is some plain text.
------MyBoundary123
Content-Type: application/json
Content-Disposition: form-data; name="jsonPart"

{
"Key": "Value"
"id": 42,
"message": "Hello from JSON"
}
--=-5XgmpXt0XOfzdtcgNJc2ZQ==
Content-Type: image/png; name=image.png
Content-Disposition: attachment; filename=image.png

------MyBoundary123
Content-Type: image/png
Content-Disposition: form-data; name="imagePart"; filename="example.png"
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjl
AAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC
iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4
//8wEzIABCMDgAEMwEAAAwAA//8DAKkCBf4AAAAASUVORK5CYII=

--=-5XgmpXt0XOfzdtcgNJc2ZQ==--
------MyBoundary123--
""";

[Fact]
Expand All @@ -52,7 +51,7 @@ public void MimePartMatcher_IsMatch_Part_TextPlain()

// Act
var contentTypeMatcher = new ContentTypeMatcher("text/plain");
var contentMatcher = new ExactMatcher("This is some plain text");
var contentMatcher = new ExactMatcher("This is some plain text.");

var matcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, contentTypeMatcher, null, null, contentMatcher);
var result = matcher.IsMatch(part);
Expand All @@ -70,8 +69,8 @@ public void MimePartMatcher_IsMatch_Part_TextJson()
var part = message.BodyParts[1];

// Act
var contentTypeMatcher = new ContentTypeMatcher("text/json");
var contentMatcher = new JsonMatcher(new { Key = "Value" }, true);
var contentTypeMatcher = new ContentTypeMatcher("application/json");
var contentMatcher = new JsonPartialMatcher(new { id = 42 }, true);

var matcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, contentTypeMatcher, null, null, contentMatcher);
var result = matcher.IsMatch(part);
Expand All @@ -89,9 +88,9 @@ public void MimePartMatcher_IsMatch_Part_ImagePng()

// Act
var contentTypeMatcher = new ContentTypeMatcher("image/png");
var contentDispositionMatcher = new ExactMatcher("attachment; filename=\"image.png\"");
var contentDispositionMatcher = new WildcardMatcher("*filename=\"example.png\"");
var contentTransferEncodingMatcher = new ExactMatcher("base64");
var contentMatcher = new ExactObjectMatcher(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEX/tID/vpH/pWX/sHidUyjlAAAADElEQVR4XmMQYNgAAADkAMHebX3mAAAAAElFTkSuQmCC"));
var contentMatcher = new ExactObjectMatcher(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4\r\n//8wEzIABCMDgAEMwEAAAwAA//8DAKkCBf4AAAAASUVORK5CYII="));

var matcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, contentTypeMatcher, contentDispositionMatcher, contentTransferEncodingMatcher, contentMatcher);
var result = matcher.IsMatch(part);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,44 @@ public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsMultiPart_Mixed_Wit
score.Should().Be(MatchScores.Perfect);
}

[Fact]
public void RequestMessageBodyMatcher_GetMatchingScore_BodyAsMultiPart_Issue1371()
{
var body = new BodyData
{
BodyAsString =
"--------------------------woli8b80pw4vBJtNpAMOKS\r\nContent-Disposition: form-data; name=\"metadata\"\r\nContent-Type: application/json\r\n\r\n{\"ID\": \"9858013b-e020-4ef9-b8a8-0bebc740e6a7\", \"DATE\": \"2025-08-15T13:45:30.0000000Z\", \"NAME\": \"32c9a8dd-e214-4afb-9611-9cde81f827c6\", \"NUMBER\": 10}\r\n--------------------------woli8b80pw4vBJtNpAMOKS--\r\n",
DetectedBodyType = BodyType.MultiPart
};

var headers = new Dictionary<string, string[]>
{
{ "Content-Type", [@"multipart/form-data; boundary=------------------------woli8b80pw4vBJtNpAMOKS"] }
};
var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body, headers);

var bodyMatcher = new MimePartMatcher(
MatchBehaviour.AcceptOnMatch,
new ContentTypeMatcher("application/json"),
null, // Content-Disposition
null, // Content-Transfer-Encoding
new JsonPartialMatcher(new { id = "9858013b-e020-4ef9-b8a8-0bebc740e6a7" }, true)
);

var matchers = new[] { bodyMatcher }
.OfType<IMatcher>()
.ToArray();

var matcher = new RequestMessageMultiPartMatcher(MatchBehaviour.AcceptOnMatch, MatchOperator.Or, matchers!);

// Act
var result = new RequestMatchResult();
var score = matcher.GetMatchingScore(requestMessage, result);

// Assert
score.Should().Be(MatchScores.Perfect);
}

private static double GetScore(IMatcher? matcher1, IMatcher? matcher2, IMatcher? matcher3, MatchOperator matchOperator = MatchOperator.And)
{
// Assign
Expand Down
Loading