diff --git a/Directory.Build.props b/Directory.Build.props index 2823ad1e9..bb5dd4bee 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -4,7 +4,7 @@ - 1.13.0 + 1.14.0-preview-01 WireMock.Net-Logo.png https://github.com/wiremock/WireMock.Net Apache-2.0 diff --git a/src/WireMock.Net.Abstractions/Admin/Settings/ProxyUrlReplaceSettingsModel.cs b/src/WireMock.Net.Abstractions/Admin/Settings/ProxyUrlReplaceSettingsModel.cs index cc9ce5a64..a3f54d90c 100644 --- a/src/WireMock.Net.Abstractions/Admin/Settings/ProxyUrlReplaceSettingsModel.cs +++ b/src/WireMock.Net.Abstractions/Admin/Settings/ProxyUrlReplaceSettingsModel.cs @@ -1,5 +1,7 @@ // Copyright © WireMock.Net +using WireMock.Types; + namespace WireMock.Admin.Settings; /// @@ -11,15 +13,25 @@ public class ProxyUrlReplaceSettingsModel /// /// The old path value to be replaced by the new path value /// - public string OldValue { get; set; } = null!; + public string? OldValue { get; set; } /// /// The new path value to replace the old value with /// - public string NewValue { get; set; } = null!; + public string? NewValue { get; set; } /// - /// Defines if the case should be ignore when replacing. + /// Defines if the case should be ignored when replacing. /// public bool IgnoreCase { get; set; } + + /// + /// Holds the transformation template. + /// + public string? TransformTemplate { get; set; } + + /// + /// The transformer type. + /// + public TransformerType TransformerType { get; set; } = TransformerType.Handlebars; } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Settings/WireMockServerSettingsParser.cs b/src/WireMock.Net.Minimal/Settings/WireMockServerSettingsParser.cs index d65029b19..f9d4dd755 100644 --- a/src/WireMock.Net.Minimal/Settings/WireMockServerSettingsParser.cs +++ b/src/WireMock.Net.Minimal/Settings/WireMockServerSettingsParser.cs @@ -202,14 +202,27 @@ private static void ParseWebProxyAddressSettings(ProxyAndRecordSettings settings private static void ParseProxyUrlReplaceSettings(ProxyAndRecordSettings settings, SimpleSettingsParser parser) { - var proxyUrlReplaceOldValue = parser.GetStringValue("ProxyUrlReplaceOldValue"); - var proxyUrlReplaceNewValue = parser.GetStringValue("ProxyUrlReplaceNewValue"); + const string prefix = "ProxyUrlReplace"; + var proxyUrlReplaceOldValue = parser.GetStringValue($"{prefix}OldValue"); + var proxyUrlReplaceNewValue = parser.GetStringValue($"{prefix}NewValue"); if (!string.IsNullOrEmpty(proxyUrlReplaceOldValue) && proxyUrlReplaceNewValue != null) { settings.ReplaceSettings = new ProxyUrlReplaceSettings { - OldValue = proxyUrlReplaceOldValue!, - NewValue = proxyUrlReplaceNewValue + OldValue = proxyUrlReplaceOldValue, + NewValue = proxyUrlReplaceNewValue, + IgnoreCase = parser.GetBoolValue($"{prefix}IgnoreCase") + }; + return; + } + + var transformTemplate = parser.GetStringValue($"{prefix}TransformTemplate"); + if (!string.IsNullOrEmpty(transformTemplate)) + { + settings.ReplaceSettings = new ProxyUrlReplaceSettings + { + TransformTemplate = transformTemplate, + TransformerType = parser.GetEnumValue($"{prefix}TransformerType", TransformerType.Handlebars) }; } } diff --git a/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplaceSettings_And_TransformTemplate.verified.txt b/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplaceSettings_And_TransformTemplate.verified.txt new file mode 100644 index 000000000..61180ff86 --- /dev/null +++ b/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplaceSettings_And_TransformTemplate.verified.txt @@ -0,0 +1,29 @@ +{ + Guid: 90356dba-b36c-469a-a17e-669cd84f1f06, + UpdatedAt: DateTime_1, + Request: { + Path: { + Matchers: [ + { + Name: WildcardMatcher, + Pattern: /1, + IgnoreCase: false + } + ] + }, + Body: { + Matcher: { + Name: RegexMatcher, + Pattern: hello, + IgnoreCase: true + } + } + }, + Response: { + ProxyUrl: https://my-proxy.com, + ProxyUrlReplaceSettings: { + IgnoreCase: false, + TransformTemplate: x{{this}}y + } + } +} \ No newline at end of file diff --git a/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.cs b/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.cs index 20aaea0b2..257cca894 100644 --- a/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.cs +++ b/test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.cs @@ -450,6 +450,55 @@ public async Task IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplac server.Stop(); } + [Fact] + public async Task IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplaceSettings_And_TransformTemplate() + { + // Arrange + var guid = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f06"); + var server = WireMockServer.StartWithAdminInterface(); + var api = RestClient.For(server.Url); + + // Act + var model = new MappingModel + { + Guid = guid, + Request = new RequestModel + { + Path = "/1", + Body = new BodyModel + { + Matcher = new MatcherModel + { + Name = "RegexMatcher", + Pattern = "hello", + IgnoreCase = true + } + } + }, + Response = new ResponseModel + { + ProxyUrl = "https://my-proxy.com", + ProxyUrlReplaceSettings = new ProxyUrlReplaceSettingsModel + { + TransformTemplate = "x{{this}}y" + } + } + }; + var postMappingResult = await api.PostMappingAsync(model).ConfigureAwait(false); + + // Assert + postMappingResult.Should().NotBeNull(); + + var mapping = server.Mappings.FirstOrDefault(m => m.Guid == guid); + mapping.Should().NotBeNull(); + + var getMappingResult = await api.GetMappingAsync(guid).ConfigureAwait(false); + + await Verifier.Verify(getMappingResult, VerifySettings).DontScrubGuids(); + + server.Stop(); + } + [Fact] public async Task IWireMockAdminApi_GetRequestsAsync_Json() {