Skip to content

Commit

Permalink
WireMock.Net.MimeKitLite
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Feb 26, 2024
1 parent 398ea09 commit 97e5c41
Show file tree
Hide file tree
Showing 25 changed files with 220 additions and 306 deletions.
7 changes: 7 additions & 0 deletions WireMock.Net Solution.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 0 additions & 2 deletions src/WireMock.Net.Abstractions/IRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,11 @@ public interface IRequestMessage
/// </summary>
byte[]? BodyAsBytes { get; }

#if MIMEKIT
/// <summary>
/// The original body as MimeMessage.
/// Convenience getter for Handlebars and WireMockAssertions.
/// </summary>
object? BodyAsMimeMessage { get; }
#endif

/// <summary>
/// The detected body type. Convenience getter for Handlebars.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1'">
<DefineConstants>$(DefineConstants);MIMEKIT;PROTOBUF</DefineConstants>
<DefineConstants>$(DefineConstants);PROTOBUF</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions src/WireMock.Net.GraphQL/WireMock.Net.GraphQL.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>A GraphQL which can be used to match WireMock.Net Requests.</Description>
<Description>GraphQL support for WireMock.Net</Description>
<AssemblyTitle>WireMock.Net.Matchers.GraphQL</AssemblyTitle>
<Authors>Stef Heyenrath</Authors>
<TargetFrameworks>netstandard2.0;netstandard2.1;net5.0;net6.0;net8.0</TargetFrameworks>
Expand All @@ -10,7 +10,6 @@
<RootNamespace>WireMock</RootNamespace>
<ProjectGuid>{B6269AAC-170A-4346-8B9A-444DED3D9A45}</ProjectGuid>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!--<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>-->
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
#if MIMEKIT
using System;
using MimeKit;
using WireMock.Matchers;
using WireMock.Matchers.Helpers;
using WireMock.Models;
using WireMock.Util;

namespace WireMock.Matchers;

/// <summary>
/// MimePartMatcher
/// </summary>
public class MimePartMatcher : IMatcher
public class MimePartMatcher : IMimePartMatcher
{
private readonly Func<MimePart, MatchResult>[] _funcs;

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

/// <summary>
/// ContentType Matcher (image/png; name=image.png.)
/// </summary>
/// <inheritdoc />
public IStringMatcher? ContentTypeMatcher { get; }

/// <summary>
/// ContentDisposition Matcher (attachment; filename=image.png)
/// </summary>
/// <inheritdoc />
public IStringMatcher? ContentDispositionMatcher { get; }

/// <summary>
/// ContentTransferEncoding Matcher (base64)
/// </summary>
/// <inheritdoc />
public IStringMatcher? ContentTransferEncodingMatcher { get; }

/// <summary>
/// Content Matcher
/// </summary>
/// <inheritdoc />
public IMatcher? ContentMatcher { get; }

/// <inheritdoc />
Expand Down Expand Up @@ -116,5 +105,4 @@ private MatchResult MatchOnContent(MimePart mimePart)
{
return contentType?.ToString().Replace("Content-Type: ", string.Empty);
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MimeKit;
using Stef.Validation;
using WireMock.Util;

Expand All @@ -9,23 +10,21 @@ namespace WireMock.Matchers.Request;
/// <summary>
/// The request body MultiPart matcher.
/// </summary>
public class RequestMessageMultiPartMatcher : IRequestMatcher
public class RequestMessageMultiPartMatcher : IRequestMessageMultiPartMatcher
{
/// <summary>
/// The matchers.
/// </summary>
/// <inheritdoc />
public IMatcher[]? Matchers { get; }

/// <summary>
/// The <see cref="MatchOperator"/>
/// </summary>
/// <inheritdoc />
public MatchOperator MatchOperator { get; } = MatchOperator.Or;

/// <summary>
/// The <see cref="MatchBehaviour"/>
/// </summary>
public MatchBehaviour MatchBehaviour { get; }

private readonly MimeKitUtils _mimeKitUtils = new();

/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageMultiPartMatcher"/> class.
/// </summary>
Expand All @@ -51,9 +50,6 @@ public RequestMessageMultiPartMatcher(MatchBehaviour matchBehaviour, MatchOperat
/// <inheritdoc />
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;

Expand All @@ -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);
}
Expand All @@ -71,7 +67,7 @@ public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResu
{
var mimePartMatchers = Matchers.OfType<MimePartMatcher>().ToArray();

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

return requestMatchResult.AddScore(GetType(), score, exception);
#endif
}
}
6 changes: 6 additions & 0 deletions src/WireMock.Net.MimeKitLite/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// ReSharper disable InconsistentNaming
using WireMock.Matchers;
using WireMock.Matchers.Request;

namespace WireMock.RequestBuilders;

/// <summary>
/// IRequestBuilderExtensions extensions for MultiPart Mime using MimeKitLite.
/// </summary>
public static class IRequestBuilderExtensions
{
/// <summary>
/// WithMultiPart: IMatcher
/// </summary>
/// <param name="requestBuilder">The <see cref="IRequestBuilder"/>.</param>
/// <param name="matcher">The matcher.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
public static IRequestBuilder WithMultiPart(this IRequestBuilder requestBuilder, IMatcher matcher)
{
return requestBuilder.Add(new RequestMessageMultiPartMatcher(matcher));
}

/// <summary>
/// WithMultiPart: IMatcher[], MatchBehaviour and MatchOperator
/// </summary>
/// <param name="requestBuilder">The <see cref="IRequestBuilder"/>.</param>
/// <param name="matchers">The matchers.</param>
/// <param name="matchBehaviour">The <see cref="MatchBehaviour"/> to use.</param>
/// <param name="matchOperator">The <see cref="MatchOperator"/> to use.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
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));
}

