-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
131 lines (103 loc) · 2.97 KB
/
test.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var test = require('tape'),
request = require('request'),
express = require('express'),
limitr = require('./index'),
TimeDevice = require('./lib/time-device');
var config = {
limit: 5,
rate: 2 /* seconds */,
clientId: 'headers.token',
ignore: ['ignore-me']
};
test('time-device test', function(t) {
t.plan(9);
var td = new TimeDevice({
limit: config.limit,
period: config.period
});
td.start();
var loops = 5;
for(var i = 0; i < loops; i++) {
td.increment();
}
/* Test limit not exceeded */
t.equal(td.requests, loops);
t.equal(td.remainingRequests(), config.limit - loops);
t.equal(td.limitExceeded, false);
td.increment();
/* Test limit exceeded */
t.equal(td.requests, loops + 1);
t.equal(td.remainingRequests(), 0);
t.equal(td.limitExceeded, true);
/* Test rate limit reset */
setTimeout(function() {
t.equal(td.requests, 0);
t.equal(td.remainingRequests(), config.limit);
t.equal(td.limitExceeded, false);
}, 2000);
});
test('limitr test', function(t) {
t.plan(1);
var fn = limitr(config);
t.equal(typeof fn, 'function');
});
test('rest test', function(t) {
t.plan(15);
var app = express();
app.use('/', limitr(config));
app.get('/ping', function(req, res) {
res.send(200, 'Pong!');
});
app.listen(1990);
var limitHeader = 'x-ratelimit-limit';
var remainingHeader = 'x-ratelimit-remaining';
var resetHeader = 'x-ratelimit-reset';
/* Test ignore */
var optsIgnore = {
url: 'https://localhost:1990/ping',
method: 'GET',
headers: {
token: 'ignore-me'
}
};
request(optsIgnore, function(err, response, body) {
t.equal(response.statusCode, 200);
t.equal(body, 'Pong!');
t.equal(response.headers[limitHeader], undefined);
t.equal(response.headers[remainingHeader], undefined);
t.equal(response.headers[resetHeader], undefined);
});
/* Test normal rate-limiting scenario */
var opts = {
url: 'https://localhost:1990/ping',
method: 'GET',
headers: {
token: 'dont-ignore-me'
}
};
request(opts, function(err, response, body) {
t.equal(response.statusCode, 200);
t.equal(body, 'Pong!');
t.equal(parseInt(response.headers[limitHeader]), config.limit);
t.equal(parseInt(response.headers[remainingHeader]), config.limit - 1);
var resetTime = parseInt(response.headers[resetHeader]);
t.equal(resetTime > Date.now(), true);
});
/* Test rate-limiting exceeded */
var loops = 4;
for(var i = 0; i < loops; i++) {
request(opts);
}
request(opts, function(err, response, body) {
t.equal(response.statusCode, 429);
t.equal(JSON.parse(body).message, 'API rate limit exceeded.');
t.equal(parseInt(response.headers[limitHeader]), config.limit);
t.equal(parseInt(response.headers[remainingHeader]), 0);
var resetTime = parseInt(response.headers[resetHeader]);
t.equal(resetTime > Date.now(), true);
});
});
test('end', function(t) {
t.end();
process.exit();
});