Skip to content

Commit 97e5c41

Browse files
committed
WireMock.Net.MimeKitLite
1 parent 398ea09 commit 97e5c41

25 files changed

+220
-306
lines changed

WireMock.Net Solution.sln

+7
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.Console.GrpcCl
116116
EndProject
117117
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.GraphQL", "src\WireMock.Net.GraphQL\WireMock.Net.GraphQL.csproj", "{B6269AAC-170A-4346-8B9A-444DED3D9A45}"
118118
EndProject
119+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.MimeKitLite", "src\WireMock.Net.MimeKitLite\WireMock.Net.MimeKitLite.csproj", "{F8B4A93E-46EF-4237-88FE-15FDAB7635C2}"
120+
EndProject
119121
Global
120122
GlobalSection(SolutionConfigurationPlatforms) = preSolution
121123
Debug|Any CPU = Debug|Any CPU
@@ -274,6 +276,10 @@ Global
274276
{B6269AAC-170A-4346-8B9A-444DED3D9A45}.Debug|Any CPU.Build.0 = Debug|Any CPU
275277
{B6269AAC-170A-4346-8B9A-444DED3D9A45}.Release|Any CPU.ActiveCfg = Release|Any CPU
276278
{B6269AAC-170A-4346-8B9A-444DED3D9A45}.Release|Any CPU.Build.0 = Release|Any CPU
279+
{F8B4A93E-46EF-4237-88FE-15FDAB7635C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
280+
{F8B4A93E-46EF-4237-88FE-15FDAB7635C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
281+
{F8B4A93E-46EF-4237-88FE-15FDAB7635C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
282+
{F8B4A93E-46EF-4237-88FE-15FDAB7635C2}.Release|Any CPU.Build.0 = Release|Any CPU
277283
EndGlobalSection
278284
GlobalSection(SolutionProperties) = preSolution
279285
HideSolutionNode = FALSE
@@ -319,6 +325,7 @@ Global
319325
{7FC0B409-2682-40EE-B3B9-3930D6769D01} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
320326
{B1580A38-84E7-44BE-8FE7-3EE5031D74A1} = {985E0ADB-D4B4-473A-AA40-567E279B7946}
321327
{B6269AAC-170A-4346-8B9A-444DED3D9A45} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2}
328+
{F8B4A93E-46EF-4237-88FE-15FDAB7635C2} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2}
322329
EndGlobalSection
323330
GlobalSection(ExtensibilityGlobals) = postSolution
324331
SolutionGuid = {DC539027-9852-430C-B19F-FD035D018458}

src/WireMock.Net.Abstractions/IRequestMessage.cs

-2
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,11 @@ public interface IRequestMessage
116116
/// </summary>
117117
byte[]? BodyAsBytes { get; }
118118

119-
#if MIMEKIT
120119
/// <summary>
121120
/// The original body as MimeMessage.
122121
/// Convenience getter for Handlebars and WireMockAssertions.
123122
/// </summary>
124123
object? BodyAsMimeMessage { get; }
125-
#endif
126124

127125
/// <summary>
128126
/// The detected body type. Convenience getter for Handlebars.

src/WireMock.Net.Abstractions/WireMock.Net.Abstractions.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</PropertyGroup>
3232

3333
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1'">
34-
<DefineConstants>$(DefineConstants);MIMEKIT;PROTOBUF</DefineConstants>
34+
<DefineConstants>$(DefineConstants);PROTOBUF</DefineConstants>
3535
</PropertyGroup>
3636

3737
<ItemGroup>

src/WireMock.Net.GraphQL/WireMock.Net.GraphQL.csproj

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<Description>A GraphQL which can be used to match WireMock.Net Requests.</Description>
4+
<Description>GraphQL support for WireMock.Net</Description>
55
<AssemblyTitle>WireMock.Net.Matchers.GraphQL</AssemblyTitle>
66
<Authors>Stef Heyenrath</Authors>
77
<TargetFrameworks>netstandard2.0;netstandard2.1;net5.0;net6.0;net8.0</TargetFrameworks>
@@ -10,7 +10,6 @@
1010
<RootNamespace>WireMock</RootNamespace>
1111
<ProjectGuid>{B6269AAC-170A-4346-8B9A-444DED3D9A45}</ProjectGuid>
1212
<PublishRepositoryUrl>true</PublishRepositoryUrl>
13-
<!--<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>-->
1413
<EmbedUntrackedSources>true</EmbedUntrackedSources>
1514
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
1615
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

src/WireMock.Net/Matchers/MimePartMatcher.cs src/WireMock.Net.MimeKitLite/Matchers/MimePartMatcher.cs

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
1-
#if MIMEKIT
21
using System;
32
using MimeKit;
4-
using WireMock.Matchers;
53
using WireMock.Matchers.Helpers;
6-
using WireMock.Models;
74
using WireMock.Util;
85

96
namespace WireMock.Matchers;
107

118
/// <summary>
129
/// MimePartMatcher
1310
/// </summary>
14-
public class MimePartMatcher : IMatcher
11+
public class MimePartMatcher : IMimePartMatcher
1512
{
1613
private readonly Func<MimePart, MatchResult>[] _funcs;
1714

1815
/// <inheritdoc />
1916
public string Name => nameof(MimePartMatcher);
2017

21-
/// <summary>
22-
/// ContentType Matcher (image/png; name=image.png.)
23-
/// </summary>
18+
/// <inheritdoc />
2419
public IStringMatcher? ContentTypeMatcher { get; }
2520

26-
/// <summary>
27-
/// ContentDisposition Matcher (attachment; filename=image.png)
28-
/// </summary>
21+
/// <inheritdoc />
2922
public IStringMatcher? ContentDispositionMatcher { get; }
3023

31-
/// <summary>
32-
/// ContentTransferEncoding Matcher (base64)
33-
/// </summary>
24+
/// <inheritdoc />
3425
public IStringMatcher? ContentTransferEncodingMatcher { get; }
3526

36-
/// <summary>
37-
/// Content Matcher
38-
/// </summary>
27+
/// <inheritdoc />
3928
public IMatcher? ContentMatcher { get; }
4029

4130
/// <inheritdoc />
@@ -116,5 +105,4 @@ private MatchResult MatchOnContent(MimePart mimePart)
116105
{
117106
return contentType?.ToString().Replace("Content-Type: ", string.Empty);
118107
}
119-
}
120-
#endif
108+
}

