-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathContentRateLimitingTests.cs
More file actions
30 lines (26 loc) · 1.19 KB
/
ContentRateLimitingTests.cs
File metadata and controls
30 lines (26 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System.Net;
namespace EssentialCSharp.Web.Tests;
/// <summary>
/// HTTP integration tests for the "content" rate limit policy.
/// Each test gets its own factory (fresh IHost) so the rate limiter starts from a clean state.
/// Anonymous users are limited to 10 requests per minute on chapter content pages.
/// </summary>
public class ContentRateLimitingTests : IntegrationTestBase
{
[Test]
public async Task ContentEndpoint_ExceedingPerMinuteLimit_Returns429()
{
HttpClient client = Factory.CreateClient();
// Anonymous limit is 10/min. First 10 requests should not be rate-limited.
for (int i = 0; i < 10; i++)
{
using HttpResponseMessage response = await client.GetAsync("/hello-world");
await Assert.That(response.StatusCode)
.IsNotEqualTo(HttpStatusCode.TooManyRequests)
.Because($"request {i + 1} of 10 should be within the rate limit");
}
// 11th request must be rejected by the content rate limiter.
using HttpResponseMessage rateLimited = await client.GetAsync("/hello-world");
await Assert.That(rateLimited.StatusCode).IsEqualTo(HttpStatusCode.TooManyRequests);
}
}