#912 add excluded params to proxy mapping (#914)

This commit is contained in:
Walid Haidari
2023-03-24 23:35:09 +08:00
committed by GitHub
parent 090989ea7f
commit 95bf8e31aa
3 changed files with 60 additions and 2 deletions

View File

@@ -31,6 +31,7 @@ internal class ProxyMappingConverter
var useDefinedRequestMatchers = proxyAndRecordSettings.UseDefinedRequestMatchers; var useDefinedRequestMatchers = proxyAndRecordSettings.UseDefinedRequestMatchers;
var excludedHeaders = new List<string>(proxyAndRecordSettings.ExcludedHeaders ?? new string[] { }) { "Cookie" }; var excludedHeaders = new List<string>(proxyAndRecordSettings.ExcludedHeaders ?? new string[] { }) { "Cookie" };
var excludedCookies = proxyAndRecordSettings.ExcludedCookies ?? new string[0]; var excludedCookies = proxyAndRecordSettings.ExcludedCookies ?? new string[0];
var excludedParams = proxyAndRecordSettings.ExcludedParams ?? new string[0];
var request = (Request?)mapping?.RequestMatcher; var request = (Request?)mapping?.RequestMatcher;
var clientIPMatcher = request?.GetRequestMessageMatcher<RequestMessageClientIPMatcher>(); var clientIPMatcher = request?.GetRequestMessageMatcher<RequestMessageClientIPMatcher>();
@@ -74,12 +75,21 @@ internal class ProxyMappingConverter
{ {
foreach (var paramMatcher in paramMatchers) foreach (var paramMatcher in paramMatchers)
{ {
newRequest.WithParam(paramMatcher.Key, paramMatcher.MatchBehaviour, paramMatcher.Matchers!.ToArray()); if (!excludedParams.Contains(paramMatcher.Key, StringComparer.OrdinalIgnoreCase))
{
newRequest.WithParam(paramMatcher.Key, paramMatcher.MatchBehaviour, paramMatcher.Matchers!.ToArray());
}
} }
} }
else else
{ {
requestMessage.Query?.Loop((key, value) => newRequest.WithParam(key, false, value.ToArray())); requestMessage.Query?.Loop((key, value) =>
{
if (!excludedParams.Contains(key, StringComparer.OrdinalIgnoreCase))
{
newRequest.WithParam(key, false, value.ToArray());
}
});
} }
// Cookies // Cookies

View File

@@ -57,6 +57,12 @@ public class ProxyAndRecordSettings : HttpClientSettings
[PublicAPI] [PublicAPI]
public string[]? ExcludedHeaders { get; set; } public string[]? ExcludedHeaders { get; set; }
/// <summary>
/// Defines a list of params which will be excluded from the saved mappings.
/// </summary>
[PublicAPI]
public string[]? ExcludedParams { get; set; }
/// <summary> /// <summary>
/// Defines a list of cookies which will be excluded from the saved mappings. /// Defines a list of cookies which will be excluded from the saved mappings.
/// </summary> /// </summary>

View File

@@ -528,6 +528,48 @@ public class WireMockServerProxyTests
Check.That(matchers).Contains("GoodCookie"); Check.That(matchers).Contains("GoodCookie");
} }
[Fact]
public async Task WireMockServer_Proxy_Should_exclude_ExcludedParams_in_mapping()
{
// Assign
string path = $"/prx_{Guid.NewGuid()}";
var serverForProxyForwarding = WireMockServer.Start();
serverForProxyForwarding
.Given(Request.Create().WithPath(path))
.RespondWith(Response.Create());
var settings = new WireMockServerSettings
{
ProxyAndRecordSettings = new ProxyAndRecordSettings
{
Url = serverForProxyForwarding.Urls[0],
SaveMapping = true,
SaveMappingToFile = false,
ExcludedParams = new[] { "timestamp" }
}
};
var server = WireMockServer.Start(settings);
var defaultMapping = server.Mappings.First();
var param01 = "?timestamp=2023-03-23";
var param02 = "&name=person";
// Act
var requestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri($"{server.Urls[0]}{path}{param01}{param02}"),
Content = new StringContent("stringContent"),
};
await new HttpClient().SendAsync(requestMessage).ConfigureAwait(false);
// Assert
var mapping = server.Mappings.FirstOrDefault(m => m.Guid != defaultMapping.Guid);
Check.That(mapping).IsNotNull();
var matchers = ((Request)mapping.RequestMatcher).GetRequestMessageMatchers<RequestMessageParamMatcher>().Select(m => m.Key).ToList();
Check.That(matchers).Not.Contains("timestamp");
Check.That(matchers).Contains("name");
}
[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()
{ {