import 'package:angel_framework/angel_framework.dart'; import 'package:angel_security/angel_security.dart'; import 'package:angel_test/angel_test.dart'; import 'package:test/test.dart'; main() { Angel app; TestClient client; setUp(() async { app = Angel(); app.chain([throttleRequests(1, Duration(hours: 1))]).get( '/once-per-hour', (req, res) => 'OK'); app.chain([throttleRequests(3, Duration(minutes: 1))]).get( '/thrice-per-minute', (req, res) => 'OK'); client = await connectTo(app); }); tearDown(() => client.close()); test('once per hour', () async { // First request within the hour is fine var response = await client.get('/once-per-hour', headers: { 'accept': 'application/json', }); print(response.body); expect(response, hasBody('"OK"')); // Second request within an hour? No no no! response = await client.get('/once-per-hour', headers: { 'accept': 'application/json', }); print(response.body); expect(response, isAngelHttpException(statusCode: 429)); }); test('thrice per minute', () async { // First request within the minute is fine var response = await client.get('/thrice-per-minute'); print(response.body); expect(response, hasBody('"OK"')); // Second request within the minute is fine response = await client.get('/thrice-per-minute'); print(response.body); expect(response, hasBody('"OK"')); // Third request within the minute is fine response = await client.get('/thrice-per-minute'); print(response.body); expect(response, hasBody('"OK"')); // Fourth request within a minute? No no no! response = await client.get('/thrice-per-minute', headers: { 'accept': 'application/json', }); print(response.body); expect(response, isAngelHttpException(statusCode: 429)); }); }