mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
Proxy all requests - even a repeated one (#1038)
* Adding proxyall * Adding proxyAll flag * Reverting extra spaces added * Make proxyall to true * Resolving review comments for proxyall * Resolving codefactor spaces in the code * Adding proxyall parser in wmserversettingsparser --------- Co-authored-by: Sameena Syed <sameena.syed@nexigroup.com>
This commit is contained in:
@@ -73,4 +73,9 @@ public class ProxyAndRecordSettingsModel
|
||||
/// Defines the Replace Settings.
|
||||
/// </summary>
|
||||
public ProxyUrlReplaceSettingsModel? ReplaceSettings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Proxy all Api calls, irrespective of any condition
|
||||
/// </summary>
|
||||
public bool ProxyAll { get; set; } = false;
|
||||
}
|
||||
@@ -77,4 +77,6 @@ internal interface IWireMockMiddlewareOptions
|
||||
bool? DoNotSaveDynamicResponseInLogEntry { get; set; }
|
||||
|
||||
QueryParameterMultipleValueSupport? QueryParameterMultipleValueSupport { get; set; }
|
||||
|
||||
public bool ProxyAll { get; set; }
|
||||
}
|
||||
@@ -95,4 +95,7 @@ internal class WireMockMiddlewareOptions : IWireMockMiddlewareOptions
|
||||
|
||||
/// <inheritdoc />
|
||||
public QueryParameterMultipleValueSupport? QueryParameterMultipleValueSupport { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool ProxyAll { get; set; }
|
||||
}
|
||||
@@ -31,6 +31,11 @@ public partial class WireMockServer
|
||||
proxyRespondProvider.AtPriority(WireMockConstants.ProxyPriority);
|
||||
}
|
||||
|
||||
if(settings.ProxyAndRecordSettings.ProxyAll)
|
||||
{
|
||||
proxyRespondProvider.AtPriority(int.MinValue);
|
||||
}
|
||||
|
||||
proxyRespondProvider.RespondWith(new ProxyAsyncResponseProvider(ProxyAndRecordAsync, settings));
|
||||
}
|
||||
|
||||
|
||||
@@ -94,4 +94,10 @@ public class ProxyAndRecordSettings : HttpClientSettings
|
||||
/// Append an unique GUID to the filename from the saved mapping file.
|
||||
/// </summary>
|
||||
public bool AppendGuidToSavedMappingFile { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Proxy all Api calls, irrespective of any condition
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public bool ProxyAll { get; set; } = false;
|
||||
}
|
||||
@@ -125,7 +125,8 @@ public static class WireMockServerSettingsParser
|
||||
{
|
||||
StatusCodePattern = parser.GetStringValue("SaveMappingForStatusCodePattern", "*"),
|
||||
// HttpMethods = new ProxySaveMappingSetting<string[]>(parser.GetValues("DoNotSaveMappingForHttpMethods", new string[0]), MatchBehaviour.RejectOnMatch)
|
||||
}
|
||||
},
|
||||
ProxyAll = parser.GetBoolValue(nameof(ProxyAndRecordSettings.ProxyAll))
|
||||
};
|
||||
|
||||
ParseWebProxyAddressSettings(proxyAndRecordSettings, parser);
|
||||
|
||||
@@ -922,4 +922,49 @@ public class WireMockServerProxyTests
|
||||
server.LogEntries.Should().HaveCount(1);
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_ProxyAndRecordSettings_SameRequest_ShouldProxyAll()
|
||||
{
|
||||
//Arrange
|
||||
var wireMockServerSettings = new WireMockServerSettings
|
||||
{
|
||||
Urls = new[] { "http://localhost:9091" },
|
||||
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = "http://postman-echo.com",
|
||||
SaveMapping = true,
|
||||
ProxyAll = true,
|
||||
SaveMappingToFile = false,
|
||||
ExcludedHeaders = new[] { "Postman-Token" },
|
||||
ExcludedCookies = new[] { "sails.sid" }
|
||||
}
|
||||
};
|
||||
|
||||
var server = WireMockServer.Start(wireMockServerSettings);
|
||||
|
||||
var requestBody = "{\"key1\": \"value1\", \"key2\": \"value2\"}";
|
||||
var request = new HttpRequestMessage
|
||||
{
|
||||
Method = HttpMethod.Post,
|
||||
RequestUri = new Uri("http://localhost:9091/post"),
|
||||
Content = new StringContent(requestBody)
|
||||
};
|
||||
var request2 = new HttpRequestMessage
|
||||
{
|
||||
Method = HttpMethod.Post,
|
||||
RequestUri = new Uri("http://localhost:9091/post"),
|
||||
Content = new StringContent(requestBody)
|
||||
};
|
||||
server.ResetMappings();
|
||||
|
||||
//Act
|
||||
await new HttpClient().SendAsync(request);
|
||||
await new HttpClient().SendAsync(request2);
|
||||
|
||||
//Assert
|
||||
Check.That(server.Mappings.Count()).IsEqualTo(3);
|
||||
|
||||
server.Dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user