src/WireMock.Net/Matchers/Request/RequestMessageMultiPartMatcher.cs src/WireMock.Net.MimeKitLite/Matchers/Request/RequestMessageMultiPartMatcher.cs

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using MimeKit;
45
using Stef.Validation;
56
using WireMock.Util;
67

@@ -9,23 +10,21 @@ namespace WireMock.Matchers.Request;
910
/// <summary>
1011
/// The request body MultiPart matcher.
1112
/// </summary>
12-
public class RequestMessageMultiPartMatcher : IRequestMatcher
13+
public class RequestMessageMultiPartMatcher : IRequestMessageMultiPartMatcher
1314
{
14-
/// <summary>
15-
/// The matchers.
16-
/// </summary>
15+
/// <inheritdoc />
1716
public IMatcher[]? Matchers { get; }
1817

19-
/// <summary>
20-
/// The <see cref="MatchOperator"/>
21-
/// </summary>
18+
/// <inheritdoc />
2219
public MatchOperator MatchOperator { get; } = MatchOperator.Or;
2320

2421
/// <summary>
2522
/// The <see cref="MatchBehaviour"/>
2623
/// </summary>
2724
public MatchBehaviour MatchBehaviour { get; }
2825

26+
private readonly MimeKitUtils _mimeKitUtils = new();
27+
2928
/// <summary>
3029
/// Initializes a new instance of the <see cref="RequestMessageMultiPartMatcher"/> class.
3130
/// </summary>
@@ -51,9 +50,6 @@ public RequestMessageMultiPartMatcher(MatchBehaviour matchBehaviour, MatchOperat
5150
/// <inheritdoc />
5251
public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult)
5352
{
54-
#if !MIMEKIT
55-
throw new System.NotSupportedException("The MultiPartMatcher can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
56-
#else
5753
var score = MatchScores.Mismatch;
5854
Exception? exception = null;
5955

@@ -62,7 +58,7 @@ public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResu
6258
return requestMatchResult.AddScore(GetType(), score, null);
6359
}
6460

65-
if (!MimeKitUtils.TryGetMimeMessage(requestMessage, out var message))
61+
if (!_mimeKitUtils.TryGetMimeMessage(requestMessage, out MimeMessage? message))
6662
{
6763
return requestMatchResult.AddScore(GetType(), score, null);
6864
}
@@ -71,7 +67,7 @@ public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResu
7167
{
7268
var mimePartMatchers = Matchers.OfType<MimePartMatcher>().ToArray();
7369

74-
foreach (var mimePart in message.BodyParts.OfType<MimeKit.MimePart>())
70+
foreach (var mimePart in message.BodyParts.OfType<MimePart>())
7571
{
7672
var matchesForMimePart = new List<MatchResult> { default };
7773
matchesForMimePart.AddRange(mimePartMatchers.Select(matcher => matcher.IsMatch(mimePart)));
@@ -98,6 +94,5 @@ public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResu
9894
}
9995

10096
return requestMatchResult.AddScore(GetType(), score, exception);
101-
#endif
10297
}
10398
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("WireMock.Net.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
4+
5+
// Needed for Moq in the UnitTest project
6+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// ReSharper disable InconsistentNaming
2+
using WireMock.Matchers;
3+
using WireMock.Matchers.Request;
4+
5+
namespace WireMock.RequestBuilders;
6+
7+
/// <summary>
8+
/// IRequestBuilderExtensions extensions for MultiPart Mime using MimeKitLite.
9+
/// </summary>
10+
public static class IRequestBuilderExtensions
11+
{
12+
/// <summary>
13+
/// WithMultiPart: IMatcher
14+
/// </summary>
15+
/// <param name="requestBuilder">The <see cref="IRequestBuilder"/>.</param>
16+
/// <param name="matcher">The matcher.</param>
17+
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
18+
public static IRequestBuilder WithMultiPart(this IRequestBuilder requestBuilder, IMatcher matcher)
19+
{
20+
return requestBuilder.Add(new RequestMessageMultiPartMatcher(matcher));
21+
}
22+
23+
/// <summary>
24+
/// WithMultiPart: IMatcher[], MatchBehaviour and MatchOperator
25+
/// </summary>
26+
/// <param name="requestBuilder">The <see cref="IRequestBuilder"/>.</param>
27+
/// <param name="matchers">The matchers.</param>
28+
/// <param name="matchBehaviour">The <see cref="MatchBehaviour"/> to use.</param>
29+
/// <param name="matchOperator">The <see cref="MatchOperator"/> to use.</param>
30+
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
31+
public static IRequestBuilder WithMultiPart(this IRequestBuilder requestBuilder, IMatcher[] matchers, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch, MatchOperator matchOperator = MatchOperator.Or)
32+
{
33+
return requestBuilder.Add(new RequestMessageMultiPartMatcher(matchBehaviour, matchOperator, matchers));
34+
}
35+
36+
/// <summary>
37+
/// WithMultiPart: MatchBehaviour and IMatcher[]
38+
/// </summary>
39+
/// <param name="requestBuilder">The <see cref="IRequestBuilder"/>.</param>
40+
/// <param name="matchBehaviour">The <see cref="MatchBehaviour"/> to use.</param>
41+
/// <param name="matchers">The matchers.</param>
42+
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
43+
public static IRequestBuilder WithMultiPart(this IRequestBuilder requestBuilder, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch, params IMatcher[] matchers)
44+
{
45+
return requestBuilder.Add(new RequestMessageMultiPartMatcher(matchBehaviour, MatchOperator.Or, matchers));
46+
}
47+
}

src/WireMock.Net/Util/MimeKitUtils.cs src/WireMock.Net.MimeKitLite/Util/MimeKitUtils.cs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if MIMEKIT
21
using System;
32
using System.Diagnostics.CodeAnalysis;
43
using System.IO;
@@ -11,9 +10,16 @@
1110

1211
namespace WireMock.Util;
1312

14-
internal static class MimeKitUtils
13+
internal class MimeKitUtils : IMimeKitUtils
1514
{
16-
public static bool TryGetMimeMessage(IRequestMessage requestMessage, [NotNullWhen(true)] out MimeMessage? mimeMessage)
15+
public bool TryGetMimeMessage(IRequestMessage requestMessage, [NotNullWhen(true)] out object? mimeMessage)
16+
{
17+
var result = TryGetMimeMessage(requestMessage, out MimeMessage? value);
18+
mimeMessage = value;
19+
return result;
20+
}
21+
22+
public bool TryGetMimeMessage(IRequestMessage requestMessage, [NotNullWhen(true)] out MimeMessage? mimeMessage)
1723
{
1824
Guard.NotNull(requestMessage);
1925

@@ -59,5 +65,4 @@ private static byte[] FixBytes(byte[] bytes, WireMockList<string> contentType)
5965

6066
return result;
6167
}
62-
}
63-
#endif
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Description>MultiPart Mime support for WireMock.Net</Description>
5+
<AssemblyTitle>WireMock.Net.Matchers.GraphQL</AssemblyTitle>
6+
<Authors>Stef Heyenrath</Authors>
7+
<TargetFrameworks>netstandard2.0;netstandard2.1;net462;net47;net48;net6.0;net8.0</TargetFrameworks>
8+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
9+
<PackageTags>wiremock;matchers;matcher;graphql</PackageTags>
10+
<RootNamespace>WireMock</RootNamespace>
11+
<ProjectGuid>{F8B4A93E-46EF-4237-88FE-15FDAB7635D4}</ProjectGuid>
12+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
13+
<EmbedUntrackedSources>true</EmbedUntrackedSources>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
16+
<CodeAnalysisRuleSet>../WireMock.Net/WireMock.Net.ruleset</CodeAnalysisRuleSet>
17+
<SignAssembly>true</SignAssembly>
18+
<AssemblyOriginatorKeyFile>../WireMock.Net/WireMock.Net.snk</AssemblyOriginatorKeyFile>
19+
<!--<DelaySign>true</DelaySign>-->
20+
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
21+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
22+
</PropertyGroup>
23+
24+
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
25+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
26+
</PropertyGroup>
27+
28+
<ItemGroup>
29+
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="All" />
30+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
31+
<PackageReference Include="MimeKitLite" Version="4.1.0.1" />
32+
33+
<ProjectReference Include="..\WireMock.Net\WireMock.Net.csproj" />
34+
</ItemGroup>
35+
36+
</Project>

src/WireMock.Net/Matchers/IGraphQLMatcher.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace WireMock.Matchers;
66
/// <summary>
77
/// GraphQLMatcher
88
/// </summary>
9-
/// <inheritdoc cref="IGraphQLMatcher"/>
9+
/// <inheritdoc cref="IStringMatcher"/>
1010
public interface IGraphQLMatcher : IStringMatcher
1111
{
1212
/// <summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace WireMock.Matchers;
2+
3+
/// <summary>
4+
/// MimePartMatcher
5+
/// </summary>
6+
/// <inheritdoc cref="IMatcher"/>
7+
public interface IMimePartMatcher : IMatcher
8+
{
9+
/// <summary>
10+
/// ContentType Matcher (image/png; name=image.png.)
11+
/// </summary>
12+
IStringMatcher? ContentTypeMatcher { get; }
13+
14+
/// <summary>
15+
/// ContentDisposition Matcher (attachment; filename=image.png)
16+
/// </summary>
17+
IStringMatcher? ContentDispositionMatcher { get; }
18+
19+
/// <summary>
20+
/// ContentTransferEncoding Matcher (base64)
21+
/// </summary>
22+
IStringMatcher? ContentTransferEncodingMatcher { get; }
23+
24+
/// <summary>
25+
/// Content Matcher
26+
/// </summary>
27+
IMatcher? ContentMatcher { get; }
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace WireMock.Matchers.Request;
2+
3+
/// <summary>
4+
/// The request body MultiPart matcher.
5+
/// </summary>
6+
public interface IRequestMessageMultiPartMatcher : IRequestMatcher
7+
{
8+
/// <summary>
9+
/// The matchers.
10+
/// </summary>
11+
IMatcher[]? Matchers { get; }
12+
13+
/// <summary>
14+
/// The <see cref="MatchOperator"/>
15+
/// </summary>
16+
MatchOperator MatchOperator { get; }
17+
}

src/WireMock.Net/Properties/AssemblyInfo.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Runtime.CompilerServices;
22

3+
[assembly: InternalsVisibleTo("WireMock.Net.MimeKitLite, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
34
[assembly: InternalsVisibleTo("WireMock.Net.GraphQL, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
45
[assembly: InternalsVisibleTo("WireMock.Net.Matchers.CSharpCode, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]
56
[assembly: InternalsVisibleTo("WireMock.Net.StandAlone, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")]

0 commit comments

Comments
 (0)