How to limit one client's API requests and test HTTP 429
Rate limiting needs to be tested from two sides: a client that exhausts its quota is denied, while another retains access within its own quota. Seeing a single HTTP 429 response is not enough. You need a clear contract: which operation is limited, who counts as a client, and which requests are allowed. We will use an entirely fictional example with two test users we own and synthetic data. All numbers below are conditions of the training environment, not recommendations for every API.
Define the rate limit contract first
OWASP recommends limiting incoming request rates by IP address, user, or both, and returning HTTP 429 Too Many Requests when requests arrive too frequently. This requires a mechanism to track each client's request rate. The developer's practical task is to turn that general recommendation into testable rules for one API operation.
In our fictional contract, GET /demo/items allows each user no more than three requests per fixed 60-second window. The first three requests return HTTP 200 and the expected synthetic data; the fourth receives HTTP 429. The quota resets in the next window. These parameters were chosen solely to keep the test short and clear.
Choose a trusted client identity
Authentication for the two test clients we own, A and B, is an established precondition. In this example, a client is a user: the server takes the user identifier from a trusted authentication result. The counter key combines this identifier with the selected operation, GET /demo/items. This explicitly ties the quota to a particular user and API operation.
The trust boundary lies between data supplied by the client and the server's decision about whose counter to use. For this training implementation, do not select the quota owner using an arbitrary request field. Both test clients will share one IP address, allowing us to test the chosen per-user accounting. Limiting by IP address is a valid separate choice, but it does not match the conditions of this example.
Check that the implementation counts incoming requests
If you need to limit the number of requests, use a mechanism that counts the client's incoming HTTP requests. Apache mod_ratelimit is unsuitable for this task: it limits the bandwidth of each response, rather than the number of incoming requests or a client's total traffic.
Before testing, record the expected behavior for allowed and rejected requests. Under our contract, an allowed request returns HTTP 200 with a specified set of synthetic items. Exceeding the quota must produce an actual HTTP 429 status and a generic message, such as: ‘Too many requests. Try again later.’ The response should not include internal failure details.
Start with a positive control
The following are original proposed acceptance tests, not an official OWASP procedure. Run them in your own test environment, with clients you own and synthetic data. Start with clean counters and controlled time so that the entire sequence is guaranteed to stay within one fixed window.
Send one request each from A and B to GET /demo/items. Both should receive HTTP 200 and exactly the synthetic items specified for the test. Record the observed statuses and response contents. A successful HTTP response alone does not replace checking the result: in this example, the operation reads data, so its contents are checked; no state change is specified.
Exhaust A's quota and check B's access
After the positive control, send two more requests from A within the same window. These are the user's second and third requests: expect HTTP 200 and the correct data. Then send A's fourth request. The rejection criterion is HTTP 429 as the response status, not merely rate limit text inside a successful response. The message should remain generic.
Immediately after A is rejected, send B's second request to the same operation. Expect HTTP 200 and the synthetic data: B has not yet exhausted its quota. Both clients are still using the same test IP address. Then clear the counters and repeat the scenario with A and B swapping roles. This ensures the result does not depend solely on one selected user.
Test the new window and fix discrepancies
After exhausting A's quota, advance the test environment's controlled time into the next fixed window. A new request from A should return HTTP 200 with the expected data. This reset is part of our contract; do not apply that expectation to another rate limiting algorithm without a separate specification.
If the test fails, first check the server-side identifier and counter key: do they correspond to the user and operation? Then check the accounting for the three allowed requests, rejection of the fourth, and the window boundary. If A's rejection is accompanied by rejection of B's second request, investigate whether the quotas are being mixed. After fixing the issue, repeat the entire scenario with clean counters, including the positive control and role swap.
What this test confirms
A successful result confirms observed compliance with the training quota, HTTP 429 responses, and isolation between the two clients under the specified conditions. It does not prove that the service can withstand denial of service. In addition, a simple HTTP request count does not account for expensive GraphQL queries or multiple operations within one request; assessing those mechanisms is outside the scope of this article.
The practical outcome is a documented contract and a reproducible test covering allowed requests, quota exhaustion, access for a second client, and a new window. If you want to review rate limiting in your own web product, you can start with a free audit of your website or its web component.