-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
70 lines (57 loc) · 1.48 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
import test from 'ava';
import t from 'tcomb-validation';
import express from 'express';
import Router from 'express-router-tcomb';
import Agent from './';
test('test ok', async assert => {
const app = express();
const router = Router();
router.route({
method: 'GET',
path: '/api/v1/items/:id',
schema: {
response: t.struct({
status: t.String,
id: t.String
})
},
handler: (req, res) => {
res.json({ status: 'ok', id: req.params.id });
}
});
app.use('/', router.getRoutes(app));
const server = app.listen();
const agent = Agent(app, server);
const id = 'test';
const { body } = await agent.get('/api/v1/items/:id', { params: { id } });
assert.truthy(body);
});
test('test error', async assert => {
const app = express();
const router = Router();
router.route({
method: 'GET',
path: '/api/v1/items/:id',
schema: {
response: t.struct({
status: t.String,
id: t.Number
})
},
handler: (req, res) => {
res.json({ status: 'ok', id: req.params.id });
}
});
app.use('/', router.getRoutes(app));
const server = app.listen();
const agent = Agent(app, server);
const id = 'test';
try {
const { body } = await agent.get('/api/v1/items/:id', { params: { id } });
assert.falsy(body);
} catch (e) {
assert.truthy(e);
}
const { body } = await agent.get('/api/v1/items/:id', { params: { id }, validateResponse: false });
assert.truthy(body);
});