From 0a2763c06e795c2afcc323ed04ca938a22b44637 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 23 Apr 2023 11:56:08 +0200 Subject: [PATCH] Add IgnoreCase option to ProxyUrlReplaceSettings (#925) * Add IgnoreCase option to ProxyUrlReplaceSettings * fix --- .../Settings/ProxyUrlReplaceSettingsModel.cs | 5 +++++ .../Compatibility/StringExtensions.cs | 15 +++++++++++++++ src/WireMock.Net/Proxy/ProxyHelper.cs | 12 +++++++++++- .../Settings/ProxyUrlReplaceSettings.cs | 5 +++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/WireMock.Net/Compatibility/StringExtensions.cs diff --git a/src/WireMock.Net.Abstractions/Admin/Settings/ProxyUrlReplaceSettingsModel.cs b/src/WireMock.Net.Abstractions/Admin/Settings/ProxyUrlReplaceSettingsModel.cs index 2073acb5..b66b122b 100644 --- a/src/WireMock.Net.Abstractions/Admin/Settings/ProxyUrlReplaceSettingsModel.cs +++ b/src/WireMock.Net.Abstractions/Admin/Settings/ProxyUrlReplaceSettingsModel.cs @@ -15,4 +15,9 @@ public class ProxyUrlReplaceSettingsModel /// The new path value to replace the old value with /// public string NewValue { get; set; } = null!; + + /// + /// Defines if the case should be ignore when replacing. + /// + public bool IgnoreCase { get; set; } } \ No newline at end of file diff --git a/src/WireMock.Net/Compatibility/StringExtensions.cs b/src/WireMock.Net/Compatibility/StringExtensions.cs new file mode 100644 index 00000000..63426786 --- /dev/null +++ b/src/WireMock.Net/Compatibility/StringExtensions.cs @@ -0,0 +1,15 @@ +#if NET451 || NET452 || NET46 || NET451 || NET461 || NETSTANDARD1_3 || NETSTANDARD2_0 +using System.Text.RegularExpressions; + +// ReSharper disable once CheckNamespace +namespace System; + +internal static class StringExtensions +{ + public static string Replace(this string text, string oldValue, string newValue, StringComparison stringComparison) + { + var options = stringComparison == StringComparison.OrdinalIgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None; + return Regex.Replace(text, oldValue, newValue, options); + } +} +#endif \ No newline at end of file diff --git a/src/WireMock.Net/Proxy/ProxyHelper.cs b/src/WireMock.Net/Proxy/ProxyHelper.cs index cd0c279c..5a772826 100644 --- a/src/WireMock.Net/Proxy/ProxyHelper.cs +++ b/src/WireMock.Net/Proxy/ProxyHelper.cs @@ -38,7 +38,17 @@ internal class ProxyHelper // Create HttpRequestMessage var replaceSettings = proxyAndRecordSettings.ReplaceSettings; - var proxyUrl = replaceSettings is not null ? url.Replace(replaceSettings.OldValue, replaceSettings.NewValue) : url; + string proxyUrl; + if (replaceSettings is not null) + { + var stringComparison = replaceSettings.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; + proxyUrl = url.Replace(replaceSettings.OldValue, replaceSettings.NewValue, stringComparison); + } + else + { + proxyUrl = url; + } + var httpRequestMessage = HttpRequestMessageHelper.Create(requestMessage, proxyUrl); // Call the URL diff --git a/src/WireMock.Net/Settings/ProxyUrlReplaceSettings.cs b/src/WireMock.Net/Settings/ProxyUrlReplaceSettings.cs index 11da3362..61fabc8a 100644 --- a/src/WireMock.Net/Settings/ProxyUrlReplaceSettings.cs +++ b/src/WireMock.Net/Settings/ProxyUrlReplaceSettings.cs @@ -14,4 +14,9 @@ public class ProxyUrlReplaceSettings /// The new path value to replace the old value with /// public string NewValue { get; set; } = null!; + + /// + /// Defines if the case should be ignore when replacing. + /// + public bool IgnoreCase { get; set; } } \ No newline at end of file