30API TestingAPI TestingPerformance
What is API Rate Limiting and how do you test it?
Rate Limiting is a technique used to control the number of requests a client can make to an API within a specific time period, preventing abuse and ensuring fair usage.
Why Rate Limiting is Important
- Prevents API abuse and DDoS attacks
- Ensures fair resource distribution
- Protects server from overload
- Manages costs for cloud-based APIs
- Maintains quality of service for all users
Common Rate Limiting Strategies
- Fixed Window: X requests per minute/hour
- Sliding Window: Rolling time window
- Token Bucket: Tokens replenish over time
- Leaky Bucket: Requests processed at constant rate
Rate Limit Response Headers
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 75
X-RateLimit-Reset: 1640995200
Retry-After: 60
Testing Rate Limiting
- Test Within Limits: Verify API works normally
- Test Exceeding Limits: Send requests beyond limit
- Verify Status Code: Should return 429 (Too Many Requests)
- Check Headers: Validate rate limit headers
- Test Reset: Verify limits reset after time window
- Test Different Users: Ensure limits are per-user/per-key
Example Test Scenario
// Send 100 requests (within limit)
for (let i = 0; i < 100; i++) {
// Should return 200 OK
}
// Send 101st request (exceeds limit)
// Should return 429 Too Many Requests
// Response should include Retry-After header