diff --git a/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs b/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs index 0b7218080..215ee99c6 100644 --- a/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs +++ b/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs @@ -320,6 +320,23 @@ public static void Run() .WithHeader("Keep-Alive-Test", "stef") ); + server + .Given(Request.Create() + .UsingGet() + .WithPath("/proxy-replace") + ) + .RespondWith(Response.Create() + .WithProxy(new ProxyAndRecordSettings + { + Url = "http://localhost:9999", + ReplaceSettings = new ProxyUrlReplaceSettings + { + OldValue = "old", + NewValue = "new" + } + }) + ); + server .Given(Request.Create() .WithPath("/xpath").UsingPost() diff --git a/src/WireMock.Net.Abstractions/Admin/Mappings/ResponseModel.cs b/src/WireMock.Net.Abstractions/Admin/Mappings/ResponseModel.cs index d0ff23097..0dacb64eb 100644 --- a/src/WireMock.Net.Abstractions/Admin/Mappings/ResponseModel.cs +++ b/src/WireMock.Net.Abstractions/Admin/Mappings/ResponseModel.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using WireMock.Admin.Settings; namespace WireMock.Admin.Mappings; @@ -103,6 +104,11 @@ public class ResponseModel /// public string? ProxyUrl { get; set; } + /// + /// Defines the Proxy Url Replace Settings. + /// + public ProxyUrlReplaceSettingsModel? ProxyUrlReplaceSettings { get; set; } + /// /// The client X509Certificate2 Thumbprint or SubjectName to use. /// diff --git a/src/WireMock.Net.Abstractions/Admin/Settings/ProxyAndRecordSettingsModel.cs b/src/WireMock.Net.Abstractions/Admin/Settings/ProxyAndRecordSettingsModel.cs index 5bf9e95c9..0a789b3cd 100644 --- a/src/WireMock.Net.Abstractions/Admin/Settings/ProxyAndRecordSettingsModel.cs +++ b/src/WireMock.Net.Abstractions/Admin/Settings/ProxyAndRecordSettingsModel.cs @@ -70,7 +70,7 @@ public class ProxyAndRecordSettingsModel public bool AppendGuidToSavedMappingFile { get; set; } /// - /// Defines the Replace Settings + /// Defines the Replace Settings. /// public ProxyUrlReplaceSettingsModel? ReplaceSettings { get; set; } } \ No newline at end of file diff --git a/src/WireMock.Net.Abstractions/Admin/Settings/WebProxySettings.cs b/src/WireMock.Net.Abstractions/Admin/Settings/WebProxySettings.cs index 8c6c081d2..51b22f270 100644 --- a/src/WireMock.Net.Abstractions/Admin/Settings/WebProxySettings.cs +++ b/src/WireMock.Net.Abstractions/Admin/Settings/WebProxySettings.cs @@ -1,24 +1,23 @@ -namespace WireMock.Admin.Settings +namespace WireMock.Admin.Settings; + +/// +/// WebProxySettings +/// +[FluentBuilder.AutoGenerateBuilder] +public class WebProxySettingsModel { /// - /// WebProxySettings + /// A string instance that contains the address of the proxy server. /// - [FluentBuilder.AutoGenerateBuilder] - public class WebProxySettingsModel - { - /// - /// A string instance that contains the address of the proxy server. - /// - public string Address { get; set; } + public string Address { get; set; } = null!; - /// - /// The user name associated with the credentials. - /// - public string? UserName { get; set; } + /// + /// The user name associated with the credentials. + /// + public string? UserName { get; set; } - /// - /// The password for the user name associated with the credentials. - /// - public string? Password { get; set; } - } + /// + /// The password for the user name associated with the credentials. + /// + public string? Password { get; set; } } \ No newline at end of file diff --git a/src/WireMock.Net/Serialization/MappingConverter.cs b/src/WireMock.Net/Serialization/MappingConverter.cs index eccacf8a1..c7cac97ab 100644 --- a/src/WireMock.Net/Serialization/MappingConverter.cs +++ b/src/WireMock.Net/Serialization/MappingConverter.cs @@ -373,9 +373,11 @@ public MappingModel ToMappingModel(IMapping mapping) mappingModel.Response.UseTransformerForBodyAsFile = null; mappingModel.Response.TransformerReplaceNodeOptions = null; mappingModel.Response.BodyEncoding = null; - mappingModel.Response.ProxyUrl = response.ProxyAndRecordSettings.Url; mappingModel.Response.Fault = null; - mappingModel.Response.WebProxy = MapWebProxy(response.ProxyAndRecordSettings.WebProxySettings); + + mappingModel.Response.WebProxy = TinyMapperUtils.Instance.Map(response.ProxyAndRecordSettings.WebProxySettings); + mappingModel.Response.ProxyUrl = response.ProxyAndRecordSettings.Url; + mappingModel.Response.ProxyUrlReplaceSettings = TinyMapperUtils.Instance.Map(response.ProxyAndRecordSettings.ReplaceSettings); } else { @@ -509,16 +511,6 @@ private static string ToValueArguments(string[]? values, string defaultValue = " return values is { } ? string.Join(", ", values.Select(ToCSharpStringLiteral)) : ToCSharpStringLiteral(defaultValue); } - private static WebProxyModel? MapWebProxy(WebProxySettings? settings) - { - return settings != null ? new WebProxyModel - { - Address = settings.Address, - UserName = settings.UserName, - Password = settings.Password - } : null; - } - private static IDictionary MapHeaders(IDictionary> dictionary) { var newDictionary = new Dictionary(); diff --git a/src/WireMock.Net/Server/WireMockServer.ConvertMapping.cs b/src/WireMock.Net/Server/WireMockServer.ConvertMapping.cs index 8b5e97e9e..cb1639948 100644 --- a/src/WireMock.Net/Server/WireMockServer.ConvertMapping.cs +++ b/src/WireMock.Net/Server/WireMockServer.ConvertMapping.cs @@ -280,12 +280,8 @@ private static IResponseBuilder InitResponseBuilder(ResponseModel responseModel) { Url = responseModel.ProxyUrl!, ClientX509Certificate2ThumbprintOrSubjectName = responseModel.X509Certificate2ThumbprintOrSubjectName, - WebProxySettings = responseModel.WebProxy != null ? new WebProxySettings - { - Address = responseModel.WebProxy.Address, - UserName = responseModel.WebProxy.UserName, - Password = responseModel.WebProxy.Password - } : null + WebProxySettings = TinyMapperUtils.Instance.Map(responseModel.WebProxy), + ReplaceSettings = TinyMapperUtils.Instance.Map(responseModel.ProxyUrlReplaceSettings) }; return responseBuilder.WithProxy(proxyAndRecordSettings); diff --git a/src/WireMock.Net/Util/TinyMapperUtils.cs b/src/WireMock.Net/Util/TinyMapperUtils.cs index 06de691ae..52c74f746 100644 --- a/src/WireMock.Net/Util/TinyMapperUtils.cs +++ b/src/WireMock.Net/Util/TinyMapperUtils.cs @@ -1,4 +1,5 @@ using Nelibur.ObjectMapper; +using WireMock.Admin.Mappings; using WireMock.Admin.Settings; using WireMock.Settings; @@ -12,10 +13,12 @@ private TinyMapperUtils() { TinyMapper.Bind(); TinyMapper.Bind(); + TinyMapper.Bind(); TinyMapper.Bind(); TinyMapper.Bind(); TinyMapper.Bind(); + TinyMapper.Bind(); TinyMapper.Bind(); } @@ -28,4 +31,24 @@ private TinyMapperUtils() { return model == null ? null : TinyMapper.Map(model); } + + public ProxyUrlReplaceSettingsModel? Map(ProxyUrlReplaceSettings? instance) + { + return instance == null ? null : TinyMapper.Map(instance); + } + + public ProxyUrlReplaceSettings? Map(ProxyUrlReplaceSettingsModel? model) + { + return model == null ? null : TinyMapper.Map(model); + } + + public WebProxyModel? Map(WebProxySettings? instance) + { + return instance == null ? null : TinyMapper.Map(instance); + } + + public WebProxySettings? Map(WebProxyModel? model) + { + return model == null ? null : TinyMapper.Map(model); + } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/WireMockAdminApiTests.IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplaceSettings.verified.txt b/test/WireMock.Net.Tests/WireMockAdminApiTests.IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplaceSettings.verified.txt new file mode 100644 index 000000000..12e99b632 --- /dev/null +++ b/test/WireMock.Net.Tests/WireMockAdminApiTests.IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplaceSettings.verified.txt @@ -0,0 +1,30 @@ +{ + Guid: 90356dba-b36c-469a-a17e-669cd84f1f05, + 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: { + OldValue: x, + NewValue: y, + IgnoreCase: true + } + } +} \ No newline at end of file diff --git a/test/WireMock.Net.Tests/WireMockAdminApiTests.cs b/test/WireMock.Net.Tests/WireMockAdminApiTests.cs index e566b7bda..43f83a8e4 100644 --- a/test/WireMock.Net.Tests/WireMockAdminApiTests.cs +++ b/test/WireMock.Net.Tests/WireMockAdminApiTests.cs @@ -351,6 +351,57 @@ public async Task IWireMockAdminApi_GetMappingAsync_WithBodyModelMatcherModel_Wi server.Stop(); } + [Fact] + public async Task IWireMockAdminApi_GetMappingAsync_WithProxy_And_ProxyUrlReplaceSettings() + { + // Arrange + var guid = Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05"); + 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 + { + OldValue = "x", + NewValue = "y", + IgnoreCase = true + } + } + }; + 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() {