Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ProxyUrlReplaceSettings to Response #1026

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/WireMock.Net.Console.Net452.Classic/MainApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions src/WireMock.Net.Abstractions/Admin/Mappings/ResponseModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using WireMock.Admin.Settings;

namespace WireMock.Admin.Mappings;

Expand Down Expand Up @@ -103,6 +104,11 @@ public class ResponseModel
/// </summary>
public string? ProxyUrl { get; set; }

/// <summary>
/// Defines the Proxy Url Replace Settings.
/// </summary>
public ProxyUrlReplaceSettingsModel? ProxyUrlReplaceSettings { get; set; }

/// <summary>
/// The client X509Certificate2 Thumbprint or SubjectName to use.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class ProxyAndRecordSettingsModel
public bool AppendGuidToSavedMappingFile { get; set; }

/// <summary>
/// Defines the Replace Settings
/// Defines the Replace Settings.
/// </summary>
public ProxyUrlReplaceSettingsModel? ReplaceSettings { get; set; }
}
35 changes: 17 additions & 18 deletions src/WireMock.Net.Abstractions/Admin/Settings/WebProxySettings.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
namespace WireMock.Admin.Settings
namespace WireMock.Admin.Settings;

/// <summary>
/// WebProxySettings
/// </summary>
[FluentBuilder.AutoGenerateBuilder]
public class WebProxySettingsModel
{
/// <summary>
/// WebProxySettings
/// A string instance that contains the address of the proxy server.
/// </summary>
[FluentBuilder.AutoGenerateBuilder]
public class WebProxySettingsModel
{
/// <summary>
/// A string instance that contains the address of the proxy server.
/// </summary>
public string Address { get; set; }
public string Address { get; set; } = null!;

/// <summary>
/// The user name associated with the credentials.
/// </summary>
public string? UserName { get; set; }
/// <summary>
/// The user name associated with the credentials.
/// </summary>
public string? UserName { get; set; }

/// <summary>
/// The password for the user name associated with the credentials.
/// </summary>
public string? Password { get; set; }
}
/// <summary>
/// The password for the user name associated with the credentials.
/// </summary>
public string? Password { get; set; }
}
16 changes: 4 additions & 12 deletions src/WireMock.Net/Serialization/MappingConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<string, object> MapHeaders(IDictionary<string, WireMockList<string>> dictionary)
{
var newDictionary = new Dictionary<string, object>();
Expand Down
8 changes: 2 additions & 6 deletions src/WireMock.Net/Server/WireMockServer.ConvertMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions src/WireMock.Net/Util/TinyMapperUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Nelibur.ObjectMapper;
using WireMock.Admin.Mappings;
using WireMock.Admin.Settings;
using WireMock.Settings;

Expand All @@ -12,10 +13,12 @@ private TinyMapperUtils()
{
TinyMapper.Bind<ProxyAndRecordSettings, ProxyAndRecordSettingsModel>();
TinyMapper.Bind<WebProxySettings, WebProxySettingsModel>();
TinyMapper.Bind<WebProxySettings, WebProxyModel>();
TinyMapper.Bind<ProxyUrlReplaceSettings, ProxyUrlReplaceSettingsModel>();

TinyMapper.Bind<ProxyAndRecordSettingsModel, ProxyAndRecordSettings>();
TinyMapper.Bind<WebProxySettingsModel, WebProxySettings>();
TinyMapper.Bind<WebProxyModel, WebProxySettings>();
TinyMapper.Bind<ProxyUrlReplaceSettingsModel, ProxyUrlReplaceSettings>();
}

Expand All @@ -28,4 +31,24 @@ private TinyMapperUtils()
{
return model == null ? null : TinyMapper.Map<ProxyAndRecordSettings>(model);
}

public ProxyUrlReplaceSettingsModel? Map(ProxyUrlReplaceSettings? instance)
{
return instance == null ? null : TinyMapper.Map<ProxyUrlReplaceSettingsModel>(instance);
}

public ProxyUrlReplaceSettings? Map(ProxyUrlReplaceSettingsModel? model)
{
return model == null ? null : TinyMapper.Map<ProxyUrlReplaceSettings>(model);
}

public WebProxyModel? Map(WebProxySettings? instance)
{
return instance == null ? null : TinyMapper.Map<WebProxyModel>(instance);
}

public WebProxySettings? Map(WebProxyModel? model)
{
return model == null ? null : TinyMapper.Map<WebProxySettings>(model);
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
}
51 changes: 51 additions & 0 deletions test/WireMock.Net.Tests/WireMockAdminApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IWireMockAdminApi>(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()
{
Expand Down
Loading