-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
331 lines (304 loc) · 12.3 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
const fastify = require('fastify');
const vhost = require('.');
const noop = () => {};
const tap = require('tap');
const rp = require('request-promise-native');
const pump = require('pump');
const fs = require('fs');
const fastifyA = fastify();
const fastifyB = fastify();
async function stop(){
await fastifyA.close();
await fastifyB.close();
}
const deleteSchema = {
body: {
type: 'object',
properties: {
test: {
type: 'number',
const: 123
}
},
required: ['test'],
additionalProperties: false
}
}
async function listen(){
fastifyA.get('/', async (req, reply) => 'Hi from example.com');
fastifyB.get('/', async (req, reply) => 'Hi from test.example.com');
fastifyA.get('/headers', async (req, reply) => req.headers);
fastifyB.get('/headers', async (req, reply) => req.headers);
fastifyB.get('/timeout', async (req, reply) => { });
fastifyB.get('/error500', (req, reply) => reply.status(500).send('Internal Server Error'));
fastifyB.get('/error400', (req, reply) => reply.status(400).send('Bad Request'));
fastifyA.delete('/', { schema: deleteSchema }, async (req, reply) => 'DELETE example.com');
fastifyB.delete('/', { schema: deleteSchema }, async (req, reply) => 'DELETE test.example.com');
fastifyB.delete('/nobody', async (req, reply) => 'DELETE test.example.com/nobody');
fastifyB.delete('/timeout', { schema: deleteSchema }, async (req, reply) => { });
fastifyB.delete('/error500', { schema: deleteSchema }, (req, reply) => reply.status(500).send('Internal Server Error'));
fastifyB.delete('/error400', { schema: deleteSchema }, (req, reply) => reply.status(400).send('Bad Request'));
fastifyA.post('/multipart', (req, reply) => {
function handler (field, file, filename, encoding, mimetype) {
let stream = fs.createWriteStream('test1-new-fastifyA.txt');
pump(file, stream);
stream.once('close', () => {
reply.send(fs.readFileSync('test1-new-fastifyA.txt').toString());
fs.unlinkSync('test1-new-fastifyA.txt');
});
}
req.multipart(handler, err => {
if(err)
throw err;
});
});
fastifyB.post('/multipart', (req, reply) => {
function handler (field, file, filename, encoding, mimetype) {
let stream = fs.createWriteStream('test1-new-fastifyB.txt');
pump(file, stream);
stream.once('close', () => {
reply.send(fs.readFileSync('test1-new-fastifyB.txt').toString());
fs.unlinkSync('test1-new-fastifyB.txt');
});
}
req.multipart(handler, err => {
if(err)
throw err;
});
});
fastifyA.register(vhost, {
upstream: 'http://localhost:3001',
host: 'test.example.com',
timeout: 3000
});
fastifyA.register(vhost, {
upstream: 'http://localhost:3002',
host: 'test2.example.com',
timeout: 1000
});
fastifyA.register(require('fastify-multipart'));
fastifyB.register(require('fastify-multipart'));
await fastifyA.listen(3000, '0.0.0.0');
await fastifyB.listen(3001, '0.0.0.0');
}
async function testDELETE(){
const body = { test: 123 };
await tap.test('DELETE domain', async () => {
let r = await rp('http://example.com:3000', { method: 'DELETE', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) });
tap.equal(r, 'DELETE example.com');
});
await tap.test('DELETE subdomain', async () => {
let r = await rp('http://test.example.com:3000', { method: 'DELETE', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) });
tap.equal(r, 'DELETE test.example.com');
});
await tap.test('DELETE subdomain without body', async () => {
let r = await (async () => {
try {
await rp('http://test.example.com:3000/nobody', { method: 'DELETE', headers: { 'content-type': 'application/json' } });
return 200;
}
catch(ex){
if(ex && ex.response)
return ex.response.statusCode;
return 500;
}
})();
tap.equal(r, 400, 'upstream error messages should be forwarded');
});
await tap.test('DELETE subdomain (offline upstream)', async () => {
let r = await (async () => {
try {
await rp('http://test2.example.com:3000', { method: 'DELETE', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) });
return 200;
}
catch(ex){
if(ex && ex.response)
return ex.response.statusCode;
return 500;
}
})();
tap.equal(r, 503, 'offline upstream should return Service Unavailable (503)');
});
await tap.test('DELETE subdomain (timeout upstream)', async () => {
let r = await (async () => {
try {
await rp('http://test.example.com:3000/timeout', { method: 'DELETE', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) });
return 200;
}
catch(ex){
if(ex && ex.response)
return ex.response.statusCode;
return 500;
}
})();
tap.equal(r, 504, 'timeout upstream should return Gateway Timeout (504)');
});
await tap.test('DELETE subdomain (500 error upstream)', async () => {
let r = await (async () => {
try {
await rp('http://test.example.com:3000/error500', { method: 'DELETE', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) });
return 200;
}
catch(ex){
if(ex && ex.response)
return ex.response.statusCode;
return -1;
}
})();
tap.equal(r, 500, '500 error upstream should be forwarded');
});
await tap.test('DELETE subdomain (400 error upstream)', async () => {
let r = await (async () => {
try {
await rp('http://test.example.com:3000/error400', { method: 'DELETE', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) });
return 200;
}
catch(ex){
if(ex && ex.response)
return ex.response.statusCode;
return 500;
}
})();
tap.equal(r, 400, '400 error upstream should be forwarded');
});
}
async function testGET(){
await tap.test('GET domain', async () => {
let r = await rp('http://example.com:3000');
tap.equal(r, 'Hi from example.com');
});
await tap.test('GET subdomain', async () => {
let r = await rp('http://test.example.com:3000');
tap.equal(r, 'Hi from test.example.com');
});
await tap.test('GET subdomain (offline upstream)', async () => {
let r = await (async () => {
try {
await rp('http://test2.example.com:3000');
return 200;
}
catch(ex){
if(ex && ex.response)
return ex.response.statusCode;
return 500;
}
})();
tap.equal(r, 503, 'offline upstream should return Service Unavailable (503)');
});
await tap.test('GET subdomain (timeout upstream)', async () => {
let r = await (async () => {
try {
await rp('http://test.example.com:3000/timeout');
return 200;
}
catch(ex){
if(ex && ex.response)
return ex.response.statusCode;
return 500;
}
})();
tap.equal(r, 504, 'timeout upstream should return Gateway Timeout (504)');
});
await tap.test('GET subdomain (500 error upstream)', async () => {
let r = await (async () => {
try {
await rp('http://test.example.com:3000/error500');
return 200;
}
catch(ex){
if(ex && ex.response)
return ex.response.statusCode;
return -1;
}
})();
tap.equal(r, 500, '500 error upstream should be forwarded');
});
await tap.test('GET subdomain (400 error upstream)', async () => {
let r = await (async () => {
try {
await rp('http://test.example.com:3000/error400');
return 200;
}
catch(ex){
if(ex && ex.response)
return ex.response.statusCode;
return 500;
}
})();
tap.equal(r, 400, '400 error upstream should be forwarded');
});
}
async function testHeaders(){
const opt = {
headers: {
'Authorization': '123'
}
};
await tap.test('Headers domain', async () => {
let r = JSON.parse(await rp('http://example.com:3000/headers', opt));
for(let key in opt.headers)
tap.equal(r[key.toLowerCase()], opt.headers[key]);
tap.equal(r.host, 'example.com:3000');
});
await tap.test('Headers subdomain', async () => {
let r = JSON.parse(await rp('http://test.example.com:3000/headers', opt));
for(let key in opt.headers)
tap.equal(r[key.toLowerCase()], opt.headers[key]);
tap.equal(r.host, 'test.example.com:3000');
});
await tap.test('Headers domain & subdomain', async () => {
let r1 = JSON.parse(await rp('http://example.com:3000/headers', opt));
let r2 = JSON.parse(await rp('http://test.example.com:3000/headers', opt));
for(let key in r1)
if(key !== 'host')
tap.equal(r2[key], r1[key]);
});
}
async function testMultipart(){
fs.writeFileSync('test1-original.txt', '123');
const opt = {
method: 'POST',
headers: {
'Authorization': '123'
}
};
opt.formData = {
file: fs.createReadStream('test1-original.txt')
};
await tap.test('Multipart domain', async () => {
let r = await rp('http://example.com:3000/multipart', opt);
tap.equal(r, fs.readFileSync('test1-original.txt').toString());
});
opt.formData = {
file: fs.createReadStream('test1-original.txt')
};
await tap.test('Multipart subdomain', async () => {
let r = await rp('http://test.example.com:3000/multipart', opt);
tap.equal(r, fs.readFileSync('test1-original.txt').toString());
});
fs.unlinkSync('test1-original.txt');
}
function testOptions(){
tap.throws(() => vhost(null, null), {}, 'null options should throw');
tap.throws(() => vhost(null, { host: 'test.com' }), {}, 'options missing upstream should throw');
tap.throws(() => vhost(null, { upstream: 'localhost' }), {}, 'options missing host should throw');
tap.throws(() => vhost(null, { upstream: 'localhost', subdomain: 'test' }), {}, 'options containing subdomain should throw');
tap.throws(() => vhost(null, { upstream: 'localhost', host: 123 }), {}, 'non-string host should throw');
tap.throws(() => vhost(null, { upstream: 'localhost', host: 'test' }), {}, 'tld-missing host should throw');
tap.throws(() => vhost(null, { upstream: 'localhost', host: 'test.com', timeout: 'invalid' }), {}, 'invalid timeout should throw');
tap.throws(() => vhost(null, { upstream: 'localhost', host: 'test.com', timeout: -1 }), {}, 'negative timeout should throw');
tap.throws(() => vhost(null, { upstream: 'localhost', host: 'test.com', timeout: 0 }), {}, 'timeout=0 should throw');
tap.throws(() => vhost(null, { upstream: 'localhost', host: 'test.com', hosts: [ ] }), {}, 'with both host and hosts should throw');
tap.throws(() => vhost(null, { upstream: 'localhost', hosts: [ ] }), {}, 'empty hosts should throw');
tap.doesNotThrow(() => vhost(fastify(), { upstream: 'localhost', fullHost: 'test.com' }, noop), {}, 'should support "fullHost" alias for "host"');
}
async function tests(){
testOptions();
await tap.test('listen', async (t) => listen());
await testGET();
await testDELETE();
await testHeaders();
await testMultipart();
await stop();
}
tests();