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

Proxy all requests - even a repeated one #1038

Merged
merged 7 commits into from
Dec 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,6 @@ internal interface IWireMockMiddlewareOptions
bool? DoNotSaveDynamicResponseInLogEntry { get; set; }

QueryParameterMultipleValueSupport? QueryParameterMultipleValueSupport { get; set; }

public bool ProxyAll { get; set; }
}
3 changes: 3 additions & 0 deletions src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,7 @@ internal class WireMockMiddlewareOptions : IWireMockMiddlewareOptions

/// <inheritdoc />
public QueryParameterMultipleValueSupport? QueryParameterMultipleValueSupport { get; set; }

/// <inheritdoc />
public bool ProxyAll { get; set; }
}
5 changes: 5 additions & 0 deletions src/WireMock.Net/Server/WireMockServer.Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ private void InitProxyAndRecord(WireMockServerSettings settings)
proxyRespondProvider.AtPriority(WireMockConstants.ProxyPriority);
}

if(settings.ProxyAndRecordSettings.ProxyAll)
{
proxyRespondProvider.AtPriority(int.MinValue);
}

proxyRespondProvider.RespondWith(new ProxyAsyncResponseProvider(ProxyAndRecordAsync, settings));
}

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 @@ -94,4 +94,10 @@ public string SaveMappingForStatusCodePattern
/// 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;
}
3 changes: 2 additions & 1 deletion src/WireMock.Net/Settings/WireMockServerSettingsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ private static void ParseProxyAndRecordSettings(WireMockServerSettings settings,
{
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);
Expand Down
45 changes: 45 additions & 0 deletions test/WireMock.Net.Tests/WireMockServer.Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -922,4 +922,49 @@ public async Task WireMockServer_Proxy_WhenTargetIsNotAvailable_Should_Return_Co
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();
}
}