-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathcore-error.test.js
72 lines (60 loc) · 1.79 KB
/
core-error.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
'use strict';
const test = require('tap').test;
const ecstatic = require('../lib/core');
const http = require('http');
const request = require('request');
const path = require('path');
const root = `${__dirname}/public`;
const baseDir = 'base';
require('fs').mkdirSync(`${root}/emptyDir`, {recursive: true});
const cases = require('./fixtures/common-cases-error');
test('core', (t) => {
require('portfinder').getPort((err, port) => {
const filenames = Object.keys(cases);
const server = http.createServer(
ecstatic({
root,
gzip: true,
baseDir,
autoIndex: true,
showDir: true,
handleError: false,
})
);
server.listen(port, () => {
let pending = filenames.length;
filenames.forEach((file) => {
const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`;
const headers = cases[file].headers || {};
request.get({
uri,
followRedirect: false,
headers,
}, (err, res, body) => {
if (err) {
t.fail(err);
}
const r = cases[file];
t.equal(res.statusCode, r.code, `status code for \`${file}\``);
if (r.type !== undefined) {
t.equal(
res.headers['content-type'].split(';')[0], r.type,
`content-type for \`${file}\``
);
}
if (r.body !== undefined) {
t.equal(body, r.body, `body for \`${file}\``);
}
if (r.location !== undefined) {
t.equal(res.headers.location, path.join('/', baseDir, r.location), `location for \`${file}\``);
}
pending -= 1;
if (pending === 0) {
server.close();
t.end();
}
});
});
});
});
});