/// <summary>
/// WithMultiPart: MatchBehaviour and IMatcher[]
/// </summary>
/// <param name="requestBuilder">The <see cref="IRequestBuilder"/>.</param>
/// <param name="matchBehaviour">The <see cref="MatchBehaviour"/> to use.</param>
/// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
public static IRequestBuilder WithMultiPart(this IRequestBuilder requestBuilder, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch, params IMatcher[] matchers)
{
return requestBuilder.Add(new RequestMessageMultiPartMatcher(matchBehaviour, MatchOperator.Or, matchers));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if MIMEKIT
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
Expand All @@ -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);

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

return result;
}
}
#endif
}
36 changes: 36 additions & 0 deletions src/WireMock.Net.MimeKitLite/WireMock.Net.MimeKitLite.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>MultiPart Mime support for WireMock.Net</Description>
<AssemblyTitle>WireMock.Net.Matchers.GraphQL</AssemblyTitle>
<Authors>Stef Heyenrath</Authors>
<TargetFrameworks>netstandard2.0;netstandard2.1;net462;net47;net48;net6.0;net8.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>wiremock;matchers;matcher;graphql</PackageTags>
<RootNamespace>WireMock</RootNamespace>
<ProjectGuid>{F8B4A93E-46EF-4237-88FE-15FDAB7635D4}</ProjectGuid>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<CodeAnalysisRuleSet>../WireMock.Net/WireMock.Net.ruleset</CodeAnalysisRuleSet>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../WireMock.Net/WireMock.Net.snk</AssemblyOriginatorKeyFile>
<!--<DelaySign>true</DelaySign>-->
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="MimeKitLite" Version="4.1.0.1" />

<ProjectReference Include="..\WireMock.Net\WireMock.Net.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/WireMock.Net/Matchers/IGraphQLMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace WireMock.Matchers;
/// <summary>
/// GraphQLMatcher
/// </summary>
/// <inheritdoc cref="IGraphQLMatcher"/>
/// <inheritdoc cref="IStringMatcher"/>
public interface IGraphQLMatcher : IStringMatcher
{
/// <summary>
Expand Down
28 changes: 28 additions & 0 deletions src/WireMock.Net/Matchers/IMimePartMatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace WireMock.Matchers;

/// <summary>
/// MimePartMatcher
/// </summary>
/// <inheritdoc cref="IMatcher"/>
public interface IMimePartMatcher : IMatcher
{
/// <summary>
/// ContentType Matcher (image/png; name=image.png.)
/// </summary>
IStringMatcher? ContentTypeMatcher { get; }

/// <summary>
/// ContentDisposition Matcher (attachment; filename=image.png)
/// </summary>
IStringMatcher? ContentDispositionMatcher { get; }

/// <summary>
/// ContentTransferEncoding Matcher (base64)
/// </summary>
IStringMatcher? ContentTransferEncodingMatcher { get; }

/// <summary>
/// Content Matcher
/// </summary>
IMatcher? ContentMatcher { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace WireMock.Matchers.Request;

/// <summary>
/// The request body MultiPart matcher.
/// </summary>
public interface IRequestMessageMultiPartMatcher : IRequestMatcher
{
/// <summary>
/// The matchers.
/// </summary>
IMatcher[]? Matchers { get; }

/// <summary>
/// The <see cref="MatchOperator"/>
/// </summary>
MatchOperator MatchOperator { get; }
}
1 change: 1 addition & 0 deletions src/WireMock.Net/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down
Loading

0 comments on commit 97e5c41

Please sign in to comment.