|
| 1 | +using System.Threading.RateLimiting; |
| 2 | +using Microsoft.AspNetCore.Builder; |
| 3 | +using Microsoft.AspNetCore.RateLimiting; |
| 4 | +using Microsoft.AspNetCore.Hosting; |
| 5 | +using Microsoft.AspNetCore.Http; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Microsoft.Extensions.Logging; |
| 8 | +using NpgsqlRest.Mcp; |
| 9 | + |
| 10 | +namespace NpgsqlRestTests.Setup; |
| 11 | + |
| 12 | +[CollectionDefinition("McpRateLimiterFixture")] |
| 13 | +public class McpRateLimiterFixtureCollection : ICollectionFixture<McpRateLimiterTestFixture> { } |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Fixture proving <see cref="McpOptions.RateLimiterPolicy"/> is applied to the whole <c>/mcp</c> endpoint. |
| 17 | +/// The host registers a fixed-window policy that allows a single request per (long) window; the second |
| 18 | +/// request to <c>/mcp</c> must be rejected by the rate limiter with 429. |
| 19 | +/// </summary> |
| 20 | +public class McpRateLimiterTestFixture : IDisposable |
| 21 | +{ |
| 22 | + public const string PolicyName = "mcp_test_policy"; |
| 23 | + |
| 24 | + private readonly WebApplication _app; |
| 25 | + private readonly HttpClient _client; |
| 26 | + |
| 27 | + public HttpClient Client => _client; |
| 28 | + |
| 29 | + public McpRateLimiterTestFixture() |
| 30 | + { |
| 31 | + Database.Create(); |
| 32 | + var connectionString = Database.CreateAdditional("mcp_rate_limiter_test"); |
| 33 | + |
| 34 | + var builder = WebApplication.CreateBuilder(); |
| 35 | + builder.WebHost.UseUrls("http://127.0.0.1:0"); |
| 36 | + builder.Logging.ClearProviders(); |
| 37 | + |
| 38 | + // One permit per (long) window, no queue → the second request inside the window is rejected. |
| 39 | + builder.Services.AddRateLimiter(o => |
| 40 | + { |
| 41 | + o.RejectionStatusCode = StatusCodes.Status429TooManyRequests; |
| 42 | + o.AddFixedWindowLimiter(PolicyName, opt => |
| 43 | + { |
| 44 | + opt.PermitLimit = 1; |
| 45 | + opt.Window = TimeSpan.FromMinutes(10); |
| 46 | + opt.QueueLimit = 0; |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + _app = builder.Build(); |
| 51 | + _app.UseRateLimiter(); |
| 52 | + |
| 53 | + _app.UseNpgsqlRest(new NpgsqlRestOptions(connectionString) |
| 54 | + { |
| 55 | + IncludeSchemas = ["mcp"], |
| 56 | + CommentsMode = CommentsMode.OnlyAnnotated, |
| 57 | + EndpointCreateHandlers = |
| 58 | + [ |
| 59 | + new Mcp(new McpOptions |
| 60 | + { |
| 61 | + Enabled = true, |
| 62 | + RateLimiterPolicy = PolicyName, |
| 63 | + }) |
| 64 | + ] |
| 65 | + }); |
| 66 | + |
| 67 | + _app.StartAsync().GetAwaiter().GetResult(); |
| 68 | + _client = new HttpClient { BaseAddress = new Uri(_app.Urls.First()) }; |
| 69 | + _client.Timeout = TimeSpan.FromHours(1); |
| 70 | + } |
| 71 | + |
| 72 | +#pragma warning disable CA1816 // Dispose methods should call SuppressFinalize |
| 73 | + public void Dispose() |
| 74 | +#pragma warning restore CA1816 // Dispose methods should call SuppressFinalize |
| 75 | + { |
| 76 | + _client.Dispose(); |
| 77 | + _app.StopAsync().GetAwaiter().GetResult(); |
| 78 | + _app.DisposeAsync().GetAwaiter().GetResult(); |
| 79 | + } |
| 80 | +} |
0 commit comments