mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-12-06 15:06:45 +01:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d4541c3a46 | ||
|
|
ef93f7417d | ||
|
|
acbc33f04e |
No files matched your search
@@ -21,6 +21,9 @@ EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.Tests", "test\WireMock.Net.Tests\WireMock.Net.Tests.csproj", "{31DC2EF8-C3FE-467D-84BE-FB5D956E612E}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{985E0ADB-D4B4-473A-AA40-567E279B7946}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
examples\Issue1553.cs = examples\Issue1553.cs
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7EFB2C5B-1BB2-4AAF-BC9F-216ED80C594D}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
<Project>
|
||||
<PropertyGroup>
|
||||
<MsBuildAllProjects>$(MsBuildAllProjects);$(MsBuildThisFileFullPath)</MsBuildAllProjects>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,20 @@
|
||||
// #:package WireMock.Net@2.35.0
|
||||
#:project ../src/WireMock.Net/WireMock.Net.csproj
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
|
||||
static bool MimePluginLoaded() => AppDomain.CurrentDomain.GetAssemblies()
|
||||
.Any(a => a.GetName().Name == "WireMock.Net.MimePart");
|
||||
|
||||
using var server = WireMockServer.Start();
|
||||
server.Given(Request.Create().WithPath("/ping").UsingGet())
|
||||
.RespondWith(Response.Create().WithStatusCode(200));
|
||||
|
||||
using var client = server.CreateClient();
|
||||
Console.WriteLine($"Before GET: {MimePluginLoaded()}");
|
||||
using var response = await client.GetAsync("/ping");
|
||||
Console.WriteLine($"After GET ({(int)response.StatusCode}): {MimePluginLoaded()}");
|
||||
@@ -21,6 +21,8 @@ public static class MainApp
|
||||
{
|
||||
using var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
PreloadMimeKitUtils = true,
|
||||
|
||||
Port = 9091,
|
||||
StartAdminInterface = true,
|
||||
|
||||
|
||||
@@ -286,6 +286,9 @@ namespace WireMock.Net.ConsoleApplication
|
||||
|
||||
var server = WireMockServer.Start(new WireMockServerSettings
|
||||
{
|
||||
PreloadMimeKitUtils = true,
|
||||
PreloadProtoBufUtils = true,
|
||||
|
||||
StartAdminInterface = true,
|
||||
Logger = new WireMockConsoleLogger(),
|
||||
Port = 9091
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
@@ -167,4 +168,11 @@ public interface IRequestMessage
|
||||
/// Gets the connection's client certificate
|
||||
/// </summary>
|
||||
X509Certificate2? ClientCertificate { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the multipart MIME header, if present.
|
||||
/// </summary>
|
||||
/// <param name="contentTypeHeader">When this method returns, contains the multipart MIME header values if found; otherwise, null.</param>
|
||||
/// <returns>true if a multipart MIME header is found; otherwise, false.</returns>
|
||||
bool TryGetMultipartMimeHeader([NotNullWhen(true)] out WireMockList<string>? contentTypeHeader);
|
||||
}
|
||||
@@ -1,9 +1,6 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using MimeKit;
|
||||
using Stef.Validation;
|
||||
@@ -27,45 +24,36 @@ internal class MimeKitUtils : IMimeKitUtils
|
||||
{
|
||||
Guard.NotNull(requestMessage);
|
||||
|
||||
if (requestMessage.BodyData != null &&
|
||||
requestMessage.Headers?.TryGetValue(HttpKnownHeaderNames.ContentType, out var contentTypeHeader) == true &&
|
||||
StartsWithMultiPart(contentTypeHeader)
|
||||
)
|
||||
if (!requestMessage.TryGetMultipartMimeHeader(out var contentTypeHeader))
|
||||
{
|
||||
byte[] bytes;
|
||||
|
||||
switch (requestMessage.BodyData?.DetectedBodyType)
|
||||
{
|
||||
// If the body is bytes, use the BodyAsBytes to match on.
|
||||
case BodyType.Bytes:
|
||||
bytes = requestMessage.BodyData.BodyAsBytes!;
|
||||
break;
|
||||
|
||||
// If the body is a String or MultiPart, use the BodyAsString to match on.
|
||||
case BodyType.String or BodyType.MultiPart:
|
||||
bytes = Encoding.UTF8.GetBytes(requestMessage.BodyData.BodyAsString!);
|
||||
break;
|
||||
|
||||
// Else return false.
|
||||
default:
|
||||
mimeMessageData = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
var fixedBytes = PrependContentTypeHeader(bytes, contentTypeHeader[0]);
|
||||
|
||||
mimeMessageData = LoadFromStream(new MemoryStream(fixedBytes));
|
||||
return true;
|
||||
mimeMessageData = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
mimeMessageData = null;
|
||||
return false;
|
||||
}
|
||||
byte[] bytes;
|
||||
switch (requestMessage.BodyData?.DetectedBodyType)
|
||||
{
|
||||
// If the body is bytes, use the BodyAsBytes to match on.
|
||||
case BodyType.Bytes:
|
||||
bytes = requestMessage.BodyData.BodyAsBytes!;
|
||||
break;
|
||||
|
||||
// If the body is a String or MultiPart, use the BodyAsString to match on.
|
||||
case BodyType.String or BodyType.MultiPart:
|
||||
bytes = Encoding.UTF8.GetBytes(requestMessage.BodyData.BodyAsString!);
|
||||
break;
|
||||
|
||||
private static bool StartsWithMultiPart(WireMockList<string> contentTypeHeader)
|
||||
{
|
||||
return contentTypeHeader.Any(ct => ct.TrimStart().StartsWith("multipart/", StringComparison.OrdinalIgnoreCase));
|
||||
// Else return false.
|
||||
default:
|
||||
mimeMessageData = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
var fixedBytes = PrependContentTypeHeader(bytes, contentTypeHeader[0]);
|
||||
|
||||
using var stream = new MemoryStream(fixedBytes);
|
||||
mimeMessageData = LoadFromStream(stream);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -2,12 +2,14 @@
|
||||
|
||||
// This source file is based on mock4net by Alexandre Victoor which is licensed under the Apache 2.0 License.
|
||||
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
|
||||
using System.Linq;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text.Json.Serialization;
|
||||
using Stef.Validation;
|
||||
using WireMock.Http;
|
||||
using WireMock.Models;
|
||||
using WireMock.Models.Mime;
|
||||
using WireMock.Owin;
|
||||
using WireMock.Types;
|
||||
using WireMock.Util;
|
||||
@@ -171,7 +173,8 @@ public class RequestMessage : IRequestMessage
|
||||
QueryIgnoreCase = new Dictionary<string, WireMockList<string>>(Query, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
ClientCertificate = clientCertificate;
|
||||
if (TypeLoader.TryLoadStaticInstance<IMimeKitUtils>(out var mimeKitUtils) && mimeKitUtils.TryGetMimeMessage(this, out var mimeMessage))
|
||||
|
||||
if (TryLoadBodyAsMimeMessage(out var mimeMessage))
|
||||
{
|
||||
BodyAsMimeMessage = mimeMessage;
|
||||
}
|
||||
@@ -188,4 +191,31 @@ public class RequestMessage : IRequestMessage
|
||||
var query = !ignoreCase ? Query : new Dictionary<string, WireMockList<string>>(Query, StringComparer.OrdinalIgnoreCase);
|
||||
return query.TryGetValue(key, out var value) ? value : null;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool TryGetMultipartMimeHeader([NotNullWhen(true)] out WireMockList<string>? contentTypeHeader)
|
||||
{
|
||||
if (BodyData != null &&
|
||||
Headers?.TryGetValue(HttpKnownHeaderNames.ContentType, out contentTypeHeader) == true &&
|
||||
contentTypeHeader.Any(ct => ct.TrimStart().StartsWith("multipart/", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
contentTypeHeader = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryLoadBodyAsMimeMessage([NotNullWhen(true)] out IMimeMessageData? mimeMessage)
|
||||
{
|
||||
if (TryGetMultipartMimeHeader(out _) &&
|
||||
TypeLoader.TryLoadStaticInstance<IMimeKitUtils>(out var mimeKitUtils) &&
|
||||
mimeKitUtils.TryGetMimeMessage(this, out mimeMessage))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
mimeMessage = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -366,6 +366,18 @@ public partial class WireMockServer : IWireMockServer
|
||||
_settings.Logger.Info("By Stef Heyenrath (https://github.com/wiremock/WireMock.Net)");
|
||||
_settings.Logger.Debug("Server settings {0}", JsonConvert.SerializeObject(settings, Formatting.Indented));
|
||||
|
||||
if (settings.PreloadMimeKitUtils == true)
|
||||
{
|
||||
_settings.Logger.Info("Preloading MimeKitUtils");
|
||||
TypeLoader.TryLoadStaticInstance<IMimeKitUtils>(out _);
|
||||
}
|
||||
|
||||
if (settings.PreloadProtoBufUtils == true)
|
||||
{
|
||||
_settings.Logger.Info("Preloading ProtoBufUtils");
|
||||
TypeLoader.TryLoadStaticInstance<IProtoBufUtils>(out _);
|
||||
}
|
||||
|
||||
HostUrlOptions urlOptions;
|
||||
if (settings.Urls != null)
|
||||
{
|
||||
|
||||
@@ -62,6 +62,8 @@ public static class WireMockServerSettingsParser
|
||||
HandleRequestsSynchronously = parser.GetBoolValue(nameof(WireMockServerSettings.HandleRequestsSynchronously)),
|
||||
HostingScheme = parser.GetEnumValue<HostingScheme>(nameof(WireMockServerSettings.HostingScheme)),
|
||||
MaxRequestLogCount = parser.GetIntValue(nameof(WireMockServerSettings.MaxRequestLogCount)),
|
||||
PreloadMimeKitUtils = parser.GetBoolValue(nameof(WireMockServerSettings.PreloadMimeKitUtils)),
|
||||
PreloadProtoBufUtils = parser.GetBoolValue(nameof(WireMockServerSettings.PreloadProtoBufUtils)),
|
||||
QueryParameterMultipleValueSupport = parser.GetEnumValue<QueryParameterMultipleValueSupport>(nameof(WireMockServerSettings.QueryParameterMultipleValueSupport)),
|
||||
ReadStaticMappings = parser.GetBoolValue(nameof(WireMockServerSettings.ReadStaticMappings)),
|
||||
RequestLogExpirationDuration = parser.GetIntValue(nameof(WireMockServerSettings.RequestLogExpirationDuration)),
|
||||
|
||||
@@ -63,6 +63,20 @@ public class WireMockServerSettings
|
||||
[PublicAPI]
|
||||
public bool? ReadStaticMappings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Preload MimeKit utilities during server startup.
|
||||
/// Default is <c>false</c>. Set to <c>true</c> to improve performance for the first request with a multipart MIME body.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public bool? PreloadMimeKitUtils { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Preload ProtoBuf utilities during server startup.
|
||||
/// Default is <c>false</c>. Set to <c>true</c> to improve performance for the first ProtoBuf request.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public bool? PreloadProtoBufUtils { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Watch the static mapping files + folder for changes when running.
|
||||
/// </summary>
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace WireMock.Net.Tests.Matchers;
|
||||
|
||||
public class MimePartMatcherTests
|
||||
{
|
||||
private static readonly IMimeKitUtils MimeKitUtils = new MimeKitUtils();
|
||||
private static readonly MimeKitUtils MimeKitUtils = new();
|
||||
|
||||
private const string TestMultiPart =
|
||||
"""
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using WireMock.Models;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Net.Tests;
|
||||
|
||||
@@ -82,4 +83,37 @@ public class RequestMessageTests
|
||||
request.GetParameter("key").Should().Contain("2");
|
||||
request.GetParameter("key").Should().Contain("3");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestMessage_TryGetMultipartMimeHeader_WithMultipartContentType()
|
||||
{
|
||||
// Arrange
|
||||
var contentType = " MULTIPART/form-data; boundary=boundary";
|
||||
var body = new BodyData();
|
||||
var headers = new Dictionary<string, string[]> { { "Content-Type", [contentType] } };
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body, headers);
|
||||
|
||||
// Act
|
||||
var result = request.TryGetMultipartMimeHeader(out var contentTypeHeader);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
contentTypeHeader.Should().ContainSingle(contentType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RequestMessage_TryGetMultipartMimeHeader_WithNonMultipartContentType()
|
||||
{
|
||||
// Arrange
|
||||
var body = new BodyData();
|
||||
var headers = new Dictionary<string, string[]> { { "Content-Type", ["application/json"] } };
|
||||
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "POST", ClientIp, body, headers);
|
||||
|
||||
// Act
|
||||
var result = request.TryGetMultipartMimeHeader(out var contentTypeHeader);
|
||||
|
||||
// Assert
|
||||
result.Should().BeFalse();
|
||||
contentTypeHeader.Should().BeNull();
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,36 @@ public class WireMockServerSettingsParserTests
|
||||
settings!.AdminPath.Should().Be("/__admin");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryParseArguments_With_PreloadMimeKitUtils()
|
||||
{
|
||||
// Act
|
||||
var result = WireMockServerSettingsParser.TryParseArguments(new[]
|
||||
{
|
||||
"--PreloadMimeKitUtils", "true"
|
||||
}, null, out var settings);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
settings.Should().NotBeNull();
|
||||
settings!.PreloadMimeKitUtils.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryParseArguments_With_PreloadProtoBufUtils()
|
||||
{
|
||||
// Act
|
||||
var result = WireMockServerSettingsParser.TryParseArguments(new[]
|
||||
{
|
||||
"--PreloadProtoBufUtils", "true"
|
||||
}, null, out var settings);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
settings.Should().NotBeNull();
|
||||
settings!.PreloadProtoBufUtils.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryParseArguments_With_ActivityTracingEnabled_ShouldParseOptions()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user