/
cli.test.js
103 lines (83 loc) · 2.65 KB
/
cli.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
'use strict';
/* this test suit is incomplete 2015-12-18 */
const test = require('tap').test;
const request = require('request');
const spawn = require('child_process').spawn;
const path = require('path');
const portfinder = require('portfinder');
const node = process.execPath;
const defaultPort = 8080;
function startServer(args) {
return spawn(node, [require.resolve('../bin/http-server')].concat(args));
}
function checkServerIsRunning(url, msg, t, _cb) {
if (!msg.toString().match(/Starting up/)) {
return;
}
t.pass('http-server started');
const cb = _cb || (() => {});
request(url, (err, res) => {
if (!err && res.statusCode !== 500) {
t.pass('a successful request from the server was made');
cb(null, res);
} else {
t.fail(`the server could not be reached @ ${url}`);
cb(err);
}
});
}
function tearDown(ps, t) {
t.tearDown(() => {
ps.kill('SIGTERM');
});
}
const getPort = () => new Promise((resolve, reject) => {
portfinder.getPort((err, port) => {
if (err) reject(err);
resolve(port);
});
});
test('setting port via cli - custom port', (t) => {
t.plan(2);
getPort().then((port) => {
const options = ['.', '--port', port];
const server = startServer(options);
tearDown(server, t);
server.stdout.on('data', (msg) => {
checkServerIsRunning(`http://localhost:${port}`, msg, t);
});
});
});
test('setting mimeTypes via cli - .types file', (t) => {
t.plan(4);
getPort().then((port) => {
const root = path.resolve(__dirname, 'public/');
const pathMimetypeFile = path.resolve(__dirname, 'fixtures/custom_mime_type.types');
const options = [root, '--port', port, '--mimetypes', pathMimetypeFile];
const server = startServer(options);
tearDown(server, t);
server.stdout.on('data', (msg) => {
checkServerIsRunning(`http://localhost:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
t.error(err);
t.equal(res.headers['content-type'], 'application/secret');
});
});
});
});
test('setting mimeTypes via cli - directly', (t) => {
t.plan(4);
getPort().then((port) => {
const root = path.resolve(__dirname, 'public/');
const mimeType = ['--mimetypes', '{ "application/x-my-type": ["opml"] }'];
const options = [root, '--port', port].concat(mimeType);
const server = startServer(options);
// TODO: remove error handler
tearDown(server, t);
server.stdout.on('data', (msg) => {
checkServerIsRunning(`http://localhost:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
t.error(err);
t.equal(res.headers['content-type'], 'application/x-my-type');
});
});
});
});