Skip to content

Commit 51ee532

Browse files
dxdxdtKernelDeimos
authored andcommitted
Add tests for --header option, accept empty header
- Change --help option output - Accept empty header to conform to RFC 7230 (-H "empty-header" or -H "empty-header:")
1 parent 11b57b1 commit 51ee532

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

bin/http-server

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ if (argv.h || argv.help) {
3737
' --cors[=headers] Enable CORS via the "Access-Control-Allow-Origin" header',
3838
' Optionally provide CORS headers list separated by commas',
3939
' -H',
40-
' --header="key: val"',
41-
' Add an arbitrary header (repeatable)',
40+
' --header',
41+
' Add an extra response header (can be used several times)',
4242
' -o [path] Open browser window after starting the server.',
4343
' Optionally provide a URL path to open the browser window to.',
4444
' -c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.',
@@ -162,11 +162,11 @@ function listen(port) {
162162
};
163163

164164
function setHeader(str) {
165-
const m = /^(.+?)\s*:\s*(.*)$/.exec(str);
166-
if (!m) {
167-
options.headers[str] = true;
165+
const m = /^(.+?)\s*(:\s*(.*))$/.exec(str);
166+
if (!m || m.length < 4) {
167+
options.headers[str] = '';
168168
} else {
169-
options.headers[m[1]] = m[2];
169+
options.headers[m[1]] = m[3];
170170
}
171171
}
172172

test/cli.test.js

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,67 @@ test('--proxy requires you to specify a protocol', (t) => {
113113
server.on('exit', (code) => {
114114
t.equal(code, 1);
115115
});
116-
});
116+
});
117+
118+
function doHeaderOptionTest(t, argv, obj) {
119+
const options = ['.', '--port', defaultPort].concat(argv);
120+
const server = startServer(options);
121+
122+
tearDown(server, t);
123+
124+
server.stdout.on('data', (msg) => {
125+
checkServerIsRunning(`http://localhost:${defaultPort}`, msg, t, (err, res) => {
126+
t.error(err);
127+
128+
for (const [k, v] of Object.entries(obj)) {
129+
t.equal(res.headers[k], v, 'expected header value matches in response');
130+
}
131+
});
132+
});
133+
}
134+
135+
test('single --header option is applied', (t) => {
136+
t.plan(4);
137+
138+
doHeaderOptionTest(t,
139+
['--header=X-http-server-test-A: hello'],
140+
{ 'x-http-server-test-a': 'hello' }
141+
);
142+
});
143+
144+
test('single -H option is applied', (t) => {
145+
t.plan(4);
146+
147+
doHeaderOptionTest(t,
148+
['-H', 'X-http-server-test-A: hello'],
149+
{ 'x-http-server-test-a': 'hello' }
150+
);
151+
});
152+
153+
test('mix of multiple --header and -H options are applied', (t) => {
154+
t.plan(7);
155+
156+
doHeaderOptionTest(t,
157+
[
158+
'--header=X-http-server-test-A: Lorem ipsum dolor sit amet',
159+
'-H', 'X-http-server-test-B: consectetur=adipiscing; elit',
160+
'-H', 'X-http-server-test-C: c',
161+
'--header=X-http-server-test-D: d'
162+
],
163+
{
164+
'x-http-server-test-a': 'Lorem ipsum dolor sit amet',
165+
'x-http-server-test-b': 'consectetur=adipiscing; elit',
166+
'x-http-server-test-c': 'c',
167+
'x-http-server-test-d': 'd'
168+
}
169+
);
170+
});
171+
172+
test('empty header value is allowed (RFC 7230)', (t) => {
173+
t.plan(5);
174+
175+
doHeaderOptionTest(t,
176+
['-H', 'X-http-server-test-empty-a:', '-H', 'X-http-server-test-empty-b'],
177+
{ 'x-http-server-test-empty-a': '', 'x-http-server-test-empty-b': '' }
178+
);
179+
});

0 commit comments

Comments
 (0)