From 92923a12ae70aa5dc7bc228e49499d2ed7e911d8 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 12 Jan 2021 16:56:03 +0100 Subject: [PATCH] Move CSharpCodeMatcher to a new project (#548) * matcher * wip * fix * 1.4.0 * . * x * ? * netstandard2.1 * {} * test * Fix: Assembly with same name is already loaded * _format file * AssemblyFile = $"WireMock.CodeHelper.Class{Guid.NewGuid()}" * AssemblyFile = $"WireMock.CodeHelper.Class{Guid.NewGuid().ToString().Replace("-", "")}" * GC * x * remove load ex * ret * readme * no GC * GetImplementationTypeByInterface * `` * PluginLoader * type --- Directory.Build.props | 2 +- README.md | 3 +- WireMock.Net Solution.sln | 7 +++ .../Matchers/CSharpCodeMatcher.cs | 45 +++++++++----- .../Properties/AssemblyInfo.cs | 6 ++ .../WireMock.Net.Matchers.CSharpCode.csproj | 60 +++++++++++++++++++ .../Matchers/ICSharpCodeMatcher.cs | 11 ++++ src/WireMock.Net/Plugin/PluginLoader.cs | 57 ++++++++++++++++++ src/WireMock.Net/Properties/AssemblyInfo.cs | 13 ++-- .../Serialization/MatcherMapper.cs | 3 +- src/WireMock.Net/WireMock.Net.csproj | 40 ++----------- .../Plugin/PluginLoaderTests.cs | 35 +++++++++++ .../Serialization/MatcherModelMapperTests.cs | 44 ++++++++++++++ .../WireMock.Net.Tests.csproj | 3 +- .../WireMockAdminApiTests.cs | 6 +- .../WireMock.Net.Tests/WireMockServerTests.cs | 2 +- 16 files changed, 274 insertions(+), 63 deletions(-) rename src/{WireMock.Net => WireMock.Net.Matchers.CSharpCode}/Matchers/CSharpCodeMatcher.cs (83%) create mode 100644 src/WireMock.Net.Matchers.CSharpCode/Properties/AssemblyInfo.cs create mode 100644 src/WireMock.Net.Matchers.CSharpCode/WireMock.Net.Matchers.CSharpCode.csproj create mode 100644 src/WireMock.Net/Matchers/ICSharpCodeMatcher.cs create mode 100644 src/WireMock.Net/Plugin/PluginLoader.cs create mode 100644 test/WireMock.Net.Tests/Plugin/PluginLoaderTests.cs diff --git a/Directory.Build.props b/Directory.Build.props index 7eb54209..1275b6d3 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -4,7 +4,7 @@ - 1.3.10 + 1.4.0 See CHANGELOG.md https://raw.githubusercontent.com/WireMock-Net/WireMock.Net/master/WireMock.Net-Logo.png https://github.com/WireMock-Net/WireMock.Net diff --git a/README.md b/README.md index 1fd9397c..fc85cdd7 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ For more info, see also this WIKI page: [What is WireMock.Net](https://github.co * Per-request conditional proxying * Stateful behaviour simulation * Response templating / transformation using Handlebars and extensions -* Can be used locally or in CI/CD scenarios. +* Can be used locally or in CI/CD scenarios ## Info | | | @@ -35,6 +35,7 @@ For more info, see also this WIKI page: [What is WireMock.Net](https://github.co |   **WireMock.Net.StandAlone** | [![NuGet Badge WireMock.Net](https://buildstats.info/nuget/WireMock.Net.StandAlone)](https://www.nuget.org/packages/WireMock.Net.StandAlone) | [![MyGet Badge WireMock.Net.StandAlone](https://buildstats.info/myget/wiremock-net/WireMock.Net.StandAlone?includePreReleases=true)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.StandAlone) |   **WireMock.Net.FluentAssertions** | [![NuGet Badge WireMock.Net.FluentAssertions](https://buildstats.info/nuget/WireMock.Net.FluentAssertions)](https://www.nuget.org/packages/WireMock.Net.FluentAssertions) | [![MyGet Badge WireMock.Net.FluentAssertions](https://buildstats.info/myget/wiremock-net/WireMock.Net.FluentAssertions?includePreReleases=true)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.FluentAssertions) |   **WireMock.Net.RestClient** | [![NuGet Badge WireMock.Net.RestClient](https://buildstats.info/nuget/WireMock.Net.RestClient)](https://www.nuget.org/packages/WireMock.Net.RestClient) | [![MyGet Badge WireMock.Net.RestClient](https://buildstats.info/myget/wiremock-net/WireMock.Net.RestClient?includePreReleases=true)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.RestClient) +|   **WireMock.Net.Matchers.CSharpCode** | [![NuGet Badge WireMock.Net.Matchers.CSharpCode](https://buildstats.info/nuget/WireMock.Net.Matchers.CSharpCode)](https://www.nuget.org/packages/WireMock.Net.Matchers.CSharpCode) | [![MyGet Badge WireMock.Net.Matchers.CSharpCode](https://buildstats.info/myget/wiremock-net/WireMock.Net.Matchers.CSharpCode?includePreReleases=true)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.Matchers.CSharpCode) ## Development For the supported frameworks and build information, see [this](https://github.com/WireMock-Net/WireMock.Net/wiki/Development-Information) page. diff --git a/WireMock.Net Solution.sln b/WireMock.Net Solution.sln index 9b0243e0..0d20008c 100644 --- a/WireMock.Net Solution.sln +++ b/WireMock.Net Solution.sln @@ -78,6 +78,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-WireMock", "src\dotn EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.Console.NET5", "examples\WireMock.Net.Console.NET5\WireMock.Net.Console.NET5.csproj", "{3F8CF0AE-5F24-4A54-89E7-A3EE829DB5F8}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.Matchers.CSharpCode", "src\WireMock.Net.Matchers.CSharpCode\WireMock.Net.Matchers.CSharpCode.csproj", "{6437CE56-8AB9-4D6D-90F1-70CC5BBE1572}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -192,6 +194,10 @@ Global {3F8CF0AE-5F24-4A54-89E7-A3EE829DB5F8}.Debug|Any CPU.Build.0 = Debug|Any CPU {3F8CF0AE-5F24-4A54-89E7-A3EE829DB5F8}.Release|Any CPU.ActiveCfg = Release|Any CPU {3F8CF0AE-5F24-4A54-89E7-A3EE829DB5F8}.Release|Any CPU.Build.0 = Release|Any CPU + {6437CE56-8AB9-4D6D-90F1-70CC5BBE1572}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6437CE56-8AB9-4D6D-90F1-70CC5BBE1572}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6437CE56-8AB9-4D6D-90F1-70CC5BBE1572}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6437CE56-8AB9-4D6D-90F1-70CC5BBE1572}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -224,6 +230,7 @@ Global {925E421A-1B3F-4202-B48F-734743573A4B} = {985E0ADB-D4B4-473A-AA40-567E279B7946} {40BF24B5-12E6-4610-9489-138798632E28} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2} {3F8CF0AE-5F24-4A54-89E7-A3EE829DB5F8} = {985E0ADB-D4B4-473A-AA40-567E279B7946} + {6437CE56-8AB9-4D6D-90F1-70CC5BBE1572} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {DC539027-9852-430C-B19F-FD035D018458} diff --git a/src/WireMock.Net/Matchers/CSharpCodeMatcher.cs b/src/WireMock.Net.Matchers.CSharpCode/Matchers/CSharpCodeMatcher.cs similarity index 83% rename from src/WireMock.Net/Matchers/CSharpCodeMatcher.cs rename to src/WireMock.Net.Matchers.CSharpCode/Matchers/CSharpCodeMatcher.cs index a0afd2e9..70926936 100644 --- a/src/WireMock.Net/Matchers/CSharpCodeMatcher.cs +++ b/src/WireMock.Net.Matchers.CSharpCode/Matchers/CSharpCodeMatcher.cs @@ -1,5 +1,7 @@ using System; using System.Linq; +using System.Reflection; +using System.Text; using JetBrains.Annotations; using Newtonsoft.Json.Linq; using WireMock.Exceptions; @@ -10,14 +12,12 @@ namespace WireMock.Matchers /// /// CSharpCode / CS-Script Matcher /// - /// - /// - [Obsolete("This class will be moved to a separate NuGet package 'WireMock.Net.Matchers.CSharpCode'")] - internal class CSharpCodeMatcher : IObjectMatcher, IStringMatcher + /// + internal class CSharpCodeMatcher : ICSharpCodeMatcher { - private const string TemplateForIsMatchWithString = "{0} public class CodeHelper {{ public bool IsMatch(string it) {{ {1} }} }}"; + private const string TemplateForIsMatchWithString = "public class CodeHelper {{ public bool IsMatch(string it) {{ {0} }} }}"; - private const string TemplateForIsMatchWithDynamic = "{0} public class CodeHelper {{ public bool IsMatch(dynamic it) {{ {1} }} }}"; + private const string TemplateForIsMatchWithDynamic = "public class CodeHelper {{ public bool IsMatch(dynamic it) {{ {0} }} }}"; private readonly string[] _usings = { @@ -153,20 +153,28 @@ namespace WireMock.Matchers } #elif (NETSTANDARD2_0 || NETSTANDARD2_1 || NETCOREAPP3_1 || NET5_0) - dynamic script; + Assembly assembly; try + { + assembly = CSScriptLib.CSScript.Evaluator.CompileCode(source); + } + catch (Exception ex) { - var assembly = CSScriptLib.CSScript.Evaluator.CompileCode(source); + throw new WireMockException($"CSharpCodeMatcher: Unable to compile code `{source}` for WireMock.CodeHelper", ex); + } + dynamic script; + try + { #if NETSTANDARD2_0 script = csscript.GenericExtensions.CreateObject(assembly, "*"); #else script = CSScriptLib.ReflectionExtensions.CreateObject(assembly, "*"); -#endif +#endif } catch (Exception ex) - { - throw new WireMockException("CSharpCodeMatcher: Unable to compile code for WireMock.CodeHelper", ex); + { + throw new WireMockException("CSharpCodeMatcher: Unable to create object from assembly", ex); } try @@ -176,9 +184,9 @@ namespace WireMock.Matchers catch (Exception ex) { throw new WireMockException("CSharpCodeMatcher: Problem calling method 'IsMatch' in WireMock.CodeHelper", ex); - } + } #else - throw new NotSupportedException("The 'CSharpCodeMatcher' cannot be used in netstandard 1.3"); + throw new NotSupportedException("The 'CSharpCodeMatcher' cannot be used in netstandard 1.3"); #endif try { @@ -193,7 +201,16 @@ namespace WireMock.Matchers private string GetSourceForIsMatchWithString(string pattern, bool isMatchWithString) { string template = isMatchWithString ? TemplateForIsMatchWithString : TemplateForIsMatchWithDynamic; - return string.Format(template, string.Join(Environment.NewLine, _usings.Select(u => $"using {u};")), pattern); + + var stringBuilder = new StringBuilder(); + foreach (string @using in _usings) + { + stringBuilder.AppendLine($"using {@using};"); + } + stringBuilder.AppendLine(); + stringBuilder.AppendFormat(template, pattern); + + return stringBuilder.ToString(); } /// diff --git a/src/WireMock.Net.Matchers.CSharpCode/Properties/AssemblyInfo.cs b/src/WireMock.Net.Matchers.CSharpCode/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..4f81246f --- /dev/null +++ b/src/WireMock.Net.Matchers.CSharpCode/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.Matchers.CSharpCode/WireMock.Net.Matchers.CSharpCode.csproj b/src/WireMock.Net.Matchers.CSharpCode/WireMock.Net.Matchers.CSharpCode.csproj new file mode 100644 index 00000000..49e351cd --- /dev/null +++ b/src/WireMock.Net.Matchers.CSharpCode/WireMock.Net.Matchers.CSharpCode.csproj @@ -0,0 +1,60 @@ + + + + A CSharpCodeMatcher which can be used to match WireMock.Net Requests using C# code. + WireMock.Net.Matchers.CSharpCode + Stef Heyenrath + net451;net452;net46;net461;netstandard1.3;netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0 + true + wiremock;matchers;matcher;csharp;csharpcode + WireMock + {B6269AAC-170A-4346-8B9A-444DED3D9A44} + true + $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb + 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/ICSharpCodeMatcher.cs b/src/WireMock.Net/Matchers/ICSharpCodeMatcher.cs new file mode 100644 index 00000000..f75e3326 --- /dev/null +++ b/src/WireMock.Net/Matchers/ICSharpCodeMatcher.cs @@ -0,0 +1,11 @@ +namespace WireMock.Matchers +{ + /// + /// CSharpCode / CS-Script Matcher + /// + /// + /// + public interface ICSharpCodeMatcher : IObjectMatcher, IStringMatcher + { + } +} \ No newline at end of file diff --git a/src/WireMock.Net/Plugin/PluginLoader.cs b/src/WireMock.Net/Plugin/PluginLoader.cs new file mode 100644 index 00000000..7479049f --- /dev/null +++ b/src/WireMock.Net/Plugin/PluginLoader.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Concurrent; +using System.IO; +using System.Linq; +using System.Reflection; + +namespace WireMock.Plugin +{ + internal static class PluginLoader + { + private static readonly ConcurrentDictionary Assemblies = new ConcurrentDictionary(); + + public static T Load(params object[] args) where T : class + { + var foundType = Assemblies.GetOrAdd(typeof(T), (type) => + { + var files = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.dll"); + + Type pluginType = null; + foreach (var file in files) + { + try + { + var assembly = Assembly.Load(new AssemblyName + { + Name = Path.GetFileNameWithoutExtension(file) + }); + + pluginType = GetImplementationTypeByInterface(assembly); + if (pluginType != null) + { + break; + } + } + catch + { + // no-op: just try next .dll + } + } + + if (pluginType != null) + { + return pluginType; + } + + throw new DllNotFoundException($"No dll found which implements type '{type}'"); + }); + + return (T)Activator.CreateInstance(foundType, args); + } + + private static Type GetImplementationTypeByInterface(Assembly assembly) + { + return assembly.GetTypes().FirstOrDefault(t => typeof(T).IsAssignableFrom(t) && !t.GetTypeInfo().IsInterface); + } + } +} \ No newline at end of file diff --git a/src/WireMock.Net/Properties/AssemblyInfo.cs b/src/WireMock.Net/Properties/AssemblyInfo.cs index f211ac62..4d736963 100644 --- a/src/WireMock.Net/Properties/AssemblyInfo.cs +++ b/src/WireMock.Net/Properties/AssemblyInfo.cs @@ -1,7 +1,8 @@ -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("WireMock.Net.StandAlone, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] -[assembly: InternalsVisibleTo("WireMock.Net.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] - -// Needed for Moq in the UnitTest project +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("WireMock.Net.Matchers.CSharpCode, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] +[assembly: InternalsVisibleTo("WireMock.Net.StandAlone, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] +[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/Serialization/MatcherMapper.cs b/src/WireMock.Net/Serialization/MatcherMapper.cs index 3021401c..4d369779 100644 --- a/src/WireMock.Net/Serialization/MatcherMapper.cs +++ b/src/WireMock.Net/Serialization/MatcherMapper.cs @@ -5,6 +5,7 @@ using JetBrains.Annotations; using SimMetrics.Net; using WireMock.Admin.Mappings; using WireMock.Matchers; +using WireMock.Plugin; using WireMock.Settings; using WireMock.Validation; @@ -46,7 +47,7 @@ namespace WireMock.Serialization case "CSharpCodeMatcher": if (_settings.AllowCSharpCodeMatcher == true) { - return new CSharpCodeMatcher(matchBehaviour, stringPatterns); + return PluginLoader.Load(matchBehaviour, stringPatterns); } throw new NotSupportedException("It's not allowed to use the 'CSharpCodeMatcher' because IWireMockServerSettings.AllowCSharpCodeMatcher is not set to 'true'."); diff --git a/src/WireMock.Net/WireMock.Net.csproj b/src/WireMock.Net/WireMock.Net.csproj index d1a4a1ef..98d03397 100644 --- a/src/WireMock.Net/WireMock.Net.csproj +++ b/src/WireMock.Net/WireMock.Net.csproj @@ -27,7 +27,7 @@ - + @@ -72,22 +72,12 @@ - + - - - - - - - - - - @@ -97,7 +87,6 @@ - @@ -105,8 +94,6 @@ - - @@ -117,34 +104,15 @@ - + - - - - - - - - - - - - + - - - - - - - - diff --git a/test/WireMock.Net.Tests/Plugin/PluginLoaderTests.cs b/test/WireMock.Net.Tests/Plugin/PluginLoaderTests.cs new file mode 100644 index 00000000..fe0f1262 --- /dev/null +++ b/test/WireMock.Net.Tests/Plugin/PluginLoaderTests.cs @@ -0,0 +1,35 @@ +using System; +using FluentAssertions; +using WireMock.Matchers; +using WireMock.Plugin; +using Xunit; + +namespace WireMock.Net.Tests.Plugin +{ + public class PluginLoaderTests + { + public interface IDummy + { + } + + [Fact] + public void Load_Valid() + { + // Act + var result = PluginLoader.Load(MatchBehaviour.AcceptOnMatch, "x"); + + // Assert + result.Should().NotBeNull(); + } + + [Fact] + public void Load_Invalid_ThrowsException() + { + // Act + Action a = () => PluginLoader.Load(); + + // Assert + a.Should().Throw(); + } + } +} \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Serialization/MatcherModelMapperTests.cs b/test/WireMock.Net.Tests/Serialization/MatcherModelMapperTests.cs index 9cecd32e..0a9c8393 100644 --- a/test/WireMock.Net.Tests/Serialization/MatcherModelMapperTests.cs +++ b/test/WireMock.Net.Tests/Serialization/MatcherModelMapperTests.cs @@ -20,6 +20,50 @@ namespace WireMock.Net.Tests.Serialization _sut = new MatcherMapper(_settings); } + [Fact] + public void MatcherModelMapper_Map_CSharpCodeMatcher() + { + // Assign + var model = new MatcherModel + { + Name = "CSharpCodeMatcher", + Patterns = new[] { "return it == \"x\";" } + }; + var sut = new MatcherMapper(new WireMockServerSettings { AllowCSharpCodeMatcher = true }); + + // Act 1 + var matcher1 = (ICSharpCodeMatcher)sut.Map(model); + + // Assert 1 + matcher1.Should().NotBeNull(); + matcher1.IsMatch("x").Should().Be(1.0d); + + // Act 2 + var matcher2 = (ICSharpCodeMatcher)sut.Map(model); + + // Assert 2 + matcher2.Should().NotBeNull(); + matcher2.IsMatch("x").Should().Be(1.0d); + } + + [Fact] + public void MatcherModelMapper_Map_CSharpCodeMatcher_NotAllowed_ThrowsException() + { + // Assign + var model = new MatcherModel + { + Name = "CSharpCodeMatcher", + Patterns = new[] { "x" } + }; + var sut = new MatcherMapper(new WireMockServerSettings { AllowCSharpCodeMatcher = false }); + + // Act + Action action = () => sut.Map(model); + + // Assert + action.Should().Throw(); + } + [Fact] public void MatcherModelMapper_Map_Null() { diff --git a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj index 744f3656..76c744fb 100644 --- a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj +++ b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj @@ -2,7 +2,7 @@ Stef Heyenrath - net452;netcoreapp3.1;net5.0 + net452;net461;netcoreapp3.1;net5.0 false full WireMock.Net.Tests @@ -26,6 +26,7 @@ + diff --git a/test/WireMock.Net.Tests/WireMockAdminApiTests.cs b/test/WireMock.Net.Tests/WireMockAdminApiTests.cs index 99a18d19..bc2ec470 100644 --- a/test/WireMock.Net.Tests/WireMockAdminApiTests.cs +++ b/test/WireMock.Net.Tests/WireMockAdminApiTests.cs @@ -1,4 +1,5 @@ -using System; +#if !NET452 && !NET461 +using System; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; @@ -552,4 +553,5 @@ namespace WireMock.Net.Tests server.Stop(); } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/test/WireMock.Net.Tests/WireMockServerTests.cs b/test/WireMock.Net.Tests/WireMockServerTests.cs index d4fffd7a..bc35f3b3 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.cs @@ -175,7 +175,7 @@ namespace WireMock.Net.Tests server.Stop(); } -#if !NET452 +#if !NET452 && !NET461 [Theory] [InlineData("TRACE")] [InlineData("GET")]