-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
96 lines (82 loc) · 2.18 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
import test from 'ava';
import m from '.'; // eslint-disable-line import/order
import restify from 'restify';
import restifyErrors from 'restify-errors';
import restifyClients from 'restify-clients';
let PORT = 6543;
test.before(() => {
m.install();
});
test.after(() => {
m.uninstall();
});
test.cb.beforeEach(t => {
t.context.port = PORT++;
t.context.server = restify.createServer();
t.context.client = restifyClients.createJsonClient({
url: 'http://127.0.0.1:' + t.context.port,
});
t.context.server.use(
restify.plugins.throttle({
burst: 1,
rate: 1,
ip: true,
})
);
t.context.server.listen(t.context.port, '127.0.0.1', t.end);
});
test.cb.afterEach(t => {
t.context.client.close();
t.context.server.close(t.end);
});
test.cb('should set RNFE as errno if the endpoint does not exists', t => {
t.context.client.get('/foo/bar', err => {
t.true(err instanceof Error);
t.is(err.body.code, 'ResourceNotFound');
t.is(err.body.errno, 'RNFE');
t.end();
});
});
test.cb(
"should set IVE as errno if the requested endpoint's version doesn't match",
t => {
t.context.server.get(
{path: '/foo/bar', version: '1.0.0'},
(req, res, next) => {
res.send();
next();
}
);
t.context.server.on('VersionNotAllowed', (req, res, err, next) => {
t.true(err instanceof restifyErrors.InvalidVersionError);
t.is(err.body.code, 'InvalidVersion');
t.is(err.body.errno, 'IVE');
next();
});
t.context.client.get(
{path: '/foo/bar', headers: {'accept-version': '3.0.0'}},
err => {
t.true(err instanceof Error);
t.is(err.body.code, 'InvalidVersion');
t.is(err.body.errno, 'IVE');
t.end();
}
);
}
);
test.cb('should set TMRE as errno if too many requests are recived', t => {
t.context.server.get('/foo/bar', (req, res, next) => {
res.send();
next();
});
const requests = Array.from(new Array(100).keys());
requests.forEach(() =>
t.context.client.get('/foo/bar', err => {
if (err) {
t.is(err.body.code, 'TooManyRequests');
t.is(err.body.errno, 'TMRE');
t.end();
}
})
);
});