Skip to content

Commit b9019a2

Browse files
authored
Update ProxyUrlReplaceSettingsModel with TransformTemplate property (#1362)
* Update ProxyUrlReplaceSettingsModel with TransformTemplate property + parse settings correctly * oldValue nullable * <Version>1.14.0-preview-01</Version>
1 parent b82dad2 commit b9019a2

File tree

5 files changed

+111
-8
lines changed

5 files changed

+111
-8
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</PropertyGroup>
55

66
<PropertyGroup>
7-
<Version>1.13.0</Version>
7+
<Version>1.14.0-preview-01</Version>
88
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
99
<PackageProjectUrl>https://github.com/wiremock/WireMock.Net</PackageProjectUrl>
1010
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>

src/WireMock.Net.Abstractions/Admin/Settings/ProxyUrlReplaceSettingsModel.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright © WireMock.Net
22

3+
using WireMock.Types;
4+
35
namespace WireMock.Admin.Settings;
46

57
/// <summary>
@@ -11,15 +13,25 @@ public class ProxyUrlReplaceSettingsModel
1113
/// <summary>
1214
/// The old path value to be replaced by the new path value
1315
/// </summary>
14-
public string OldValue { get; set; } = null!;
16+
public string? OldValue { get; set; }
1517

1618
/// <summary>
1719
/// The new path value to replace the old value with
1820
/// </summary>
19-
public string NewValue { get; set; } = null!;
21+
public string? NewValue { get; set; }
2022

2123
/// <summary>
22-
/// Defines if the case should be ignore when replacing.
24+
/// Defines if the case should be ignored when replacing.
2325
/// </summary>
2426
public bool IgnoreCase { get; set; }
27+
28+
/// <summary>
29+
/// Holds the transformation template.
30+
/// </summary>
31+
public string? TransformTemplate { get; set; }
32+
33+
/// <summary>
34+
/// The transformer type.
35+
/// </summary>
36+
public TransformerType TransformerType { get; set; } = TransformerType.Handlebars;
2537
}

src/WireMock.Net.Minimal/Settings/WireMockServerSettingsParser.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,27 @@ private static void ParseWebProxyAddressSettings(ProxyAndRecordSettings settings
202202

203203
private static void ParseProxyUrlReplaceSettings(ProxyAndRecordSettings settings, SimpleSettingsParser parser)
204204
{
205-
var proxyUrlReplaceOldValue = parser.GetStringValue("ProxyUrlReplaceOldValue");
206-
var proxyUrlReplaceNewValue = parser.GetStringValue("ProxyUrlReplaceNewValue");
205+
const string prefix = "ProxyUrlReplace";
206+
var proxyUrlReplaceOldValue = parser.GetStringValue($"{prefix}OldValue");
207+
var proxyUrlReplaceNewValue = parser.GetStringValue($"{prefix}NewValue");
207208
if (!string.IsNullOrEmpty(proxyUrlReplaceOldValue) && proxyUrlReplaceNewValue != null)
208209
{
209210
settings.ReplaceSettings = new ProxyUrlReplaceSettings
210211
{
211-
OldValue = proxyUrlReplaceOldValue!,
212-
NewValue = proxyUrlReplaceNewValue
212+
OldValue = proxyUrlReplaceOldValue,
213+
NewValue = proxyUrlReplaceNewValue,
214+
IgnoreCase = parser.GetBoolValue($"{prefix}IgnoreCase")
215+
};
216+
return;
217+
}
218+
219+
var transformTemplate = parser.GetStringValue($"{prefix}TransformTemplate");
220+
if (!string.IsNullOrEmpty(transformTemplate))
221+
{
222+
settings.ReplaceSettings = new ProxyUrlReplaceSettings
223+
{
224+
TransformTemplate = transformTemplate,
225+
TransformerType = parser.GetEnumValue($"{prefix}TransformerType", TransformerType.Handlebars)
213226
};
214227
}
215228
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
Guid: 90356dba-b36c-469a-a17e-669cd84f1f06,
3+
UpdatedAt: DateTime_1,
4+
Request: {
5+
Path: {
6+
Matchers: [
7+
{
8+
Name: WildcardMatcher,
9+
Pattern: /1,
10+
IgnoreCase: false
11+
}
12+
]
13+
},
14+
Body: {
15+
Matcher: {
16+
Name: RegexMatcher,
17+
Pattern: hello,
18+
IgnoreCase: true
19+
}
20+
}
21+
},
22+
Response: {
23+
ProxyUrl: https://my-proxy.com,
24+
ProxyUrlReplaceSettings: {
25+
IgnoreCase: false,
26+
TransformTemplate: x{{this}}y
27+
}
28+
}
29+
}

test/WireMock.Net.Tests/AdminApi/WireMockAdminApiTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,55 @@ public async Task IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplac
450450
server.Stop();
451451
}
452452

453+
[Fact]
454+
public async Task IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplaceSettings_And_TransformTemplate()
455+
{
456+
// Arrange
457+
var guid = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f06");
458+
var server = WireMockServer.StartWithAdminInterface();
459+
var api = RestClient.For<IWireMockAdminApi>(server.Url);
460+
461+
// Act
462+
var model = new MappingModel
463+
{
464+
Guid = guid,
465+
Request = new RequestModel
466+
{
467+
Path = "/1",
468+
Body = new BodyModel
469+
{
470+
Matcher = new MatcherModel
471+
{
472+
Name = "RegexMatcher",
473+
Pattern = "hello",
474+
IgnoreCase = true
475+
}
476+
}
477+
},
478+
Response = new ResponseModel
479+
{
480+
ProxyUrl = "https://my-proxy.com",
481+
ProxyUrlReplaceSettings = new ProxyUrlReplaceSettingsModel
482+
{
483+
TransformTemplate = "x{{this}}y"
484+
}
485+
}
486+
};
487+
var postMappingResult = await api.PostMappingAsync(model).ConfigureAwait(false);
488+
489+
// Assert
490+
postMappingResult.Should().NotBeNull();
491+
492+
var mapping = server.Mappings.FirstOrDefault(m => m.Guid == guid);
493+
mapping.Should().NotBeNull();
494+
495+
var getMappingResult = await api.GetMappingAsync(guid).ConfigureAwait(false);
496+
497+
await Verifier.Verify(getMappingResult, VerifySettings).DontScrubGuids();
498+
499+
server.Stop();
500+
}
501+
453502
[Fact]
454503
public async Task IWireMockAdminApi_GetRequestsAsync_Json()
455504
{

0 commit comments

Comments
 (0)