Skip to content
Open
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
2 changes: 1 addition & 1 deletion DevProxy.Abstractions/DevProxy.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<RootNamespace>DevProxy.Abstractions</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>4.0.0</Version>
<Version>4.1.0</Version>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
6 changes: 6 additions & 0 deletions DevProxy.Abstractions/Models/MockResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public class MockResponse : ICloneable
{
public MockResponseRequest? Request { get; set; }
public MockResponseResponse? Response { get; set; }
/// <summary>
/// Probability (0-100) that the mock is applied when it matches a request.
/// When not set, the mock is always applied. When the mock isn't applied,
/// the request falls through to the next matching mock or the real API.
/// </summary>
public int? Rate { get; set; }

public object Clone()
{
Expand Down
165 changes: 165 additions & 0 deletions DevProxy.Integration.Tests/ManipulationAndMockingIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,171 @@ public async Task MockResponse_Nth_DistinctMatchersHaveIndependentCounts(
Assert.Equal(HttpStatusCode.Accepted, third.StatusCode);
}

[Theory]
[InlineData(null)]
[InlineData("100")]
[InlineData("-5")]
[InlineData("150")]
public async Task MockResponse_Rate_AlwaysAppliesWhenAbsentFullOrInvalid(string? rate)
{
await using var origin = await FakeOrigin.StartAsync();
var urls = KestrelProxyHarness.BuildUrlsToWatch(origin.Host);
var config = PluginConfig.FromJson($$"""
{
"mocks": [
{
"request": { "url": "http://{{origin.Host}}/get", "method": "GET" },
"response": { "statusCode": 201 },
"rate": {{rate ?? "null"}}
}
]
}
""");
using var plugin = new MockResponsePlugin(
SharedHttpClient, NullLogger<MockResponsePlugin>.Instance, urls, ProxyConfig, config);
await using var proxy = await KestrelProxyHarness.StartAsync(origin.Host, [plugin]);
using var client = proxy.CreateHttpClient();
var uri = new Uri($"http://{origin.Host}/get");

for (var i = 0; i < 10; i++)
{
using var response = await client.GetAsync(uri);
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
}
}

[Fact]
public async Task MockResponse_Rate0_PassesThroughToOrigin()
{
await using var origin = await FakeOrigin.StartAsync();
var urls = KestrelProxyHarness.BuildUrlsToWatch(origin.Host);
var config = PluginConfig.FromJson($$"""
{
"mocks": [
{
"request": { "url": "http://{{origin.Host}}/get", "method": "GET" },
"response": { "statusCode": 503 },
"rate": 0
}
]
}
""");
using var plugin = new MockResponsePlugin(
SharedHttpClient, NullLogger<MockResponsePlugin>.Instance, urls, ProxyConfig, config);
await using var proxy = await KestrelProxyHarness.StartAsync(origin.Host, [plugin]);
using var client = proxy.CreateHttpClient();
var uri = new Uri($"http://{origin.Host}/get");

for (var i = 0; i < 10; i++)
{
using var response = await client.GetAsync(uri);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}

[Fact]
public async Task MockResponse_Rate0_FallsThroughToNextMatchingMock()
{
await using var origin = await FakeOrigin.StartAsync();
var urls = KestrelProxyHarness.BuildUrlsToWatch(origin.Host);
var config = PluginConfig.FromJson($$"""
{
"mocks": [
{
"request": { "url": "http://{{origin.Host}}/get", "method": "GET" },
"response": { "statusCode": 503 },
"rate": 0
},
{
"request": { "url": "http://{{origin.Host}}/get", "method": "GET" },
"response": { "statusCode": 202 }
}
]
}
""");
using var plugin = new MockResponsePlugin(
SharedHttpClient, NullLogger<MockResponsePlugin>.Instance, urls, ProxyConfig, config);
await using var proxy = await KestrelProxyHarness.StartAsync(origin.Host, [plugin]);
using var client = proxy.CreateHttpClient();
var uri = new Uri($"http://{origin.Host}/get");

for (var i = 0; i < 10; i++)
{
using var response = await client.GetAsync(uri);
Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);
}
}

[Fact]
public async Task MockResponse_Rate0_WithNth_IsNeverApplied()
{
await using var origin = await FakeOrigin.StartAsync();
var urls = KestrelProxyHarness.BuildUrlsToWatch(origin.Host);
var config = PluginConfig.FromJson($$"""
{
"mocks": [
{
"request": { "url": "http://{{origin.Host}}/get", "method": "GET", "nth": 2 },
"response": { "statusCode": 201 },
"rate": 0
},
{
"request": { "url": "http://{{origin.Host}}/get", "method": "GET" },
"response": { "statusCode": 202 }
}
]
}
""");
using var plugin = new MockResponsePlugin(
SharedHttpClient, NullLogger<MockResponsePlugin>.Instance, urls, ProxyConfig, config);
await using var proxy = await KestrelProxyHarness.StartAsync(origin.Host, [plugin]);
using var client = proxy.CreateHttpClient();
var uri = new Uri($"http://{origin.Host}/get");

for (var i = 0; i < 3; i++)
{
using var response = await client.GetAsync(uri);
Assert.Equal(HttpStatusCode.Accepted, response.StatusCode);
}
}

[Fact]
public async Task MockResponse_PartialRate_AppliesSomeOfTheTime()
{
await using var origin = await FakeOrigin.StartAsync();
var urls = KestrelProxyHarness.BuildUrlsToWatch(origin.Host);
var config = PluginConfig.FromJson($$"""
{
"mocks": [
{
"request": { "url": "http://{{origin.Host}}/get", "method": "GET" },
"response": { "statusCode": 503 },
"rate": 50
},
{
"request": { "url": "http://{{origin.Host}}/get", "method": "GET" },
"response": { "statusCode": 202 }
}
]
}
""");
using var plugin = new MockResponsePlugin(
SharedHttpClient, NullLogger<MockResponsePlugin>.Instance, urls, ProxyConfig, config);
await using var proxy = await KestrelProxyHarness.StartAsync(origin.Host, [plugin]);
using var client = proxy.CreateHttpClient();
var uri = new Uri($"http://{origin.Host}/get");

var statusCodes = new HashSet<HttpStatusCode>();
// the chance of 100 rolls at 50% landing on the same side is ~2^-99
for (var i = 0; i < 100; i++)
{
using var response = await client.GetAsync(uri);
_ = statusCodes.Add(response.StatusCode);
}

Assert.Equal([HttpStatusCode.Accepted, HttpStatusCode.ServiceUnavailable], statusCodes.Order());
}

[Theory]
[InlineData("/Get")]
[InlineData("/Ge*")]
Expand Down
2 changes: 1 addition & 1 deletion DevProxy.Plugins/DevProxy.Plugins.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<EnableDynamicLoading>true</EnableDynamicLoading>
<RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
<Version>4.0.0</Version>
<Version>4.1.0</Version>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
30 changes: 27 additions & 3 deletions DevProxy.Plugins/Mocking/MockResponsePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public sealed class MockResponseConfiguration
[JsonIgnore]
public bool NoMocks { get; set; }
[JsonPropertyName("$schema")]
public string Schema { get; set; } = "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/mockresponseplugin.mocksfile.schema.json";
public string Schema { get; set; } = "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/mockresponseplugin.mocksfile.schema.json";
}

public class MockResponsePlugin(
Expand Down Expand Up @@ -308,8 +308,32 @@ Configuration.Mocks is null ||
}

return matchingMocks.FirstOrDefault(mockResponse =>
mockResponse.Request!.Nth is null ||
mockResponse.Request.Nth == requestCounts[GetMockKey(mockResponse)]);
(mockResponse.Request!.Nth is null ||
mockResponse.Request.Nth == requestCounts[GetMockKey(mockResponse)]) &&
ShouldApplyMock(mockResponse));
}

