forked from http-party/http-server
/
cli.test.js
137 lines (107 loc) · 3.34 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
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
'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 node = process.execPath;
const defaultUrl = 'http://localhost';
const defaultPort = 8080;
function getRandomInt(min, max) {
return Math.floor(Math.random() * ((max - min) + 1)) + min;
}
function startEcstatic(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 getRandomPort = (() => {
const usedPorts = [];
return () => {
const port = getRandomInt(1025, 65536);
if (usedPorts.indexOf(port) > -1) {
return getRandomPort();
}
usedPorts.push(port);
return port;
};
})();
test('setting port via cli - default port', (t) => {
t.plan(2);
const port = defaultPort;
const options = ['.'];
const ecstatic = startEcstatic(options);
tearDown(ecstatic, t);
ecstatic.stdout.on('data', (msg) => {
checkServerIsRunning(`${defaultUrl}:${port}`, msg, t);
});
});
test('setting port via cli - custom port', (t) => {
t.plan(2);
const port = getRandomPort();
const options = ['.', '--port', port];
const ecstatic = startEcstatic(options);
tearDown(ecstatic, t);
ecstatic.stdout.on('data', (msg) => {
checkServerIsRunning(`${defaultUrl}:${port}`, msg, t);
});
});
test('setting mimeTypes via cli - .types file', (t) => {
t.plan(4);
const port = getRandomPort();
const root = path.resolve(__dirname, 'public/');
const pathMimetypeFile = path.resolve(__dirname, 'fixtures/custom_mime_type.types');
const options = [root, '--port', port, '--mimetypes', pathMimetypeFile];
const ecstatic = startEcstatic(options);
tearDown(ecstatic, t);
ecstatic.stdout.on('data', (msg) => {
checkServerIsRunning(`${defaultUrl}:${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);
const port = getRandomPort();
const root = path.resolve(__dirname, 'public/');
const mimeType = ['--mimetypes', '{ "application/x-my-type": ["opml"] }'];
const options = [root, '--port', port].concat(mimeType);
const ecstatic = startEcstatic(options);
// TODO: remove error handler
tearDown(ecstatic, t);
ecstatic.stdout.on('data', (msg) => {
checkServerIsRunning(`${defaultUrl}:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
t.error(err);
t.equal(res.headers['content-type'], 'application/x-my-type');
});
});
});
test('--proxy requires you to specify a protocol', (t) => {
t.plan(1);
const options = ['.', '--proxy', 'google.com'];
const ecstatic = startEcstatic(options);
tearDown(ecstatic, t);
ecstatic.on('exit', (code) => {
t.equal(code, 1);
});
});