diff --git a/WireMock.Net Solution.sln b/WireMock.Net Solution.sln
index 91c951776..4356cc40e 100644
--- a/WireMock.Net Solution.sln
+++ b/WireMock.Net Solution.sln
@@ -116,6 +116,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.Console.GrpcCl
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.GraphQL", "src\WireMock.Net.GraphQL\WireMock.Net.GraphQL.csproj", "{B6269AAC-170A-4346-8B9A-444DED3D9A45}"
EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.MimeKitLite", "src\WireMock.Net.MimeKitLite\WireMock.Net.MimeKitLite.csproj", "{F8B4A93E-46EF-4237-88FE-15FDAB7635C2}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -274,6 +276,10 @@ Global
{B6269AAC-170A-4346-8B9A-444DED3D9A45}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6269AAC-170A-4346-8B9A-444DED3D9A45}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6269AAC-170A-4346-8B9A-444DED3D9A45}.Release|Any CPU.Build.0 = Release|Any CPU
+ {F8B4A93E-46EF-4237-88FE-15FDAB7635C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {F8B4A93E-46EF-4237-88FE-15FDAB7635C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {F8B4A93E-46EF-4237-88FE-15FDAB7635C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {F8B4A93E-46EF-4237-88FE-15FDAB7635C2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -319,6 +325,7 @@ Global
{7FC0B409-2682-40EE-B3B9-3930D6769D01} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
{B1580A38-84E7-44BE-8FE7-3EE5031D74A1} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
{B6269AAC-170A-4346-8B9A-444DED3D9A45} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2}
+ {F8B4A93E-46EF-4237-88FE-15FDAB7635C2} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DC539027-9852-430C-B19F-FD035D018458}
diff --git a/src/WireMock.Net.Abstractions/IRequestMessage.cs b/src/WireMock.Net.Abstractions/IRequestMessage.cs
index 42d9c99fd..fe3e44303 100644
--- a/src/WireMock.Net.Abstractions/IRequestMessage.cs
+++ b/src/WireMock.Net.Abstractions/IRequestMessage.cs
@@ -116,13 +116,11 @@ public interface IRequestMessage
///
byte[]? BodyAsBytes { get; }
-#if MIMEKIT
///
/// The original body as MimeMessage.
/// Convenience getter for Handlebars and WireMockAssertions.
///
object? BodyAsMimeMessage { get; }
-#endif
///
/// The detected body type. Convenience getter for Handlebars.
diff --git a/src/WireMock.Net.Abstractions/WireMock.Net.Abstractions.csproj b/src/WireMock.Net.Abstractions/WireMock.Net.Abstractions.csproj
index 9cf9ca4f6..b1c504e99 100644
--- a/src/WireMock.Net.Abstractions/WireMock.Net.Abstractions.csproj
+++ b/src/WireMock.Net.Abstractions/WireMock.Net.Abstractions.csproj
@@ -31,7 +31,7 @@
- $(DefineConstants);MIMEKIT;PROTOBUF
+ $(DefineConstants);PROTOBUF
diff --git a/src/WireMock.Net.GraphQL/WireMock.Net.GraphQL.csproj b/src/WireMock.Net.GraphQL/WireMock.Net.GraphQL.csproj
index 4f6a77784..a873daef4 100644
--- a/src/WireMock.Net.GraphQL/WireMock.Net.GraphQL.csproj
+++ b/src/WireMock.Net.GraphQL/WireMock.Net.GraphQL.csproj
@@ -1,7 +1,7 @@
- A GraphQL which can be used to match WireMock.Net Requests.
+ GraphQL support for WireMock.Net
WireMock.Net.Matchers.GraphQL
Stef Heyenrath
netstandard2.0;netstandard2.1;net5.0;net6.0;net8.0
@@ -10,7 +10,6 @@
WireMock
{B6269AAC-170A-4346-8B9A-444DED3D9A45}
true
-
true
true
true
diff --git a/src/WireMock.Net/Matchers/MimePartMatcher.cs b/src/WireMock.Net.MimeKitLite/Matchers/MimePartMatcher.cs
similarity index 88%
rename from src/WireMock.Net/Matchers/MimePartMatcher.cs
rename to src/WireMock.Net.MimeKitLite/Matchers/MimePartMatcher.cs
index 145052826..fe6a84960 100644
--- a/src/WireMock.Net/Matchers/MimePartMatcher.cs
+++ b/src/WireMock.Net.MimeKitLite/Matchers/MimePartMatcher.cs
@@ -1,9 +1,6 @@
-#if MIMEKIT
using System;
using MimeKit;
-using WireMock.Matchers;
using WireMock.Matchers.Helpers;
-using WireMock.Models;
using WireMock.Util;
namespace WireMock.Matchers;
@@ -11,31 +8,23 @@ namespace WireMock.Matchers;
///
/// MimePartMatcher
///
-public class MimePartMatcher : IMatcher
+public class MimePartMatcher : IMimePartMatcher
{
private readonly Func[] _funcs;
///
public string Name => nameof(MimePartMatcher);
- ///
- /// ContentType Matcher (image/png; name=image.png.)
- ///
+ ///
public IStringMatcher? ContentTypeMatcher { get; }
- ///
- /// ContentDisposition Matcher (attachment; filename=image.png)
- ///
+ ///
public IStringMatcher? ContentDispositionMatcher { get; }
- ///
- /// ContentTransferEncoding Matcher (base64)
- ///
+ ///
public IStringMatcher? ContentTransferEncodingMatcher { get; }
- ///
- /// Content Matcher
- ///
+ ///
public IMatcher? ContentMatcher { get; }
///
@@ -116,5 +105,4 @@ private MatchResult MatchOnContent(MimePart mimePart)
{
return contentType?.ToString().Replace("Content-Type: ", string.Empty);
}
-}
-#endif
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageMultiPartMatcher.cs b/src/WireMock.Net.MimeKitLite/Matchers/Request/RequestMessageMultiPartMatcher.cs
similarity index 85%
rename from src/WireMock.Net/Matchers/Request/RequestMessageMultiPartMatcher.cs
rename to src/WireMock.Net.MimeKitLite/Matchers/Request/RequestMessageMultiPartMatcher.cs
index 4695cb71b..600ba9e23 100644
--- a/src/WireMock.Net/Matchers/Request/RequestMessageMultiPartMatcher.cs
+++ b/src/WireMock.Net.MimeKitLite/Matchers/Request/RequestMessageMultiPartMatcher.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using MimeKit;
using Stef.Validation;
using WireMock.Util;
@@ -9,16 +10,12 @@ namespace WireMock.Matchers.Request;
///
/// The request body MultiPart matcher.
///
-public class RequestMessageMultiPartMatcher : IRequestMatcher
+public class RequestMessageMultiPartMatcher : IRequestMessageMultiPartMatcher
{
- ///
- /// The matchers.
- ///
+ ///
public IMatcher[]? Matchers { get; }
- ///
- /// The
- ///
+ ///
public MatchOperator MatchOperator { get; } = MatchOperator.Or;
///
@@ -26,6 +23,8 @@ public class RequestMessageMultiPartMatcher : IRequestMatcher
///
public MatchBehaviour MatchBehaviour { get; }
+ private readonly MimeKitUtils _mimeKitUtils = new();
+
///
/// Initializes a new instance of the class.
///
@@ -51,9 +50,6 @@ public RequestMessageMultiPartMatcher(MatchBehaviour matchBehaviour, MatchOperat
///
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
{
-#if !MIMEKIT
- throw new System.NotSupportedException("The MultiPartMatcher can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
-#else
var score = MatchScores.Mismatch;
Exception? exception = null;
@@ -62,7 +58,7 @@ public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResu
return requestMatchResult.AddScore(GetType(), score, null);
}
- if (!MimeKitUtils.TryGetMimeMessage(requestMessage, out var message))
+ if (!_mimeKitUtils.TryGetMimeMessage(requestMessage, out MimeMessage? message))
{
return requestMatchResult.AddScore(GetType(), score, null);
}
@@ -71,7 +67,7 @@ public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResu
{
var mimePartMatchers = Matchers.OfType().ToArray();
- foreach (var mimePart in message.BodyParts.OfType())
+ foreach (var mimePart in message.BodyParts.OfType())
{
var matchesForMimePart = new List { default };
matchesForMimePart.AddRange(mimePartMatchers.Select(matcher => matcher.IsMatch(mimePart)));
@@ -98,6 +94,5 @@ public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResu
}
return requestMatchResult.AddScore(GetType(), score, exception);
-#endif
}
}
\ No newline at end of file
diff --git a/src/WireMock.Net.MimeKitLite/Properties/AssemblyInfo.cs b/src/WireMock.Net.MimeKitLite/Properties/AssemblyInfo.cs
new file mode 100644
index 000000000..ee5b32150
--- /dev/null
+++ b/src/WireMock.Net.MimeKitLite/Properties/AssemblyInfo.cs
@@ -0,0 +1,6 @@
+using System.Runtime.CompilerServices;
+
+[assembly: InternalsVisibleTo("WireMock.Net.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
+
+// Needed for Moq in the UnitTest project
+[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
\ No newline at end of file
diff --git a/src/WireMock.Net.MimeKitLite/RequestBuilders/IRequestBuilderExtensions.cs b/src/WireMock.Net.MimeKitLite/RequestBuilders/IRequestBuilderExtensions.cs
new file mode 100644
index 000000000..007eb05ca
--- /dev/null
+++ b/src/WireMock.Net.MimeKitLite/RequestBuilders/IRequestBuilderExtensions.cs
@@ -0,0 +1,47 @@
+// ReSharper disable InconsistentNaming
+using WireMock.Matchers;
+using WireMock.Matchers.Request;
+
+namespace WireMock.RequestBuilders;
+
+///
+/// IRequestBuilderExtensions extensions for MultiPart Mime using MimeKitLite.
+///
+public static class IRequestBuilderExtensions
+{
+ ///
+ /// WithMultiPart: IMatcher
+ ///
+ /// The .
+ /// The matcher.
+ /// The .
+ public static IRequestBuilder WithMultiPart(this IRequestBuilder requestBuilder, IMatcher matcher)
+ {
+ return requestBuilder.Add(new RequestMessageMultiPartMatcher(matcher));
+ }
+
+ ///
+ /// WithMultiPart: IMatcher[], MatchBehaviour and MatchOperator
+ ///
+ /// The .
+ /// The matchers.
+ /// The to use.
+ /// The to use.
+ /// The .
+ public static IRequestBuilder WithMultiPart(this IRequestBuilder requestBuilder, IMatcher[] matchers, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch, MatchOperator matchOperator = MatchOperator.Or)
+ {
+ return requestBuilder.Add(new RequestMessageMultiPartMatcher(matchBehaviour, matchOperator, matchers));
+ }
+
+ ///
+ /// WithMultiPart: MatchBehaviour and IMatcher[]
+ ///
+ /// The .
+ /// The to use.
+ /// The matchers.
+ /// The .
+ public static IRequestBuilder WithMultiPart(this IRequestBuilder requestBuilder, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch, params IMatcher[] matchers)
+ {
+ return requestBuilder.Add(new RequestMessageMultiPartMatcher(matchBehaviour, MatchOperator.Or, matchers));
+ }
+}
\ No newline at end of file
diff --git a/src/WireMock.Net/Util/MimeKitUtils.cs b/src/WireMock.Net.MimeKitLite/Util/MimeKitUtils.cs
similarity index 82%
rename from src/WireMock.Net/Util/MimeKitUtils.cs
rename to src/WireMock.Net.MimeKitLite/Util/MimeKitUtils.cs
index 9bb5126e4..ea83c7c51 100644
--- a/src/WireMock.Net/Util/MimeKitUtils.cs
+++ b/src/WireMock.Net.MimeKitLite/Util/MimeKitUtils.cs
@@ -1,4 +1,3 @@
-#if MIMEKIT
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
@@ -11,9 +10,16 @@
namespace WireMock.Util;
-internal static class MimeKitUtils
+internal class MimeKitUtils : IMimeKitUtils
{
- public static bool TryGetMimeMessage(IRequestMessage requestMessage, [NotNullWhen(true)] out MimeMessage? mimeMessage)
+ public bool TryGetMimeMessage(IRequestMessage requestMessage, [NotNullWhen(true)] out object? mimeMessage)
+ {
+ var result = TryGetMimeMessage(requestMessage, out MimeMessage? value);
+ mimeMessage = value;
+ return result;
+ }
+
+ public bool TryGetMimeMessage(IRequestMessage requestMessage, [NotNullWhen(true)] out MimeMessage? mimeMessage)
{
Guard.NotNull(requestMessage);
@@ -59,5 +65,4 @@ private static byte[] FixBytes(byte[] bytes, WireMockList contentType)
return result;
}
-}
-#endif
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/src/WireMock.Net.MimeKitLite/WireMock.Net.MimeKitLite.csproj b/src/WireMock.Net.MimeKitLite/WireMock.Net.MimeKitLite.csproj
new file mode 100644
index 000000000..45ed9147d
--- /dev/null
+++ b/src/WireMock.Net.MimeKitLite/WireMock.Net.MimeKitLite.csproj
@@ -0,0 +1,36 @@
+
+
+
+ MultiPart Mime support for WireMock.Net
+ WireMock.Net.Matchers.GraphQL
+ Stef Heyenrath
+ netstandard2.0;netstandard2.1;net462;net47;net48;net6.0;net8.0
+ true
+ wiremock;matchers;matcher;graphql
+ WireMock
+ {F8B4A93E-46EF-4237-88FE-15FDAB7635D4}
+ true
+ true
+ true
+ true
+ ../WireMock.Net/WireMock.Net.ruleset
+ true
+ ../WireMock.Net/WireMock.Net.snk
+
+ true
+ MIT
+
+
+
+ true
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/WireMock.Net/Matchers/IGraphQLMatcher.cs b/src/WireMock.Net/Matchers/IGraphQLMatcher.cs
index 4fc5767bb..b7a824143 100644
--- a/src/WireMock.Net/Matchers/IGraphQLMatcher.cs
+++ b/src/WireMock.Net/Matchers/IGraphQLMatcher.cs
@@ -6,7 +6,7 @@ namespace WireMock.Matchers;
///
/// GraphQLMatcher
///
-///
+///
public interface IGraphQLMatcher : IStringMatcher
{
///
diff --git a/src/WireMock.Net/Matchers/IMimePartMatcher.cs b/src/WireMock.Net/Matchers/IMimePartMatcher.cs
new file mode 100644
index 000000000..d2fda6732
--- /dev/null
+++ b/src/WireMock.Net/Matchers/IMimePartMatcher.cs
@@ -0,0 +1,28 @@
+namespace WireMock.Matchers;
+
+///
+/// MimePartMatcher
+///
+///
+public interface IMimePartMatcher : IMatcher
+{
+ ///
+ /// ContentType Matcher (image/png; name=image.png.)
+ ///
+ IStringMatcher? ContentTypeMatcher { get; }
+
+ ///
+ /// ContentDisposition Matcher (attachment; filename=image.png)
+ ///
+ IStringMatcher? ContentDispositionMatcher { get; }
+
+ ///
+ /// ContentTransferEncoding Matcher (base64)
+ ///
+ IStringMatcher? ContentTransferEncodingMatcher { get; }
+
+ ///
+ /// Content Matcher
+ ///
+ IMatcher? ContentMatcher { get; }
+}
\ No newline at end of file
diff --git a/src/WireMock.Net/Matchers/Request/IRequestMessageMultiPartMatcher.cs b/src/WireMock.Net/Matchers/Request/IRequestMessageMultiPartMatcher.cs
new file mode 100644
index 000000000..269f989f8
--- /dev/null
+++ b/src/WireMock.Net/Matchers/Request/IRequestMessageMultiPartMatcher.cs
@@ -0,0 +1,17 @@
+namespace WireMock.Matchers.Request;
+
+///
+/// The request body MultiPart matcher.
+///
+public interface IRequestMessageMultiPartMatcher : IRequestMatcher
+{
+ ///
+ /// The matchers.
+ ///
+ IMatcher[]? Matchers { get; }
+
+ ///
+ /// The
+ ///
+ MatchOperator MatchOperator { get; }
+}
\ No newline at end of file
diff --git a/src/WireMock.Net/Properties/AssemblyInfo.cs b/src/WireMock.Net/Properties/AssemblyInfo.cs
index a032f0efb..84691ea2e 100644
--- a/src/WireMock.Net/Properties/AssemblyInfo.cs
+++ b/src/WireMock.Net/Properties/AssemblyInfo.cs
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
+[assembly: InternalsVisibleTo("WireMock.Net.MimeKitLite, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
[assembly: InternalsVisibleTo("WireMock.Net.GraphQL, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
[assembly: InternalsVisibleTo("WireMock.Net.Matchers.CSharpCode, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
[assembly: InternalsVisibleTo("WireMock.Net.StandAlone, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
diff --git a/src/WireMock.Net/RequestBuilders/IMultiPartRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IMultiPartRequestBuilder.cs
deleted file mode 100644
index 4db8bc175..000000000
--- a/src/WireMock.Net/RequestBuilders/IMultiPartRequestBuilder.cs
+++ /dev/null
@@ -1,34 +0,0 @@
-using WireMock.Matchers;
-using WireMock.Matchers.Request;
-
-namespace WireMock.RequestBuilders;
-
-///
-/// The MultiPartRequestBuilder interface.
-///
-public interface IMultiPartRequestBuilder : IHttpVersionBuilder
-{
- ///
- /// WithMultiPart: IMatcher
- ///
- /// The matcher.
- /// The .
- IRequestBuilder WithMultiPart(IMatcher matcher);
-
- ///
- /// WithMultiPart: IMatcher[], MatchBehaviour and MatchOperator
- ///
- /// The matchers.
- /// The to use.
- /// The to use.
- /// The .
- IRequestBuilder WithMultiPart(IMatcher[] matchers, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch, MatchOperator matchOperator = MatchOperator.Or);
-
- ///
- /// WithMultiPart: MatchBehaviour and IMatcher[]
- ///
- /// The to use.
- /// The matchers.
- /// The .
- IRequestBuilder WithMultiPart(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch, params IMatcher[] matchers);
-}
\ No newline at end of file
diff --git a/src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs
index f5db82fda..4559ee8bb 100644
--- a/src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs
+++ b/src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs
@@ -5,7 +5,7 @@ namespace WireMock.RequestBuilders;
///
/// The ProtoBufRequestBuilder interface.
///
-public interface IProtoBufRequestBuilder : IMultiPartRequestBuilder
+public interface IProtoBufRequestBuilder : IHttpVersionBuilder
{
///
/// WithGrpcProto
diff --git a/src/WireMock.Net/RequestBuilders/Request.WithMultiPart.cs b/src/WireMock.Net/RequestBuilders/Request.WithMultiPart.cs
deleted file mode 100644
index 64011c307..000000000
--- a/src/WireMock.Net/RequestBuilders/Request.WithMultiPart.cs
+++ /dev/null
@@ -1,28 +0,0 @@
-using WireMock.Matchers;
-using WireMock.Matchers.Request;
-
-namespace WireMock.RequestBuilders;
-
-public partial class Request
-{
- ///
- public IRequestBuilder WithMultiPart(IMatcher matcher)
- {
- _requestMatchers.Add(new RequestMessageMultiPartMatcher(matcher));
- return this;
- }
-
- ///
- public IRequestBuilder WithMultiPart(IMatcher[] matchers, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch, MatchOperator matchOperator = MatchOperator.Or)
- {
- _requestMatchers.Add(new RequestMessageMultiPartMatcher(matchBehaviour, matchOperator, matchers));
- return this;
- }
-
- ///
- public IRequestBuilder WithMultiPart(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch, params IMatcher[] matchers)
- {
- _requestMatchers.Add(new RequestMessageMultiPartMatcher(matchBehaviour, MatchOperator.Or, matchers));
- return this;
- }
-}
\ No newline at end of file
diff --git a/src/WireMock.Net/RequestMessage.cs b/src/WireMock.Net/RequestMessage.cs
index 20b4518e9..50a86e37f 100644
--- a/src/WireMock.Net/RequestMessage.cs
+++ b/src/WireMock.Net/RequestMessage.cs
@@ -80,11 +80,9 @@ public class RequestMessage : IRequestMessage
///
public byte[]? BodyAsBytes { get; }
-#if MIMEKIT
///
[Newtonsoft.Json.JsonIgnore] // Issue 1001
public object? BodyAsMimeMessage { get; }
-#endif
///
public string? DetectedBodyType { get; }
@@ -180,19 +178,17 @@ internal RequestMessage(
ClientCertificate = clientCertificate;
#endif
-#if MIMEKIT
try
{
- if (MimeKitUtils.TryGetMimeMessage(this, out var mimeMessage))
+ if (TypeLoader.TryFindType(out _) && TypeLoader.Load().TryGetMimeMessage(this, out var mimeMessage))
{
BodyAsMimeMessage = mimeMessage;
}
}
catch
{
- // Ignore exception from MimeMessage.Load
+ // Ignore exception from IMimeKitUtils.TryGetMimeMessage
}
-#endif
}
///
diff --git a/src/WireMock.Net/Serialization/MappingConverter.cs b/src/WireMock.Net/Serialization/MappingConverter.cs
index da4aae564..ce80c6ed4 100644
--- a/src/WireMock.Net/Serialization/MappingConverter.cs
+++ b/src/WireMock.Net/Serialization/MappingConverter.cs
@@ -49,7 +49,7 @@ public string ToCSharpCode(IMapping mapping, MappingConverterSettings? settings
var requestMessageBodyMatcher = request.GetRequestMessageMatcher();
var requestMessageHttpVersionMatcher = request.GetRequestMessageMatcher();
var requestMessageGraphQLMatcher = request.GetRequestMessageMatcher();
- var requestMessageMultiPartMatcher = request.GetRequestMessageMatcher();
+ var requestMessageMultiPartMatcher = request.GetRequestMessageMatcher();
var requestMessageProtoBufMatcher = request.GetRequestMessageMatcher();
var sb = new StringBuilder();
@@ -122,15 +122,13 @@ public string ToCSharpCode(IMapping mapping, MappingConverterSettings? settings
}
}
-#if MIMEKIT
if (requestMessageMultiPartMatcher is { Matchers: { } })
{
- if (requestMessageMultiPartMatcher.Matchers.OfType().Any())
+ if (requestMessageMultiPartMatcher.Matchers.OfType().Any())
{
sb.AppendLine(" // .WithMultiPart() is not yet supported");
}
}
-#endif
#if PROTOBUF
if (requestMessageProtoBufMatcher is { Matcher: { } })
@@ -258,7 +256,7 @@ public MappingModel ToMappingModel(IMapping mapping)
var methodMatcher = request.GetRequestMessageMatcher();
var bodyMatcher = request.GetRequestMessageMatcher();
var graphQLMatcher = request.GetRequestMessageMatcher();
- var multiPartMatcher = request.GetRequestMessageMatcher();
+ var multiPartMatcher = request.GetRequestMessageMatcher();
var protoBufMatcher = request.GetRequestMessageMatcher();
var httpVersionMatcher = request.GetRequestMessageMatcher();
diff --git a/src/WireMock.Net/Serialization/MatcherMapper.cs b/src/WireMock.Net/Serialization/MatcherMapper.cs
index 7698fa728..d00199905 100644
--- a/src/WireMock.Net/Serialization/MatcherMapper.cs
+++ b/src/WireMock.Net/Serialization/MatcherMapper.cs
@@ -70,10 +70,8 @@ public MatcherMapper(WireMockServerSettings settings)
var schema = new AnyOf(stringPatterns[0].GetPattern());
return TypeLoader.Load(schema, matcherModel.CustomScalars, matchBehaviour, matchOperator);
-#if MIMEKIT
- case nameof(MimePartMatcher):
+ case "MimePartMatcher":
return CreateMimePartMatcher(matchBehaviour, matcherModel);
-#endif
#if PROTOBUF
case nameof(ProtoBufMatcher):
@@ -198,14 +196,12 @@ public MatcherMapper(WireMockServerSettings settings)
model.Pattern = objectMatcher.Value;
break;
-#if MIMEKIT
- case MimePartMatcher mimePartMatcher:
+ case IMimePartMatcher mimePartMatcher:
model.ContentDispositionMatcher = Map(mimePartMatcher.ContentDispositionMatcher);
model.ContentMatcher = Map(mimePartMatcher.ContentMatcher);
model.ContentTransferEncodingMatcher = Map(mimePartMatcher.ContentTransferEncodingMatcher);
model.ContentTypeMatcher = Map(mimePartMatcher.ContentTypeMatcher);
break;
-#endif
#if PROTOBUF
case ProtoBufMatcher protoBufMatcher:
@@ -263,17 +259,15 @@ private static ExactObjectMatcher CreateExactObjectMatcher(MatchBehaviour matchB
return new ExactObjectMatcher(matchBehaviour, bytePattern);
}
-#if MIMEKIT
- private MimePartMatcher CreateMimePartMatcher(MatchBehaviour matchBehaviour, MatcherModel matcher)
+ private IMimePartMatcher CreateMimePartMatcher(MatchBehaviour matchBehaviour, MatcherModel matcher)
{
- var contentTypeMatcher = Map(matcher?.ContentTypeMatcher) as IStringMatcher;
- var contentDispositionMatcher = Map(matcher?.ContentDispositionMatcher) as IStringMatcher;
- var contentTransferEncodingMatcher = Map(matcher?.ContentTransferEncodingMatcher) as IStringMatcher;
- var contentMatcher = Map(matcher?.ContentMatcher);
+ var contentTypeMatcher = Map(matcher.ContentTypeMatcher) as IStringMatcher;
+ var contentDispositionMatcher = Map(matcher.ContentDispositionMatcher) as IStringMatcher;
+ var contentTransferEncodingMatcher = Map(matcher.ContentTransferEncodingMatcher) as IStringMatcher;
+ var contentMatcher = Map(matcher.ContentMatcher);
- return new MimePartMatcher(matchBehaviour, contentTypeMatcher, contentDispositionMatcher, contentTransferEncodingMatcher, contentMatcher);
+ return TypeLoader.Load(matchBehaviour, contentTypeMatcher, contentDispositionMatcher, contentTransferEncodingMatcher, contentMatcher);
}
-#endif
#if PROTOBUF
private ProtoBufMatcher CreateProtoBufMatcher(MatchBehaviour? matchBehaviour, string protoDefinitionOrId, MatcherModel matcher)
diff --git a/src/WireMock.Net/Util/IMimeKitUtils.cs b/src/WireMock.Net/Util/IMimeKitUtils.cs
new file mode 100644
index 000000000..614e542a5
--- /dev/null
+++ b/src/WireMock.Net/Util/IMimeKitUtils.cs
@@ -0,0 +1,8 @@
+using System.Diagnostics.CodeAnalysis;
+
+namespace WireMock.Util;
+
+internal interface IMimeKitUtils
+{
+ bool TryGetMimeMessage(IRequestMessage requestMessage, [NotNullWhen(true)] out object? mimeMessage);
+}
\ No newline at end of file
diff --git a/src/WireMock.Net/Util/TypeLoader.cs b/src/WireMock.Net/Util/TypeLoader.cs
index 3786a6dcb..d52262480 100644
--- a/src/WireMock.Net/Util/TypeLoader.cs
+++ b/src/WireMock.Net/Util/TypeLoader.cs
@@ -10,43 +10,47 @@ namespace WireMock.Util;
internal static class TypeLoader
{
- private static readonly ConcurrentDictionary Assemblies = new();
+ private static readonly ConcurrentDictionary FoundTypes = new();
- public static TInterface Load(params object?[] args) where TInterface : class
+ public static bool TryFindType([NotNullWhen(true)] out Type? pluginType) where TInterface : class
{
var key = typeof(TInterface).FullName!;
- var pluginType = Assemblies.GetOrAdd(key, _ =>
- {
- if (TryFindTypeInDlls(null, out var foundType))
- {
- return foundType;
- }
-
- throw new DllNotFoundException($"No dll found which implements Interface '{key}'.");
- });
+ pluginType = FoundTypes.GetOrAdd(key, _ => TryFindTypeInDlls(null, out var foundType) ? foundType : null);
- return (TInterface)Activator.CreateInstance(pluginType, args)!;
+ return pluginType != null;
}
- public static TInterface LoadByFullName(string implementationTypeFullName, params object?[] args) where TInterface : class
+ public static bool TryFindType(string implementationTypeFullName, [NotNullWhen(true)] out Type? pluginType) where TInterface : class
{
- Guard.NotNullOrEmpty(implementationTypeFullName);
-
var @interface = typeof(TInterface).FullName;
var key = $"{@interface}_{implementationTypeFullName}";
- var pluginType = Assemblies.GetOrAdd(key, _ =>
+ pluginType = FoundTypes.GetOrAdd(key, _ => TryFindTypeInDlls(implementationTypeFullName, out var foundType) ? foundType : null);
+
+ return pluginType != null;
+ }
+
+ public static TInterface Load(params object?[] args) where TInterface : class
+ {
+ if (TryFindType(out var pluginType))
{
- if (TryFindTypeInDlls(implementationTypeFullName, out var foundType))
- {
- return foundType;
- }
+ return (TInterface)Activator.CreateInstance(pluginType, args)!;
+ }
+
+ throw new DllNotFoundException($"No dll found which implements Interface '{typeof(TInterface).FullName}'.");
+ }
- throw new DllNotFoundException($"No dll found which implements Interface '{@interface}' and has FullName '{implementationTypeFullName}'.");
- });
+ public static TInterface LoadByFullName(string implementationTypeFullName, params object?[] args) where TInterface : class
+ {
+ Guard.NotNullOrEmpty(implementationTypeFullName);
+
+ if (TryFindType(implementationTypeFullName, out var pluginType))
+ {
+ return (TInterface)Activator.CreateInstance(pluginType, args)!;
+ }
- return (TInterface)Activator.CreateInstance(pluginType, args)!;
+ throw new DllNotFoundException($"No dll found which implements Interface '{typeof(TInterface).FullName}' and has FullName '{implementationTypeFullName}'.");
}
private static bool TryFindTypeInDlls(string? implementationTypeFullName, [NotNullWhen(true)] out Type? pluginType) where TInterface : class
diff --git a/src/WireMock.Net/WireMock.Net.csproj b/src/WireMock.Net/WireMock.Net.csproj
index f0e59c832..01b4f14af 100644
--- a/src/WireMock.Net/WireMock.Net.csproj
+++ b/src/WireMock.Net/WireMock.Net.csproj
@@ -51,7 +51,7 @@
- $(DefineConstants);MIMEKIT;PROTOBUF
+ $(DefineConstants);PROTOBUF
@@ -150,9 +150,6 @@
-
-
diff --git a/test/WireMock.Net.Tests/Util/JsonUtilsTests.cs b/test/WireMock.Net.Tests/Util/JsonUtilsTests.cs
index 600d80d07..dc240bfc8 100644
--- a/test/WireMock.Net.Tests/Util/JsonUtilsTests.cs
+++ b/test/WireMock.Net.Tests/Util/JsonUtilsTests.cs
@@ -1,10 +1,5 @@
using System;
-using System.Linq;
-using System.Linq.Dynamic.Core;
-using System.Reflection;
using FluentAssertions;
-using Newtonsoft.Json.Linq;
-using NFluent;
using WireMock.Util;
using Xunit;
@@ -50,144 +45,4 @@ public void JsonUtils_ParseJTokenToObject_For_Invalid_Throws()
// Assert
action.Should().Throw();
}
-
- [Fact]
- public void JsonUtils_GenerateDynamicLinqStatement_JToken()
- {
- // Assign
- JToken instance = "Test";
-
- // Act
- string line = JsonUtils.GenerateDynamicLinqStatement(instance);
-
- // Assert
- var queryable = new[] { instance }.AsQueryable().Select(line);
- bool result = queryable.Any("it == \"Test\"");
- Check.That(result).IsTrue();
-
- Check.That(line).IsEqualTo("string(it)");
- }
-
- [Fact]
- public void JsonUtils_GenerateDynamicLinqStatement_JArray_Indexer()
- {
- // Assign
- var instance = new JObject
- {
- { "Items", new JArray(new JValue(4), new JValue(8)) }
- };
-
- // Act
- string line = JsonUtils.GenerateDynamicLinqStatement(instance);
-
- // Assert 1
- line.Should().Be("new ((new [] { long(Items[0]), long(Items[1])}) as Items)");
-
- // Assert 2
- var queryable = new[] { instance }.AsQueryable().Select(line);
- bool result = queryable.Any("Items != null");
- result.Should().BeTrue();
- }
-
- [Fact]
- public void JsonUtils_GenerateDynamicLinqStatement_JObject2()
- {
- // Assign
- var instance = new JObject
- {
- {"U", new JValue(new Uri("http://localhost:80/abc?a=5"))},
- {"N", new JValue((object?) null)},
- {"G", new JValue(Guid.NewGuid())},
- {"Flt", new JValue(10.0f)},
- {"Dbl", new JValue(Math.PI)},
- {"Check", new JValue(true)},
- {
- "Child", new JObject
- {
- {"ChildId", new JValue(4)},
- {"ChildDateTime", new JValue(new DateTime(2018, 2, 17))},
- {"TS", new JValue(TimeSpan.FromMilliseconds(999))}
- }
- },
- {"I", new JValue(9)},
- {"L", new JValue(long.MaxValue)},
- {"Name", new JValue("Test")}
- };
-
- // Act
- string line = JsonUtils.GenerateDynamicLinqStatement(instance);
-
- // Assert 1
- line.Should().Be("new (Uri(U) as U, null as N, Guid(G) as G, double(Flt) as Flt, double(Dbl) as Dbl, bool(Check) as Check, new (long(Child.ChildId) as ChildId, DateTime(Child.ChildDateTime) as ChildDateTime, TimeSpan(Child.TS) as TS) as Child, long(I) as I, long(L) as L, string(Name) as Name)");
-
- // Assert 2
- var queryable = new[] { instance }.AsQueryable().Select(line);
- bool result = queryable.Any("I > 1 && L > 1");
- result.Should().BeTrue();
- }
-
- [Fact]
- public void JsonUtils_GenerateDynamicLinqStatement_Throws()
- {
- // Assign
- var instance = new JObject
- {
- { "B", new JValue(new byte[] {48, 49}) }
- };
-
- // Act and Assert
- Check.ThatCode(() => JsonUtils.GenerateDynamicLinqStatement(instance)).Throws();
- }
-
- [Fact]
- public void JsonUtils_CreateTypeFromJObject()
- {
- // Assign
- var instance = new JObject
- {
- {"U", new JValue(new Uri("http://localhost:80/abc?a=5"))},
- {"N", new JValue((object?) null)},
- {"G", new JValue(Guid.NewGuid())},
- {"Flt", new JValue(10.0f)},
- {"Dbl", new JValue(Math.PI)},
- {"Check", new JValue(true)},
- {
- "Child", new JObject
- {
- {"ChildId", new JValue(4)},
- {"ChildDateTime", new JValue(new DateTime(2018, 2, 17))},
- {"ChildTimeSpan", new JValue(TimeSpan.FromMilliseconds(999))}
- }
- },
- {"I", new JValue(9)},
- {"L", new JValue(long.MaxValue)},
- {"S", new JValue("Test")},
- {"C", new JValue('c')}
- };
-
- // Act
- var type = JsonUtils.CreateTypeFromJObject(instance);
-
- // Assert
- var setProperties = type
- .GetProperties(BindingFlags.Public | BindingFlags.Instance)
- .Where(pi => pi.GetMethod != null).Select(pi => $"{pi.GetMethod}")
- .ToArray();
-
- setProperties.Should().HaveCount(11);
- setProperties.Should().BeEquivalentTo(new[]
- {
- "System.String get_U()",
- "System.Object get_N()",
- "System.Guid get_G()",
- "Single get_Flt()",
- "Single get_Dbl()",
- "Boolean get_Check()",
- "Child get_Child()",
- "Int64 get_I()",
- "Int64 get_L()",
- "System.String get_S()",
- "System.String get_C()"
- });
- }
}
\ No newline at end of file
diff --git a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj
index a41bed5bd..976849b38 100644
--- a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj
+++ b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj
@@ -34,10 +34,6 @@
$(DefineConstants);TRAILINGHEADERS
-
-
-
-
@@ -46,6 +42,7 @@
+