internal static bool IsValidRate(int rate) => rate is >= 0 and <= 100;

private bool ShouldApplyMock(MockResponse mockResponse)
{
if (mockResponse.Rate is not { } rate || !IsValidRate(rate))
{
return true;
}

var roll = Random.Shared.Next(1, 101);
if (roll <= rate)
{
return true;
}

Logger.LogDebug(
"Skipping mock for {Method} {Url} based on its rate of {Rate}%",
mockResponse.Request!.Method,
mockResponse.Request.Url,
rate);
return false;
}

private static MockRequestKey GetMockKey(MockResponse mockResponse)
Expand Down
9 changes: 9 additions & 0 deletions DevProxy.Plugins/Mocking/MockResponsesLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ protected override void LoadData(string fileContents)
{
Logger.LogInformation("Mock responses for {ConfigResponseCount} url patterns loaded from {MockFile}", configResponses.Count(), _configuration.MocksFile);
}

foreach (var mock in configResponses.Where(m => m.Rate is not null && !MockResponsePlugin.IsValidRate(m.Rate.Value)))
{
Logger.LogWarning(
"Mock for {Method} {Url} has an invalid rate {Rate}. Rate must be between 0 and 100. Ignoring rate and always applying the mock",
mock.Request?.Method,
mock.Request?.Url,
mock.Rate);
}
}
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion DevProxy.Plugins/Mocking/MockStdioResponsePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public sealed class MockStdioResponseConfiguration
public bool NoMocks { get; set; }

