-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
125 lines (110 loc) · 3.23 KB
/
tests.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
/* global describe, it */
const expect = require('chai').expect
const request = require('supertest')
const iffUnless = require('./index')()
const middleware = function (req, res, next) {
res.body = 'hit'
res.setHeader('id', req.params ? String(req.params.id) : '')
return next()
}
describe('middleware-if-unless', () => {
let server
const service = require('restana')({
defaultRoute: (req, res) => res.send(200)
})
it('should successfully setup the testing HTTP service', async () => {
// extend middleware with iff-unless
iffUnless(middleware)
// setup execution rules
service.use(middleware
.iff(req => req.url.startsWith('/pets'))
.iff([
'/pets/*',
'/pets/:id',
{
methods: ['GET'],
url: '/pets/:id',
version: '3.0.0',
updateParams: true
}
])
.unless([
'/pets/groups/list',
{
url: '/pets/:id', methods: ['DELETE']
}
])
.unless(req => req.url.endsWith('.js'))
.unless([{
url: '/pets/:id', methods: ['GET'], version: '2.x'
}])
)
server = await service.start(~~process.env.PORT)
})
describe('iff', () => {
it('should hit middleware using GET /pets/:id', async () => {
await request(server)
.get('/pets/0')
.then((response) => {
expect(response.text).to.equal('hit')
})
})
it('should hit middleware using GET /pets/retrieve/dogs', async () => {
await request(server)
.get('/pets/retrieve/dogs')
.then((response) => {
expect(response.text).to.equal('hit')
})
})
})
describe('unless', () => {
it('should skip middleware using DELETE /pets/0', async () => {
await request(server)
.del('/pets/0')
.then((response) => {
expect(response.text).to.equal('')
})
})
it('should skip middleware using GET /pets/groups/list', async () => {
await request(server)
.get('/pets/groups/list')
.then((response) => {
expect(response.text).to.equal('')
})
})
it('should skip middleware using GET *.js', async () => {
await request(server)
.get('/script.js')
.then((response) => {
expect(response.text).to.equal('')
})
})
it('should skip middleware on GET /pets/:id using accept-version 2.x', async () => {
await request(server)
.get('/pets/0')
.set('accept-version', '2.0.1')
.then((response) => {
expect(response.text).to.equal('')
})
})
it('should hit middleware on GET /pets/:id using accept-version 3.x', async () => {
await request(server)
.get('/pets/0')
.set('accept-version', '3.x')
.then((response) => {
expect(response.text).to.equal('hit')
})
})
it('should get middleware route params on GET /pets/:id using accept-version 3.x', async () => {
await request(server)
.get('/pets/0')
.set('accept-version', '3.x')
.then((response) => {
expect(response.headers.id).to.equal('0')
})
})
})
it('should successfully terminate the service', async () => {
await service.close()
})
})