-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
63 lines (53 loc) · 1.58 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
'use strict';
const path = require('path');
const test = require('ava');
const express = require('express');
const routes = require('express-mount-routes');
const request = require('supertest');
const ApiRate = require('./index.js');
function makeApp() {
const apiRate = new ApiRate();
const app = express();
app.use(apiRate.record());
routes(app, path.join(__dirname, 'controllers'));
apiRate.mouteRoutes(app);
return app;
}
function makeAppWithFlushdb() {
const apiRate = new ApiRate({ flushdb: true });
const app = express();
app.use(apiRate.record());
routes(app, path.join(__dirname, 'controllers'));
apiRate.mouteRoutes(app);
return app;
}
test('GET /tests/1', async (t) => {
t.plan(4);
const app = makeApp();
await request(app).get('/tests/1');
const res = await request(app).get('/api-data');
t.is(res.status, 200);
t.is(res.type, 'application/json');
t.is(typeof +res.body.count, 'number');
t.true(res.body.rank.length > 0);
});
test('POST /tests/2', async (t) => {
t.plan(4);
const app = makeAppWithFlushdb();
await request(app).post('/tests/2');
const res = await request(app).get('/api-data');
t.is(res.status, 200);
t.is(res.type, 'application/json');
t.is(typeof +res.body.count, 'number');
t.true(res.body.rank.length > 0);
});
test('GET /tests/3', async (t) => {
t.plan(4);
const app = makeApp();
await request(app).get('/tests/3');
const res = await request(app).get('/api-data');
t.is(res.status, 200);
t.is(res.type, 'application/json');
t.is(typeof +res.body.count, 'number');
t.true(res.body.rank.length > 0);
});