[JsonPropertyName("$schema")]
public string Schema { get; set; } = "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/mockstdioresponseplugin.mocksfile.schema.json";
public string Schema { get; set; } = "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/mockstdioresponseplugin.mocksfile.schema.json";
}

public class MockStdioResponsePlugin(
Expand Down
2 changes: 1 addition & 1 deletion DevProxy.Plugins/Mocking/WebSocketMockResponsePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed class WebSocketMockResponseConfiguration
public bool NoMocks { get; set; }

[JsonPropertyName("$schema")]
public string Schema { get; set; } = "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/websocketmockresponseplugin.mocksfile.schema.json";
public string Schema { get; set; } = "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/websocketmockresponseplugin.mocksfile.schema.json";
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion DevProxy.Proxy.Kestrel/DevProxy.Proxy.Kestrel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<RootNamespace>DevProxy.Proxy.Kestrel</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>4.0.0</Version>
<Version>4.1.0</Version>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
2 changes: 1 addition & 1 deletion DevProxy/DevProxy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Nullable>enable</Nullable>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Title>Dev Proxy</Title>
<Version>4.0.0</Version>
<Version>4.1.0</Version>
<Company>.NET Foundation</Company>
<Product>Dev Proxy</Product>
<AssemblyName>devproxy</AssemblyName>
Expand Down
2 changes: 1 addition & 1 deletion DevProxy/config-templates/devproxyrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rc.schema.json",
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/rc.schema.json",
"plugins": [
],
"urlsToWatch": [
Expand Down
2 changes: 1 addition & 1 deletion DevProxy/config-templates/devproxyrc.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rc.schema.json
# yaml-language-server: $schema=https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/rc.schema.json
plugins:
-
urlsToWatch:
Expand Down
2 changes: 1 addition & 1 deletion DevProxy/config/m365-mocks.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/mockresponseplugin.mocksfile.schema.json",
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/mockresponseplugin.mocksfile.schema.json",
"mocks": [
{
"request": {
Expand Down
18 changes: 9 additions & 9 deletions DevProxy/config/m365.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rc.schema.json",
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/rc.schema.json",
"plugins": [
{
"name": "DevToolsPlugin",
Expand Down Expand Up @@ -173,11 +173,11 @@
"https://*.sharepoint-df.*/*_vti_bin/*"
],
"mocksPlugin": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/mockresponseplugin.schema.json",
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/mockresponseplugin.schema.json",
"mocksFile": "m365-mocks.json"
},
"graphRandomErrorsPlugin": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/graphrandomerrorplugin.schema.json",
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/graphrandomerrorplugin.schema.json",
"allowedErrors": [
429,
500,
Expand All @@ -189,28 +189,28 @@
"rate": 50
},
"executionSummaryPlugin": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/executionsummaryplugin.schema.json",
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/executionsummaryplugin.schema.json",
"groupBy": "url"
},
"graphMinimalPermissionsPlugin": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/graphminimalpermissionsplugin.schema.json",
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/graphminimalpermissionsplugin.schema.json",
"type": "delegated"
},
"cachingGuidance": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/cachingguidanceplugin.schema.json",
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/cachingguidanceplugin.schema.json",
"cacheThresholdSeconds": 5
},
"latencyPlugin": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/latencyplugin.schema.json",
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/latencyplugin.schema.json",
"minMs": 200,
"maxMs": 10000
},
"devTools": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/devtoolsplugin.schema.json",
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/devtoolsplugin.schema.json",
"preferredBrowser": "Edge"
},
"rateLimiting": {
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/ratelimitingplugin.schema.json",
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/ratelimitingplugin.schema.json",
"costPerRequest": 2,
"rateLimit": 120,
"resetTimeWindowSeconds": 5
Expand Down
2 changes: 1 addition & 1 deletion DevProxy/config/microsoft-graph-rate-limiting.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.0.0/rc.schema.json",
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v4.1.0/rc.schema.json",
"plugins": [
{
"name": "RateLimitingPlugin",
Expand Down
Loading
Loading