From fe2f7b59455bf6612310d977d4b1efe6ca299a9e Mon Sep 17 00:00:00 2001 From: boarwell <34891695+boarwell@users.noreply.github.com> Date: Thu, 30 Sep 2021 23:58:21 +0900 Subject: [PATCH] Use relative paths in directory listing #661 --- lib/core/show-dir/index.js | 2 +- test/main.test.js | 4 ++-- test/showdir-href-encoding.test.js | 2 +- test/showdir-search-encoding.test.js | 2 +- test/showdir-with-spaces.test.js | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/core/show-dir/index.js b/lib/core/show-dir/index.js index 70cb5d1bb..f496b2d22 100644 --- a/lib/core/show-dir/index.js +++ b/lib/core/show-dir/index.js @@ -95,7 +95,7 @@ module.exports = (opts) => { const writeRow = (file) => { // render a row given a [name, stat] tuple const isDir = file[1].isDirectory && file[1].isDirectory(); - let href = `${parsed.pathname.replace(/\/$/, '')}/${encodeURIComponent(file[0])}`; + let href = `./${encodeURIComponent(file[0])}`; // append trailing slash and query for dir entry if (isDir) { diff --git a/test/main.test.js b/test/main.test.js index 0d1c1b26a..cead96470 100644 --- a/test/main.test.js +++ b/test/main.test.js @@ -57,8 +57,8 @@ test('http-server main', (t) => { requestAsync("http://localhost:8080/").then(res => { t.ok(res); t.equal(res.statusCode, 200); - t.includes(res.body, '/file'); - t.includes(res.body, '/canYouSeeMe'); + t.includes(res.body, './file'); + t.includes(res.body, './canYouSeeMe'); // Custom headers t.equal(res.headers['access-control-allow-origin'], '*'); diff --git a/test/showdir-href-encoding.test.js b/test/showdir-href-encoding.test.js index df5ae4cef..74f5ca7ed 100644 --- a/test/showdir-href-encoding.test.js +++ b/test/showdir-href-encoding.test.js @@ -26,7 +26,7 @@ test('url encoding in href', (t) => { request.get({ uri, }, (err, res, body) => { - t.match(body, /href="\/base\/show-dir%24%24href_encoding%24%24\/aname%2Baplus.txt"/, 'We found the right href'); + t.match(body, /href="\.\/aname%2Baplus.txt"/, 'We found the right href'); server.close(); t.end(); }); diff --git a/test/showdir-search-encoding.test.js b/test/showdir-search-encoding.test.js index 9561881c2..bcda82b0b 100644 --- a/test/showdir-search-encoding.test.js +++ b/test/showdir-search-encoding.test.js @@ -26,7 +26,7 @@ test('directory listing with query string specified', (t) => { request.get({ uri, }, (err, res, body) => { - t.match(body, /href="\/base\/subdir\/\?a=1&b=2"/, 'We found the encoded href'); + t.match(body, /href="\.\/subdir\/\?a=1&b=2"/, 'We found the encoded href'); t.notMatch(body, /a=1&b=2/, 'We didn\'t find the unencoded query string value'); server.close(); t.end(); diff --git a/test/showdir-with-spaces.test.js b/test/showdir-with-spaces.test.js index e5106c406..870eade2d 100644 --- a/test/showdir-with-spaces.test.js +++ b/test/showdir-with-spaces.test.js @@ -26,7 +26,7 @@ test('directory listing when directory name contains spaces', (t) => { request.get({ uri, }, (err, res, body) => { - t.ok(/href="\/base\/subdir_with%20space\/index.html"/.test(body), 'We found the right href'); + t.ok(/href="\.\/index.html"/.test(body), 'We found the right href'); server.close(); t.end(); });