Skip to content

Commit

Permalink
WireMock-Net#912 add excluded params to proxy mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
walidhaidarii committed Mar 23, 2023
1 parent 651486f commit 4ca97a9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/WireMock.Net/Serialization/ProxyMappingConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public ProxyMappingConverter(WireMockServerSettings settings, IGuidUtils guidUti
var useDefinedRequestMatchers = proxyAndRecordSettings.UseDefinedRequestMatchers;
var excludedHeaders = new List<string>(proxyAndRecordSettings.ExcludedHeaders ?? new string[] { }) { "Cookie" };
var excludedCookies = proxyAndRecordSettings.ExcludedCookies ?? new string[0];
var excludedParams = proxyAndRecordSettings.ExcludedParams ?? new string[0];

var request = (Request?)mapping?.RequestMatcher;
var clientIPMatcher = request?.GetRequestMessageMatcher<RequestMessageClientIPMatcher>();
Expand Down Expand Up @@ -74,12 +75,21 @@ public ProxyMappingConverter(WireMockServerSettings settings, IGuidUtils guidUti
{
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
{
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
Expand Down
6 changes: 6 additions & 0 deletions src/WireMock.Net/Settings/ProxyAndRecordSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public string SaveMappingForStatusCodePattern
[PublicAPI]
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>
/// Defines a list of cookies which will be excluded from the saved mappings.
/// </summary>
Expand Down
42 changes: 42 additions & 0 deletions test/WireMock.Net.Tests/WireMockServer.Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,48 @@ public async Task WireMockServer_Proxy_Should_exclude_ExcludedCookies_in_mapping
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]
public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_request_with_empty_content()
{
Expand Down

0 comments on commit 4ca97a9

Please sign in to comment.