mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-27 11:17:31 +02:00
* #630 Allow removal of prefix when proxying to another server * #630 Rename replace to replace settings and ensure properties used in place of fields * #630 Update replace settings type name to ProxyUrlReplaceSettings * #630 Add admin model and update settings parser to parse new values * Fix formatting issues * #630 Ensure json mapping between admin model and internal model takes place * #630 Refactor parsing and structure of extracting new proxy url * Reduce function complexity * #630 Fix line length issues and remove try prefix from parser methods
This commit is contained in:
@@ -68,4 +68,9 @@ public class ProxyAndRecordSettingsModel
|
|||||||
/// Append an unique GUID to the filename from the saved mapping file.
|
/// Append an unique GUID to the filename from the saved mapping file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool AppendGuidToSavedMappingFile { get; set; }
|
public bool AppendGuidToSavedMappingFile { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Defines the Replace Settings
|
||||||
|
/// </summary>
|
||||||
|
public ProxyUrlReplaceSettingsModel? ReplaceSettings { get; set; }
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
namespace WireMock.Admin.Settings;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Defines an old path param and a new path param to be replaced when proxying.
|
||||||
|
/// </summary>
|
||||||
|
[FluentBuilder.AutoGenerateBuilder]
|
||||||
|
public class ProxyUrlReplaceSettingsModel
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The old path value to be replaced by the new path value
|
||||||
|
/// </summary>
|
||||||
|
public string OldValue { get; set; } = null!;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The new path value to replace the old value with
|
||||||
|
/// </summary>
|
||||||
|
public string NewValue { get; set; } = null!;
|
||||||
|
}
|
||||||
@@ -37,7 +37,9 @@ internal class ProxyHelper
|
|||||||
var requiredUri = new Uri(url);
|
var requiredUri = new Uri(url);
|
||||||
|
|
||||||
// Create HttpRequestMessage
|
// Create HttpRequestMessage
|
||||||
var httpRequestMessage = HttpRequestMessageHelper.Create(requestMessage, url);
|
var replaceSettings = proxyAndRecordSettings.ReplaceSettings;
|
||||||
|
var proxyUrl = replaceSettings is not null ? url.Replace(replaceSettings.OldValue, replaceSettings.NewValue) : url;
|
||||||
|
var httpRequestMessage = HttpRequestMessageHelper.Create(requestMessage, proxyUrl);
|
||||||
|
|
||||||
// Call the URL
|
// Call the URL
|
||||||
var httpResponseMessage = await client.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false);
|
var httpResponseMessage = await client.SendAsync(httpRequestMessage, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false);
|
||||||
|
|||||||
@@ -69,6 +69,12 @@ public class ProxyAndRecordSettings : HttpClientSettings
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public string[]? ExcludedCookies { get; set; }
|
public string[]? ExcludedCookies { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Replace Settings
|
||||||
|
/// </summary>
|
||||||
|
[PublicAPI]
|
||||||
|
public ProxyUrlReplaceSettings? ReplaceSettings { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Prefer the Proxy Mapping over the saved Mapping (in case SaveMapping is set to <c>true</c>).
|
/// Prefer the Proxy Mapping over the saved Mapping (in case SaveMapping is set to <c>true</c>).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
17
src/WireMock.Net/Settings/ProxyUrlReplaceSettings.cs
Normal file
17
src/WireMock.Net/Settings/ProxyUrlReplaceSettings.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
namespace WireMock.Settings;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Defines an old path param and a new path param to be replaced when proxying.
|
||||||
|
/// </summary>
|
||||||
|
public class ProxyUrlReplaceSettings
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The old path value to be replaced by the new path value
|
||||||
|
/// </summary>
|
||||||
|
public string OldValue { get; set; } = null!;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The new path value to replace the old value with
|
||||||
|
/// </summary>
|
||||||
|
public string NewValue { get; set; } = null!;
|
||||||
|
}
|
||||||
@@ -21,7 +21,8 @@ public static class WireMockServerSettingsParser
|
|||||||
/// <param name="logger">The logger (optional, can be null)</param>
|
/// <param name="logger">The logger (optional, can be null)</param>
|
||||||
/// <param name="settings">The parsed settings</param>
|
/// <param name="settings">The parsed settings</param>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public static bool TryParseArguments(string[] args, [NotNullWhen(true)] out WireMockServerSettings? settings, IWireMockLogger? logger = null)
|
public static bool TryParseArguments(string[] args, [NotNullWhen(true)] out WireMockServerSettings? settings,
|
||||||
|
IWireMockLogger? logger = null)
|
||||||
{
|
{
|
||||||
Guard.HasNoNulls(args);
|
Guard.HasNoNulls(args);
|
||||||
|
|
||||||
@@ -70,6 +71,17 @@ public static class WireMockServerSettingsParser
|
|||||||
settings.AcceptAnyClientCertificate = parser.GetBoolValue(nameof(WireMockServerSettings.AcceptAnyClientCertificate));
|
settings.AcceptAnyClientCertificate = parser.GetBoolValue(nameof(WireMockServerSettings.AcceptAnyClientCertificate));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
ParseLoggerSettings(settings, logger, parser);
|
||||||
|
ParsePortSettings(settings, parser);
|
||||||
|
ParseProxyAndRecordSettings(settings, parser);
|
||||||
|
ParseCertificateSettings(settings, parser);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ParseLoggerSettings(WireMockServerSettings settings, IWireMockLogger? logger,
|
||||||
|
SimpleCommandLineParser parser)
|
||||||
|
{
|
||||||
var loggerType = parser.GetStringValue("WireMockLogger");
|
var loggerType = parser.GetStringValue("WireMockLogger");
|
||||||
switch (loggerType)
|
switch (loggerType)
|
||||||
{
|
{
|
||||||
@@ -86,22 +98,17 @@ public static class WireMockServerSettingsParser
|
|||||||
{
|
{
|
||||||
settings.Logger = logger;
|
settings.Logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (parser.Contains(nameof(WireMockServerSettings.Port)))
|
private static void ParseProxyAndRecordSettings(WireMockServerSettings settings, SimpleCommandLineParser parser)
|
||||||
{
|
{
|
||||||
settings.Port = parser.GetIntValue(nameof(WireMockServerSettings.Port));
|
|
||||||
}
|
|
||||||
else if (settings.HostingScheme is null)
|
|
||||||
{
|
|
||||||
settings.Urls = parser.GetValues("Urls", new[] { "http://*:9091/" });
|
|
||||||
}
|
|
||||||
|
|
||||||
var proxyUrl = parser.GetStringValue("ProxyURL") ?? parser.GetStringValue("ProxyUrl");
|
var proxyUrl = parser.GetStringValue("ProxyURL") ?? parser.GetStringValue("ProxyUrl");
|
||||||
if (!string.IsNullOrEmpty(proxyUrl))
|
if (!string.IsNullOrEmpty(proxyUrl))
|
||||||
{
|
{
|
||||||
settings.ProxyAndRecordSettings = new ProxyAndRecordSettings
|
var proxyAndRecordSettings = new ProxyAndRecordSettings
|
||||||
{
|
{
|
||||||
AllowAutoRedirect = parser.GetBoolValue("AllowAutoRedirect"),
|
AllowAutoRedirect = parser.GetBoolValue("AllowAutoRedirect"),
|
||||||
ClientX509Certificate2ThumbprintOrSubjectName = parser.GetStringValue("ClientX509Certificate2ThumbprintOrSubjectName"),
|
ClientX509Certificate2ThumbprintOrSubjectName = parser.GetStringValue("ClientX509Certificate2ThumbprintOrSubjectName"),
|
||||||
@@ -121,18 +128,27 @@ public static class WireMockServerSettingsParser
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
string? proxyAddress = parser.GetStringValue("WebProxyAddress");
|
ParseWebProxyAddressSettings(proxyAndRecordSettings, parser);
|
||||||
if (!string.IsNullOrEmpty(proxyAddress))
|
ParseProxyUrlReplaceSettings(proxyAndRecordSettings, parser);
|
||||||
{
|
|
||||||
settings.ProxyAndRecordSettings.WebProxySettings = new WebProxySettings
|
|
||||||
{
|
|
||||||
Address = proxyAddress!,
|
|
||||||
UserName = parser.GetStringValue("WebProxyUserName"),
|
|
||||||
Password = parser.GetStringValue("WebProxyPassword")
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
settings.ProxyAndRecordSettings = proxyAndRecordSettings;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ParsePortSettings(WireMockServerSettings settings, SimpleCommandLineParser parser)
|
||||||
|
{
|
||||||
|
if (parser.Contains(nameof(WireMockServerSettings.Port)))
|
||||||
|
{
|
||||||
|
settings.Port = parser.GetIntValue(nameof(WireMockServerSettings.Port));
|
||||||
|
}
|
||||||
|
else if (settings.HostingScheme is null)
|
||||||
|
{
|
||||||
|
settings.Urls = parser.GetValues("Urls", new[] { "http://*:9091/" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ParseCertificateSettings(WireMockServerSettings settings, SimpleCommandLineParser parser)
|
||||||
|
{
|
||||||
var certificateSettings = new WireMockCertificateSettings
|
var certificateSettings = new WireMockCertificateSettings
|
||||||
{
|
{
|
||||||
X509StoreName = parser.GetStringValue("X509StoreName"),
|
X509StoreName = parser.GetStringValue("X509StoreName"),
|
||||||
@@ -145,7 +161,33 @@ public static class WireMockServerSettingsParser
|
|||||||
{
|
{
|
||||||
settings.CertificateSettings = certificateSettings;
|
settings.CertificateSettings = certificateSettings;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
private static void ParseWebProxyAddressSettings(ProxyAndRecordSettings settings, SimpleCommandLineParser parser)
|
||||||
|
{
|
||||||
|
string? proxyAddress = parser.GetStringValue("WebProxyAddress");
|
||||||
|
if (!string.IsNullOrEmpty(proxyAddress))
|
||||||
|
{
|
||||||
|
settings.WebProxySettings = new WebProxySettings
|
||||||
|
{
|
||||||
|
Address = proxyAddress!,
|
||||||
|
UserName = parser.GetStringValue("WebProxyUserName"),
|
||||||
|
Password = parser.GetStringValue("WebProxyPassword")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ParseProxyUrlReplaceSettings(ProxyAndRecordSettings settings, SimpleCommandLineParser parser)
|
||||||
|
{
|
||||||
|
var proxyUrlReplaceOldValue = parser.GetStringValue("ProxyUrlReplaceOldValue");
|
||||||
|
var proxyUrlReplaceNewValue = parser.GetStringValue("ProxyUrlReplaceNewValue");
|
||||||
|
if (!string.IsNullOrEmpty(proxyUrlReplaceOldValue) && proxyUrlReplaceNewValue != null)
|
||||||
|
{
|
||||||
|
settings.ReplaceSettings = new ProxyUrlReplaceSettings
|
||||||
|
{
|
||||||
|
OldValue = proxyUrlReplaceOldValue!,
|
||||||
|
NewValue = proxyUrlReplaceNewValue!
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,9 +12,11 @@ internal sealed class TinyMapperUtils
|
|||||||
{
|
{
|
||||||
TinyMapper.Bind<ProxyAndRecordSettings, ProxyAndRecordSettingsModel>();
|
TinyMapper.Bind<ProxyAndRecordSettings, ProxyAndRecordSettingsModel>();
|
||||||
TinyMapper.Bind<WebProxySettings, WebProxySettingsModel>();
|
TinyMapper.Bind<WebProxySettings, WebProxySettingsModel>();
|
||||||
|
TinyMapper.Bind<ProxyUrlReplaceSettings, ProxyUrlReplaceSettingsModel>();
|
||||||
|
|
||||||
TinyMapper.Bind<ProxyAndRecordSettingsModel, ProxyAndRecordSettings>();
|
TinyMapper.Bind<ProxyAndRecordSettingsModel, ProxyAndRecordSettings>();
|
||||||
TinyMapper.Bind<WebProxySettingsModel, WebProxySettings>();
|
TinyMapper.Bind<WebProxySettingsModel, WebProxySettings>();
|
||||||
|
TinyMapper.Bind<ProxyUrlReplaceSettingsModel, ProxyUrlReplaceSettings>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProxyAndRecordSettingsModel? Map(ProxyAndRecordSettings? instance)
|
public ProxyAndRecordSettingsModel? Map(ProxyAndRecordSettings? instance)
|
||||||
|
|||||||
@@ -570,6 +570,52 @@ public class WireMockServerProxyTests
|
|||||||
Check.That(matchers).Contains("name");
|
Check.That(matchers).Contains("name");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task WireMockServer_Proxy_Should_replace_old_path_value_with_new_path_value_in_replace_settings()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var replaceSettings = new ProxyUrlReplaceSettings
|
||||||
|
{
|
||||||
|
OldValue = "value-to-replace",
|
||||||
|
NewValue = "new-value"
|
||||||
|
};
|
||||||
|
string path = $"/prx_{Guid.NewGuid()}";
|
||||||
|
var serverForProxyForwarding = WireMockServer.Start();
|
||||||
|
serverForProxyForwarding
|
||||||
|
.Given(Request.Create().WithPath($"/{replaceSettings.NewValue}{path}"))
|
||||||
|
.RespondWith(Response.Create());
|
||||||
|
|
||||||
|
var settings = new WireMockServerSettings
|
||||||
|
{
|
||||||
|
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||||
|
{
|
||||||
|
Url = serverForProxyForwarding.Urls[0],
|
||||||
|
SaveMapping = true,
|
||||||
|
SaveMappingToFile = false,
|
||||||
|
ReplaceSettings = replaceSettings
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var server = WireMockServer.Start(settings);
|
||||||
|
var defaultMapping = server.Mappings.First();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var requestMessage = new HttpRequestMessage
|
||||||
|
{
|
||||||
|
Method = HttpMethod.Post,
|
||||||
|
RequestUri = new Uri($"{server.Urls[0]}/{replaceSettings.OldValue}{path}"),
|
||||||
|
Content = new StringContent("stringContent")
|
||||||
|
};
|
||||||
|
|
||||||
|
var handler = new HttpClientHandler();
|
||||||
|
await new HttpClient(handler).SendAsync(requestMessage).ConfigureAwait(false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var mapping = serverForProxyForwarding.Mappings.FirstOrDefault(m => m.Guid != defaultMapping.Guid);
|
||||||
|
var score = mapping.RequestMatcher.GetMatchingScore(serverForProxyForwarding.LogEntries.First().RequestMessage,
|
||||||
|
new RequestMatchResult());
|
||||||
|
Check.That(score).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_request_with_empty_content()
|
public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_request_with_empty_content()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user