This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
config.js
203 lines (170 loc) · 4.95 KB
/
config.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const test = require('bron');
const assert = require('assert').strict;
const _ = require('lodash');
const request = require('supertest');
const { getService } = require('./_helpers');
test('should return cached response', async () => {
let id = 0;
const app = getService({
path: '/cache',
template: {
id: () => id++
}
});
const res = await request(app).get('/cache');
const cachedRes = await request(app).get('/cache');
assert.equal(cachedRes.status, 200);
assert.deepEqual(cachedRes.body, res.body);
});
test('should not cache the response with a different method', async () => {
let id = 0;
const app = getService([
{
path: '/cache',
cache: false,
method: 'GET',
template: {
id: () => id++
}
},
{
path: '/cache',
cache: false,
method: 'POST',
template: {
id: () => id++
}
}
]);
const res = await request(app).get('/cache');
const cachedRes = await request(app).post('/cache');
assert.equal(cachedRes.status, 200);
assert.notDeepEqual(cachedRes.body, res.body);
});
test('should return uncached response', async () => {
let id = 0;
const app = getService({
path: '/no-cache',
cache: false,
template: {
id: () => id++
}
});
const res = await request(app).get('/no-cache');
const uncachedRes = await request(app).get('/no-cache');
assert.equal(uncachedRes.status, 200);
assert.notDeepEqual(uncachedRes.body, res.body);
});
test('should respond with a collection', async () => {
let id = 0;
const config = {
path: '/collection',
collection: true,
size: 2,
template: {
id: () => ++id
}
};
const app = getService([
config,
{
...config,
path: '/collection-as-function',
collection: () => true
},
{
...config,
path: '/collection-as-function-negative',
collection: () => false
},
{
...config,
path: '/size-as-function',
size: (params, query) => query.count
}
]);
const res1 = await request(app).get('/collection');
const res2 = await request(app).get('/collection-as-function');
const res3 = await request(app).get('/collection-as-function-negative');
const res4 = await request(app).get('/size-as-function?count=3');
assert.equal(res1.status, 200);
assert.equal(res2.status, 200);
assert.equal(res3.status, 200);
assert.equal(res4.status, 200);
assert.deepEqual(res1.body, [{ id: 1 }, { id: 2 }]);
assert.deepEqual(res2.body, [{ id: 3 }, { id: 4 }]);
assert.deepEqual(res3.body, { id: 5 });
assert.deepEqual(res4.body, [{ id: 6 }, { id: 7 }, { id: 8 }]);
});
test('should respond with a collection (combined request)', async () => {
const config = {
path: '/combined/:id',
template: {
id: params => Number(params.id)
}
};
const app = getService(config, {
multiRequest: ','
});
const res = await request(app).get('/combined/1,2,3');
assert.equal(res.status, 200);
assert.deepEqual(res.body, [{ id: 1 }, { id: 2 }, { id: 3 }]);
});
test('should respond with a 204 for an OPTIONS request', async () => {
const app = getService({
path: '/opts',
template: []
});
const res = await request(app).options('/opts');
assert.equal(res.status, 204);
assert.equal(res.headers['access-control-allow-methods'], 'GET,HEAD,PUT,PATCH,POST,DELETE');
assert.equal(res.headers['access-control-allow-credentials'], 'true');
// The next actual value is 'undefined', should be req.header('Origin') (probably an issue with supertest)
// assert.equal(res.headers['access-control-allow-origin'], '*');
});
test('should respond with 400 bad request if required parameter not found', async () => {
const app = getService({
path: '/require-param',
requireParameters: ['name'],
template: []
});
const res = await request(app).get('/require-param');
assert.equal(res.status, 400);
assert.deepEqual(res.body, { error: 'Required parameters (name) not found.' });
const resParam = await request(app).get('/require-param?name=foo');
assert.equal(resParam.status, 200);
assert.deepEqual(resParam.body, []);
});
test('should delay the response', async () => {
const app = getService({
path: '/delay',
delay: 200,
template: []
});
const start = _.now();
const res = await request(app).get('/delay');
const delayed = _.now() - start;
assert.equal(res.status, 200);
assert(delayed >= 200);
});
test('should support status function', async () => {
const app = getService({
path: '/status-418',
status: (req, res, next) => {
res.status(418);
next();
},
template: ['foo', 'bar']
});
const res = await request(app).get('/status-418');
assert.equal(res.status, 418);
assert.deepEqual(res.body, ['foo', 'bar']);
});
test('should support HEAD requests', async () => {
const app = getService({
path: '/head',
method: 'HEAD'
});
const res = await request(app).head('/head');
assert.equal(res.status, 200);
});