2017-01-14 00:45:35 +00:00
|
|
|
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 {
|
2019-04-20 14:53:52 +00:00
|
|
|
app = Angel();
|
2017-01-14 00:45:35 +00:00
|
|
|
|
2019-04-20 14:53:52 +00:00
|
|
|
app.chain([throttleRequests(1, Duration(hours: 1))]).get(
|
|
|
|
'/once-per-hour', (req, res) => 'OK');
|
2017-01-14 00:45:35 +00:00
|
|
|
|
2019-04-20 14:53:52 +00:00
|
|
|
app.chain([throttleRequests(3, Duration(minutes: 1))]).get(
|
|
|
|
'/thrice-per-minute', (req, res) => 'OK');
|
2017-01-28 20:29:20 +00:00
|
|
|
|
2017-01-14 00:45:35 +00:00
|
|
|
client = await connectTo(app);
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() => client.close());
|
|
|
|
|
2017-01-28 20:29:20 +00:00
|
|
|
test('once per hour', () async {
|
2017-01-14 00:45:35 +00:00
|
|
|
// First request within the hour is fine
|
2017-12-22 13:39:21 +00:00
|
|
|
var response = await client.get('/once-per-hour', headers: {
|
|
|
|
'accept': 'application/json',
|
|
|
|
});
|
2017-01-14 00:45:35 +00:00
|
|
|
print(response.body);
|
2017-03-29 02:44:56 +00:00
|
|
|
expect(response, hasBody('"OK"'));
|
2017-01-14 00:45:35 +00:00
|
|
|
|
|
|
|
// Second request within an hour? No no no!
|
2017-12-22 13:39:21 +00:00
|
|
|
response = await client.get('/once-per-hour', headers: {
|
|
|
|
'accept': 'application/json',
|
|
|
|
});
|
2017-01-14 00:45:35 +00:00
|
|
|
print(response.body);
|
2017-03-29 02:14:47 +00:00
|
|
|
expect(response, isAngelHttpException(statusCode: 429));
|
2017-01-14 00:45:35 +00:00
|
|
|
});
|
2017-01-28 20:29:20 +00:00
|
|
|
|
|
|
|
test('thrice per minute', () async {
|
|
|
|
// First request within the minute is fine
|
|
|
|
var response = await client.get('/thrice-per-minute');
|
|
|
|
print(response.body);
|
2017-03-29 02:44:56 +00:00
|
|
|
expect(response, hasBody('"OK"'));
|
2017-01-28 20:29:20 +00:00
|
|
|
|
|
|
|
// Second request within the minute is fine
|
|
|
|
response = await client.get('/thrice-per-minute');
|
|
|
|
print(response.body);
|
2017-03-29 02:44:56 +00:00
|
|
|
expect(response, hasBody('"OK"'));
|
2017-01-28 20:29:20 +00:00
|
|
|
|
|
|
|
// Third request within the minute is fine
|
|
|
|
response = await client.get('/thrice-per-minute');
|
|
|
|
print(response.body);
|
2017-03-29 02:44:56 +00:00
|
|
|
expect(response, hasBody('"OK"'));
|
2017-01-28 20:29:20 +00:00
|
|
|
|
|
|
|
// Fourth request within a minute? No no no!
|
2017-12-22 13:39:21 +00:00
|
|
|
response = await client.get('/thrice-per-minute', headers: {
|
|
|
|
'accept': 'application/json',
|
|
|
|
});
|
2017-01-28 20:29:20 +00:00
|
|
|
print(response.body);
|
2017-03-29 02:14:47 +00:00
|
|
|
expect(response, isAngelHttpException(statusCode: 429));
|
2017-01-28 20:29:20 +00:00
|
|
|
});
|
2017-01-14 00:45:35 +00:00
|
|
|
}
|