From 1221d52c696431045d5920e59b45b760ee519c2d Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 19 Mar 2023 09:19:53 +0100 Subject: [PATCH] Add DeserializeFormUrl Encoded to the settings (#905) * Add DeserializeFormUrlEncoded to Settings * EmptyArray<>.Value * . --- .../Admin/Settings/SettingsModel.cs | 34 ++++- .../Http/HttpResponseMessageHelper.cs | 8 +- src/WireMock.Net/Json/JObjectExtensions.cs | 2 +- .../Matchers/NotNullOrEmptyMatcher.cs | 3 +- src/WireMock.Net/Proxy/ProxyHelper.cs | 14 ++- .../Serialization/MappingConverter.cs | 2 +- .../Server/WireMockServer.Admin.cs | 22 ++-- .../Settings/ProxySaveMappingSetting.cs | 4 +- .../Settings/SimpleCommandLineParser.cs | 2 +- .../Settings/WireMockServerSettings.cs | 24 ++-- .../Settings/WireMockServerSettingsParser.cs | 3 +- .../Util/HttpStatusRangeParser.cs | 16 +-- src/WireMock.Net/Util/RegexUtils.cs | 12 +- src/WireMock.Net/Util/TypeBuilderUtils.cs | 119 ------------------ .../Validation/ValidatedNotNullAttribute.cs | 11 -- .../Matchers/JmesPathMatcherTests.cs | 3 +- .../Matchers/JsonMatcherTests.cs | 2 +- .../Matchers/JsonPartialMatcherTests.cs | 2 +- .../JsonPartialWildcardMatcherTests.cs | 2 +- .../Matchers/JsonPathMatcherTests.cs | 3 +- 20 files changed, 101 insertions(+), 187 deletions(-) delete mode 100644 src/WireMock.Net/Util/TypeBuilderUtils.cs delete mode 100644 src/WireMock.Net/Validation/ValidatedNotNullAttribute.cs diff --git a/src/WireMock.Net.Abstractions/Admin/Settings/SettingsModel.cs b/src/WireMock.Net.Abstractions/Admin/Settings/SettingsModel.cs index 7c08512e..81dd5e14 100644 --- a/src/WireMock.Net.Abstractions/Admin/Settings/SettingsModel.cs +++ b/src/WireMock.Net.Abstractions/Admin/Settings/SettingsModel.cs @@ -31,27 +31,49 @@ public class SettingsModel public int? MaxRequestLogCount { get; set; } /// - /// Allow a Body for all HTTP Methods. (default set to false). + /// Allow a Body for all HTTP Methods. (default set to false). /// public bool? AllowBodyForAllHttpMethods { get; set; } /// - /// Handle all requests synchronously. (default set to false). + /// Allow only a HttpStatus Code in the response which is defined. (default set to false). + /// - false : also null, 0, empty or invalid HttpStatus codes are allowed. + /// - true : only codes defined in are allowed. + /// + public bool? AllowOnlyDefinedHttpStatusCodeInResponse { get; set; } + + /// + /// Set to true to disable Json deserialization when processing requests. (default set to false). + /// + public bool? DisableJsonBodyParsing { get; set; } + + /// + /// Disable support for GZip and Deflate request body decompression. (default set to false). + /// + public bool? DisableRequestBodyDecompressing { get; set; } + + /// + /// Set to true to disable FormUrlEncoded deserializing when processing requests. (default set to false). + /// + public bool? DisableDeserializeFormUrlEncoded { get; set; } + + /// + /// Handle all requests synchronously. (default set to false). /// public bool? HandleRequestsSynchronously { get; set; } /// - /// Throw an exception when the Matcher fails because of invalid input. (default set to false). + /// Throw an exception when the Matcher fails because of invalid input. (default set to false). /// public bool? ThrowExceptionWhenMatcherFails { get; set; } /// - /// Use the RegexExtended instead of the default . (default set to true). + /// Use the RegexExtended instead of the default . (default set to true). /// public bool? UseRegexExtended { get; set; } /// - /// Save unmatched requests to a file using the . (default set to false). + /// Save unmatched requests to a file using the . (default set to false). /// public bool? SaveUnmatchedRequests { get; set; } @@ -86,7 +108,7 @@ public class SettingsModel public HostingScheme? HostingScheme { get; set; } /// - /// Don't save the response-string in the LogEntry when WithBody(Func{IRequestMessage, string}) or WithBody(Func{IRequestMessage, Task{string}}) is used. (default set to false). + /// Don't save the response-string in the LogEntry when WithBody(Func{IRequestMessage, string}) or WithBody(Func{IRequestMessage, Task{string}}) is used. (default set to false). /// public bool? DoNotSaveDynamicResponseInLogEntry { get; set; } diff --git a/src/WireMock.Net/Http/HttpResponseMessageHelper.cs b/src/WireMock.Net/Http/HttpResponseMessageHelper.cs index 75cf9642..8d0e7090 100644 --- a/src/WireMock.Net/Http/HttpResponseMessageHelper.cs +++ b/src/WireMock.Net/Http/HttpResponseMessageHelper.cs @@ -15,7 +15,8 @@ internal static class HttpResponseMessageHelper Uri requiredUri, Uri originalUri, bool deserializeJson, - bool decompressGzipAndDeflate) + bool decompressGzipAndDeflate, + bool deserializeFormUrlEncoded) { var responseMessage = new ResponseMessage { StatusCode = (int)httpResponseMessage.StatusCode }; @@ -44,7 +45,8 @@ internal static class HttpResponseMessageHelper ContentType = contentTypeHeader?.FirstOrDefault(), DeserializeJson = deserializeJson, ContentEncoding = contentEncodingHeader?.FirstOrDefault(), - DecompressGZipAndDeflate = decompressGzipAndDeflate + DecompressGZipAndDeflate = decompressGzipAndDeflate, + DeserializeFormUrlEncoded = deserializeFormUrlEncoded }; responseMessage.BodyData = await BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false); } @@ -55,7 +57,7 @@ internal static class HttpResponseMessageHelper // If Location header contains absolute redirect URL, and base URL is one that we proxy to, // we need to replace it to original one. if (string.Equals(header.Key, HttpKnownHeaderNames.Location, StringComparison.OrdinalIgnoreCase) - && Uri.TryCreate(header.Value.First(), UriKind.Absolute, out Uri absoluteLocationUri) + && Uri.TryCreate(header.Value.First(), UriKind.Absolute, out var absoluteLocationUri) && string.Equals(absoluteLocationUri.Host, requiredUri.Host, StringComparison.OrdinalIgnoreCase)) { var replacedLocationUri = new Uri(originalUri, absoluteLocationUri.PathAndQuery); diff --git a/src/WireMock.Net/Json/JObjectExtensions.cs b/src/WireMock.Net/Json/JObjectExtensions.cs index 1473a489..5771fd5b 100644 --- a/src/WireMock.Net/Json/JObjectExtensions.cs +++ b/src/WireMock.Net/Json/JObjectExtensions.cs @@ -60,7 +60,7 @@ internal static class JObjectExtensions { if (src == null) { - return new object?[0]; + return EmptyArray.Value; } return ConvertJTokenArray(src, options); diff --git a/src/WireMock.Net/Matchers/NotNullOrEmptyMatcher.cs b/src/WireMock.Net/Matchers/NotNullOrEmptyMatcher.cs index a971f3e2..b45c7fe7 100644 --- a/src/WireMock.Net/Matchers/NotNullOrEmptyMatcher.cs +++ b/src/WireMock.Net/Matchers/NotNullOrEmptyMatcher.cs @@ -1,3 +1,4 @@ +using System; using System.Linq; using AnyOfTypes; using WireMock.Models; @@ -62,7 +63,7 @@ public class NotNullOrEmptyMatcher : IObjectMatcher, IStringMatcher /// public AnyOf[] GetPatterns() { - return new AnyOf[0]; + return EmptyArray>.Value; } /// diff --git a/src/WireMock.Net/Proxy/ProxyHelper.cs b/src/WireMock.Net/Proxy/ProxyHelper.cs index 803194cd..519a6056 100644 --- a/src/WireMock.Net/Proxy/ProxyHelper.cs +++ b/src/WireMock.Net/Proxy/ProxyHelper.cs @@ -45,8 +45,16 @@ internal class ProxyHelper // Create ResponseMessage bool deserializeJson = !_settings.DisableJsonBodyParsing.GetValueOrDefault(false); bool decompressGzipAndDeflate = !_settings.DisableRequestBodyDecompressing.GetValueOrDefault(false); + bool deserializeFormUrlEncoded = !_settings.DisableDeserializeFormUrlEncoded.GetValueOrDefault(false); - var responseMessage = await HttpResponseMessageHelper.CreateAsync(httpResponseMessage, requiredUri, originalUri, deserializeJson, decompressGzipAndDeflate).ConfigureAwait(false); + var responseMessage = await HttpResponseMessageHelper.CreateAsync( + httpResponseMessage, + requiredUri, + originalUri, + deserializeJson, + decompressGzipAndDeflate, + deserializeFormUrlEncoded + ).ConfigureAwait(false); IMapping? newMapping = null; @@ -56,11 +64,11 @@ internal class ProxyHelper if (saveMappingSettings != null) { save &= Check(saveMappingSettings.StatusCodePattern, - () => HttpStatusRangeParser.IsMatch(saveMappingSettings.StatusCodePattern, responseMessage.StatusCode) + () => saveMappingSettings.StatusCodePattern != null && HttpStatusRangeParser.IsMatch(saveMappingSettings.StatusCodePattern, responseMessage.StatusCode) ); save &= Check(saveMappingSettings.HttpMethods, - () => saveMappingSettings.HttpMethods.Value.Contains(requestMessage.Method, StringComparer.OrdinalIgnoreCase) + () => saveMappingSettings.HttpMethods != null && saveMappingSettings.HttpMethods.Value.Contains(requestMessage.Method, StringComparer.OrdinalIgnoreCase) ); } diff --git a/src/WireMock.Net/Serialization/MappingConverter.cs b/src/WireMock.Net/Serialization/MappingConverter.cs index bf287daa..62356cae 100644 --- a/src/WireMock.Net/Serialization/MappingConverter.cs +++ b/src/WireMock.Net/Serialization/MappingConverter.cs @@ -140,7 +140,7 @@ internal class MappingConverter { sb.AppendLine($" .WithDelay({response.Delay.Value.TotalMilliseconds})"); } - else if (response.MinimumDelayMilliseconds > 0 && response.MaximumDelayMilliseconds > 0) + else if (response is { MinimumDelayMilliseconds: > 0, MaximumDelayMilliseconds: > 0 }) { sb.AppendLine($" .WithRandomDelay({response.MinimumDelayMilliseconds}, {response.MaximumDelayMilliseconds})"); } diff --git a/src/WireMock.Net/Server/WireMockServer.Admin.cs b/src/WireMock.Net/Server/WireMockServer.Admin.cs index 04227f41..56e7d2b4 100644 --- a/src/WireMock.Net/Server/WireMockServer.Admin.cs +++ b/src/WireMock.Net/Server/WireMockServer.Admin.cs @@ -217,10 +217,17 @@ public partial class WireMockServer var model = new SettingsModel { AllowBodyForAllHttpMethods = _settings.AllowBodyForAllHttpMethods, + AllowOnlyDefinedHttpStatusCodeInResponse = _settings.AllowOnlyDefinedHttpStatusCodeInResponse, AllowPartialMapping = _settings.AllowPartialMapping, + DisableDeserializeFormUrlEncoded = _settings.DisableDeserializeFormUrlEncoded, + DisableJsonBodyParsing = _settings.DisableJsonBodyParsing, + DisableRequestBodyDecompressing = _settings.DisableRequestBodyDecompressing, + DoNotSaveDynamicResponseInLogEntry = _settings.DoNotSaveDynamicResponseInLogEntry, GlobalProcessingDelay = (int?)_options.RequestProcessingDelay?.TotalMilliseconds, HandleRequestsSynchronously = _settings.HandleRequestsSynchronously, + HostingScheme = _settings.HostingScheme, MaxRequestLogCount = _settings.MaxRequestLogCount, + QueryParameterMultipleValueSupport = _settings.QueryParameterMultipleValueSupport, ReadStaticMappings = _settings.ReadStaticMappings, RequestLogExpirationDuration = _settings.RequestLogExpirationDuration, SaveUnmatchedRequests = _settings.SaveUnmatchedRequests, @@ -228,14 +235,11 @@ public partial class WireMockServer UseRegexExtended = _settings.UseRegexExtended, WatchStaticMappings = _settings.WatchStaticMappings, WatchStaticMappingsInSubdirectories = _settings.WatchStaticMappingsInSubdirectories, - HostingScheme = _settings.HostingScheme, - DoNotSaveDynamicResponseInLogEntry = _settings.DoNotSaveDynamicResponseInLogEntry, - QueryParameterMultipleValueSupport = _settings.QueryParameterMultipleValueSupport, #if USE_ASPNETCORE - CorsPolicyOptions = _settings.CorsPolicyOptions?.ToString(), + AcceptAnyClientCertificate = _settings.AcceptAnyClientCertificate, ClientCertificateMode = _settings.ClientCertificateMode, - AcceptAnyClientCertificate = _settings.AcceptAnyClientCertificate + CorsPolicyOptions = _settings.CorsPolicyOptions?.ToString() #endif }; @@ -250,10 +254,16 @@ public partial class WireMockServer // _settings _settings.AllowBodyForAllHttpMethods = settings.AllowBodyForAllHttpMethods; + _settings.AllowOnlyDefinedHttpStatusCodeInResponse = settings.AllowOnlyDefinedHttpStatusCodeInResponse; _settings.AllowPartialMapping = settings.AllowPartialMapping; + _settings.DisableDeserializeFormUrlEncoded = settings.DisableDeserializeFormUrlEncoded; + _settings.DisableJsonBodyParsing = settings.DisableJsonBodyParsing; + _settings.DisableRequestBodyDecompressing = settings.DisableRequestBodyDecompressing; + _settings.DoNotSaveDynamicResponseInLogEntry = settings.DoNotSaveDynamicResponseInLogEntry; _settings.HandleRequestsSynchronously = settings.HandleRequestsSynchronously; _settings.MaxRequestLogCount = settings.MaxRequestLogCount; _settings.ProxyAndRecordSettings = TinyMapperUtils.Instance.Map(settings.ProxyAndRecordSettings); + _settings.QueryParameterMultipleValueSupport = settings.QueryParameterMultipleValueSupport; _settings.ReadStaticMappings = settings.ReadStaticMappings; _settings.RequestLogExpirationDuration = settings.RequestLogExpirationDuration; _settings.SaveUnmatchedRequests = settings.SaveUnmatchedRequests; @@ -261,8 +271,6 @@ public partial class WireMockServer _settings.UseRegexExtended = settings.UseRegexExtended; _settings.WatchStaticMappings = settings.WatchStaticMappings; _settings.WatchStaticMappingsInSubdirectories = settings.WatchStaticMappingsInSubdirectories; - _settings.DoNotSaveDynamicResponseInLogEntry = settings.DoNotSaveDynamicResponseInLogEntry; - _settings.QueryParameterMultipleValueSupport = settings.QueryParameterMultipleValueSupport; InitSettings(_settings); diff --git a/src/WireMock.Net/Settings/ProxySaveMappingSetting.cs b/src/WireMock.Net/Settings/ProxySaveMappingSetting.cs index 5f209eaa..ff5fd5f8 100644 --- a/src/WireMock.Net/Settings/ProxySaveMappingSetting.cs +++ b/src/WireMock.Net/Settings/ProxySaveMappingSetting.cs @@ -6,7 +6,7 @@ public class ProxySaveMappingSetting { public MatchBehaviour MatchBehaviour { get; } = MatchBehaviour.AcceptOnMatch; - public T Value { get; private set; } + public T Value { get; } public ProxySaveMappingSetting(T value, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { @@ -14,7 +14,7 @@ public class ProxySaveMappingSetting MatchBehaviour = matchBehaviour; } - public static implicit operator ProxySaveMappingSetting(T value) => new ProxySaveMappingSetting(value); + public static implicit operator ProxySaveMappingSetting(T value) => new(value); public static implicit operator T(ProxySaveMappingSetting @this) => @this.Value; } \ No newline at end of file diff --git a/src/WireMock.Net/Settings/SimpleCommandLineParser.cs b/src/WireMock.Net/Settings/SimpleCommandLineParser.cs index e7be36a7..386e0883 100644 --- a/src/WireMock.Net/Settings/SimpleCommandLineParser.cs +++ b/src/WireMock.Net/Settings/SimpleCommandLineParser.cs @@ -32,7 +32,7 @@ internal class SimpleCommandLineParser } else if (string.IsNullOrEmpty(currentName)) { - Arguments[arg] = new string[0]; + Arguments[arg] = EmptyArray.Value; } else { diff --git a/src/WireMock.Net/Settings/WireMockServerSettings.cs b/src/WireMock.Net/Settings/WireMockServerSettings.cs index ea729946..8282a97d 100644 --- a/src/WireMock.Net/Settings/WireMockServerSettings.cs +++ b/src/WireMock.Net/Settings/WireMockServerSettings.cs @@ -182,13 +182,13 @@ public class WireMockServerSettings public bool? AllowCSharpCodeMatcher { get; set; } /// - /// Allow a Body for all HTTP Methods. (default set to false). + /// Allow a Body for all HTTP Methods. (default set to false). /// [PublicAPI] public bool? AllowBodyForAllHttpMethods { get; set; } /// - /// Allow only a HttpStatus Code in the response which is defined. (default set to false). + /// Allow only a HttpStatus Code in the response which is defined. (default set to false). /// - false : also null, 0, empty or invalid HttpStatus codes are allowed. /// - true : only codes defined in are allowed. /// @@ -196,25 +196,31 @@ public class WireMockServerSettings public bool? AllowOnlyDefinedHttpStatusCodeInResponse { get; set; } /// - /// Set to true to disable Json deserialization when processing requests. (default set to false). + /// Set to true to disable Json deserialization when processing requests. (default set to false). /// [PublicAPI] public bool? DisableJsonBodyParsing { get; set; } /// - /// Disable support for GZip and Deflate request body decompression. (default set to false). + /// Disable support for GZip and Deflate request body decompression. (default set to false). /// [PublicAPI] public bool? DisableRequestBodyDecompressing { get; set; } /// - /// Handle all requests synchronously. (default set to false). + /// Set to true to disable FormUrlEncoded deserializing when processing requests. (default set to false). + /// + [PublicAPI] + public bool? DisableDeserializeFormUrlEncoded { get; set; } + + /// + /// Handle all requests synchronously. (default set to false). /// [PublicAPI] public bool? HandleRequestsSynchronously { get; set; } /// - /// Throw an exception when the fails because of invalid input. (default set to false). + /// Throw an exception when the fails because of invalid input. (default set to false). /// [PublicAPI] public bool? ThrowExceptionWhenMatcherFails { get; set; } @@ -255,19 +261,19 @@ public class WireMockServerSettings public WebhookSettings? WebhookSettings { get; set; } /// - /// Use the instead of the default (default set to true). + /// Use the instead of the default (default set to true). /// [PublicAPI] public bool? UseRegexExtended { get; set; } = true; /// - /// Save unmatched requests to a file using the (default set to false). + /// Save unmatched requests to a file using the (default set to false). /// [PublicAPI] public bool? SaveUnmatchedRequests { get; set; } /// - /// Don't save the response-string in the LogEntry when WithBody(Func{IRequestMessage, string}) or WithBody(Func{IRequestMessage, Task{string}}) is used. (default set to false). + /// Don't save the response-string in the LogEntry when WithBody(Func{IRequestMessage, string}) or WithBody(Func{IRequestMessage, Task{string}}) is used. (default set to false). /// [PublicAPI] public bool? DoNotSaveDynamicResponseInLogEntry { get; set; } diff --git a/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs b/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs index 49216dc1..f4c6c5be 100644 --- a/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs +++ b/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs @@ -4,7 +4,6 @@ using System.Linq; using JetBrains.Annotations; using Stef.Validation; using WireMock.Logging; -using WireMock.Matchers; using WireMock.Types; using WireMock.Util; @@ -47,6 +46,8 @@ public static class WireMockServerSettingsParser AllowOnlyDefinedHttpStatusCodeInResponse = parser.GetBoolValue("AllowOnlyDefinedHttpStatusCodeInResponse"), AllowPartialMapping = parser.GetBoolValue("AllowPartialMapping"), DisableJsonBodyParsing = parser.GetBoolValue("DisableJsonBodyParsing"), + DisableRequestBodyDecompressing = parser.GetBoolValue(nameof(WireMockServerSettings.DisableRequestBodyDecompressing)), + DisableDeserializeFormUrlEncoded = parser.GetBoolValue(nameof(WireMockServerSettings.DisableDeserializeFormUrlEncoded)), HandleRequestsSynchronously = parser.GetBoolValue("HandleRequestsSynchronously"), MaxRequestLogCount = parser.GetIntValue("MaxRequestLogCount"), ReadStaticMappings = parser.GetBoolValue("ReadStaticMappings"), diff --git a/src/WireMock.Net/Util/HttpStatusRangeParser.cs b/src/WireMock.Net/Util/HttpStatusRangeParser.cs index 936dccd7..3d74f8d9 100644 --- a/src/WireMock.Net/Util/HttpStatusRangeParser.cs +++ b/src/WireMock.Net/Util/HttpStatusRangeParser.cs @@ -16,18 +16,14 @@ internal static class HttpStatusRangeParser /// The pattern. (Can be null, in that case it's allowed.) /// The value. /// is invalid. - public static bool IsMatch(string pattern, object? httpStatusCode) + public static bool IsMatch(string? pattern, object? httpStatusCode) { - switch (httpStatusCode) + return httpStatusCode switch { - case int statusCodeAsInteger: - return IsMatch(pattern, statusCodeAsInteger); - - case string statusCodeAsString: - return IsMatch(pattern, int.Parse(statusCodeAsString)); - } - - return false; + int statusCodeAsInteger => IsMatch(pattern, statusCodeAsInteger), + string statusCodeAsString => IsMatch(pattern, int.Parse(statusCodeAsString)), + _ => false + }; } /// diff --git a/src/WireMock.Net/Util/RegexUtils.cs b/src/WireMock.Net/Util/RegexUtils.cs index ec819169..9b33aff8 100644 --- a/src/WireMock.Net/Util/RegexUtils.cs +++ b/src/WireMock.Net/Util/RegexUtils.cs @@ -36,14 +36,12 @@ internal static class RegexUtils { if (useRegexExtended) { - var r = new RegexExtended(pattern, RegexOptions.None, RegexTimeOut); - return (true, r.IsMatch(input)); - } - else - { - var r = new Regex(pattern, RegexOptions.None, RegexTimeOut); - return (true, r.IsMatch(input)); + var regexExtended = new RegexExtended(pattern, RegexOptions.None, RegexTimeOut); + return (true, regexExtended.IsMatch(input)); } + + var regex = new Regex(pattern, RegexOptions.None, RegexTimeOut); + return (true, regex.IsMatch(input)); } catch { diff --git a/src/WireMock.Net/Util/TypeBuilderUtils.cs b/src/WireMock.Net/Util/TypeBuilderUtils.cs deleted file mode 100644 index 313046b9..00000000 --- a/src/WireMock.Net/Util/TypeBuilderUtils.cs +++ /dev/null @@ -1,119 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Reflection.Emit; - -namespace WireMock.Util; - -/// -/// Code based on https://stackoverflow.com/questions/40507909/convert-jobject-to-anonymous-object -/// -internal static class TypeBuilderUtils -{ - private static readonly ConcurrentDictionary, Type> Types = new(); - - private static readonly ModuleBuilder ModuleBuilder = AssemblyBuilder - .DefineDynamicAssembly(new AssemblyName("WireMock.Net.Reflection"), AssemblyBuilderAccess.Run) - .DefineDynamicModule("WireMock.Net.Reflection.Module"); - - public static Type BuildType(IDictionary properties, string? name = null) - { - var keyExists = Types.Keys.FirstOrDefault(k => Compare(k, properties)); - if (keyExists != null) - { - return Types[keyExists]; - } - - var typeBuilder = GetTypeBuilder(name ?? Guid.NewGuid().ToString()); - foreach (var property in properties) - { - CreateGetSetMethods(typeBuilder, property.Key, property.Value); - } - - var type = typeBuilder.CreateTypeInfo()!.AsType(); - - Types.TryAdd(properties, type); - - return type; - } - - /// - /// https://stackoverflow.com/questions/3804367/testing-for-equality-between-dictionaries-in-c-sharp - /// - private static bool Compare(IDictionary dict1, IDictionary dict2) - where TKey : notnull - { - if (dict1 == dict2) - { - return true; - } - - if (dict1.Count != dict2.Count) - { - return false; - } - - var valueComparer = EqualityComparer.Default; - - foreach (var kvp in dict1) - { - if (!dict2.TryGetValue(kvp.Key, out var value2)) - { - return false; - } - - if (!valueComparer.Equals(kvp.Value, value2)) - { - return false; - } - } - - return true; - } - - private static TypeBuilder GetTypeBuilder(string name) - { - return ModuleBuilder.DefineType(name, - TypeAttributes.Public | - TypeAttributes.Class | - TypeAttributes.AutoClass | - TypeAttributes.AnsiClass | - TypeAttributes.BeforeFieldInit | - TypeAttributes.AutoLayout, - null); - } - - private static void CreateGetSetMethods(TypeBuilder typeBuilder, string propertyName, Type propertyType) - { - var fieldBuilder = typeBuilder.DefineField("_" + propertyName, propertyType, FieldAttributes.Private); - - var propertyBuilder = typeBuilder.DefineProperty(propertyName, PropertyAttributes.HasDefault, propertyType, null); - - var getPropertyMethodBuilder = typeBuilder.DefineMethod("get_" + propertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, propertyType, Type.EmptyTypes); - var getIl = getPropertyMethodBuilder.GetILGenerator(); - - getIl.Emit(OpCodes.Ldarg_0); - getIl.Emit(OpCodes.Ldfld, fieldBuilder); - getIl.Emit(OpCodes.Ret); - - var setPropertyMethodBuilder = typeBuilder.DefineMethod("set_" + propertyName, MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, null, new[] { propertyType }); - var setIl = setPropertyMethodBuilder.GetILGenerator(); - var modifyProperty = setIl.DefineLabel(); - - var exitSet = setIl.DefineLabel(); - - setIl.MarkLabel(modifyProperty); - setIl.Emit(OpCodes.Ldarg_0); - setIl.Emit(OpCodes.Ldarg_1); - setIl.Emit(OpCodes.Stfld, fieldBuilder); - - setIl.Emit(OpCodes.Nop); - setIl.MarkLabel(exitSet); - setIl.Emit(OpCodes.Ret); - - propertyBuilder.SetGetMethod(getPropertyMethodBuilder); - propertyBuilder.SetSetMethod(setPropertyMethodBuilder); - } -} \ No newline at end of file diff --git a/src/WireMock.Net/Validation/ValidatedNotNullAttribute.cs b/src/WireMock.Net/Validation/ValidatedNotNullAttribute.cs deleted file mode 100644 index 5c4f1d05..00000000 --- a/src/WireMock.Net/Validation/ValidatedNotNullAttribute.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace WireMock.Validation -{ - /// - /// To fix 'xxx' is null on at least one execution path. See also https://rules.sonarsource.com/csharp/RSPEC-3900. - /// - internal class ValidatedNotNullAttribute : Attribute - { - } -} \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Matchers/JmesPathMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JmesPathMatcherTests.cs index 09308c1f..05d42d5e 100644 --- a/test/WireMock.Net.Tests/Matchers/JmesPathMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JmesPathMatcherTests.cs @@ -1,3 +1,4 @@ +using System; using Newtonsoft.Json.Linq; using NFluent; using WireMock.Matchers; @@ -37,7 +38,7 @@ public class JmesPathMatcherTests public void JmesPathMatcher_IsMatch_ByteArray() { // Assign - var bytes = new byte[0]; + var bytes = EmptyArray.Value; var matcher = new JmesPathMatcher(""); // Act diff --git a/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs index 8a0435eb..9b38fff0 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs @@ -111,7 +111,7 @@ public class JsonMatcherTests public void JsonMatcher_IsMatch_ByteArray() { // Assign - var bytes = new byte[0]; + var bytes = EmptyArray.Value; var matcher = new JsonMatcher(""); // Act diff --git a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs index bf76ce5b..7d4b33b4 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPartialMatcherTests.cs @@ -89,7 +89,7 @@ public class JsonPartialMatcherTests public void JsonPartialMatcher_IsMatch_ByteArray() { // Assign - var bytes = new byte[0]; + var bytes = EmptyArray.Value; var matcher = new JsonPartialMatcher(""); // Act diff --git a/test/WireMock.Net.Tests/Matchers/JsonPartialWildcardMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPartialWildcardMatcherTests.cs index e97a9013..77ce09c8 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPartialWildcardMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPartialWildcardMatcherTests.cs @@ -89,7 +89,7 @@ public class JsonPartialWildcardMatcherTests public void JsonPartialWildcardMatcher_IsMatch_ByteArray() { // Assign - var bytes = new byte[0]; + var bytes = EmptyArray.Value; var matcher = new JsonPartialWildcardMatcher(""); // Act diff --git a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs index a79ecc52..46f06c62 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs @@ -1,3 +1,4 @@ +using System; using Newtonsoft.Json.Linq; using NFluent; using WireMock.Matchers; @@ -37,7 +38,7 @@ public class JsonPathMatcherTests public void JsonPathMatcher_IsMatch_ByteArray() { // Assign - var bytes = new byte[0]; + var bytes = EmptyArray.Value; var matcher = new JsonPathMatcher(""); // Act