-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathpathname-encoding.test.js
79 lines (68 loc) · 1.79 KB
/
pathname-encoding.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
'use strict';
const tap = require('tap');
const ecstatic = require('../lib/core');
const http = require('http');
const request = require('request');
const path = require('path');
const portfinder = require('portfinder');
const test = tap.test;
const root = `${__dirname}/public`;
const baseDir = 'base';
if (process.platform === 'win32') {
tap.plan(0, 'Windows is allergic to < in path names');
return;
}
const fs = require('fs');
test('create test directory', (t) => {
fs.mkdirSync(`${root}/<dir>`, '0755');
t.end();
});
test('directory listing with pathname including HTML characters', (t) => {
portfinder.getPort((err, port) => {
const uri = `http://localhost:${port}${path.join('/', baseDir, '/%3Cdir%3E')}`;
const server = http.createServer(
ecstatic({
root,
baseDir,
showDir: true,
autoIndex: false,
})
);
server.listen(port, () => {
request.get({
uri,
}, (err, res, body) => {
t.notMatch(body, /<dir>/, 'We didn\'t find the unencoded pathname');
t.match(body, /<dir>/, 'We found the encoded pathname');
server.close();
t.end();
});
});
});
});
test('NULL byte in request path does not crash server', (t) => {
portfinder.getPort((err, port) => {
const uri = `http://localhost:${port}${path.join('/', baseDir, '/%00')}`;
const server = http.createServer(
ecstatic({
root,
baseDir,
})
);
try {
server.listen(port, () => {
request.get({uri}, (err, res, body) => {
t.pass('server did not crash')
server.close();
t.end();
});
});
} catch (err) {
t.fail(err.toString());
}
});
});
test('remove test directory', (t) => {
fs.rmdirSync(`${root}/<dir>`);
t.end();
});