From 8c48c0ac3b40c6cb1307310c4dc4cd5d8a4033f9 Mon Sep 17 00:00:00 2001 From: Jade Michael Thornton Date: Wed, 6 Oct 2021 13:54:06 -0500 Subject: [PATCH] get a good port every time fixes #723 --- test/304.test.js | 325 +++++++++++++------------ test/core-error.test.js | 89 +++---- test/core.test.js | 91 +++---- test/express-error.test.js | 88 +++---- test/express.test.js | 89 +++---- test/showdir-href-encoding.test.js | 35 +-- test/showdir-pathname-encoding.test.js | 41 ++-- test/showdir-search-encoding.test.js | 37 +-- test/showdir-with-spaces.test.js | 35 +-- 9 files changed, 420 insertions(+), 410 deletions(-) diff --git a/test/304.test.js b/test/304.test.js index a7e57512a..5dda54851 100644 --- a/test/304.test.js +++ b/test/304.test.js @@ -5,163 +5,167 @@ const ecstatic = require('../lib/core'); const http = require('http'); const request = require('request'); const path = require('path'); +const portfinder = require('portfinder'); const root = `${__dirname}/public`; const baseDir = 'base'; test('304_not_modified_strong', (t) => { - const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4); - const file = 'a.txt'; - - const server = http.createServer( - ecstatic({ - root, - gzip: true, - baseDir, - autoIndex: true, - showDir: true, - weakEtags: false, - weakCompare: false, - }) - ); - - server.listen(port, () => { - const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; - - request.get({ - uri, - followRedirect: false, - }, (err, res) => { - if (err) { - t.fail(err); - } - - t.equal(res.statusCode, 200, 'first request should be a 200'); + portfinder.getPort((err, port) => { + const file = 'a.txt'; + + const server = http.createServer( + ecstatic({ + root, + gzip: true, + baseDir, + autoIndex: true, + showDir: true, + weakEtags: false, + weakCompare: false, + }) + ); + + server.listen(port, () => { + const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; request.get({ uri, followRedirect: false, - headers: { 'if-modified-since': res.headers['last-modified'] }, - }, (err2, res2) => { - if (err2) { - t.fail(err2); + }, (err, res) => { + if (err) { + t.fail(err); } - t.equal(res2.statusCode, 304, 'second request should be a 304'); - t.equal(res2.headers.etag.indexOf('"'), 0, 'should return a strong etag'); - server.close(); - t.end(); + t.equal(res.statusCode, 200, 'first request should be a 200'); + + request.get({ + uri, + followRedirect: false, + headers: { 'if-modified-since': res.headers['last-modified'] }, + }, (err2, res2) => { + if (err2) { + t.fail(err2); + } + + t.equal(res2.statusCode, 304, 'second request should be a 304'); + t.equal(res2.headers.etag.indexOf('"'), 0, 'should return a strong etag'); + server.close(); + t.end(); + }); }); }); }); }); test('304_not_modified_weak', (t) => { - const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4); - const file = 'b.txt'; - - const server = http.createServer( - ecstatic({ - root, - gzip: true, - baseDir, - autoIndex: true, - showDir: true, - weakCompare: false, - }) - ); - - server.listen(port, () => { - const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; - const now = (new Date()).toString(); - - request.get({ - uri, - followRedirect: false, - }, (err, res) => { - if (err) { - t.fail(err); - } - - t.equal(res.statusCode, 200, 'first request should be a 200'); + portfinder.getPort((err, port) => { + const file = 'b.txt'; + + const server = http.createServer( + ecstatic({ + root, + gzip: true, + baseDir, + autoIndex: true, + showDir: true, + weakCompare: false, + }) + ); + + server.listen(port, () => { + const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; + const now = (new Date()).toString(); request.get({ uri, followRedirect: false, - headers: { 'if-modified-since': now }, - }, (err2, res2) => { - if (err2) t.fail(err2); - - t.equal(res2.statusCode, 304, 'second request should be a 304'); - t.equal(res2.headers.etag.indexOf('W/'), 0, 'should return a weak etag'); - server.close(); - t.end(); + }, (err, res) => { + if (err) { + t.fail(err); + } + + t.equal(res.statusCode, 200, 'first request should be a 200'); + + request.get({ + uri, + followRedirect: false, + headers: { 'if-modified-since': now }, + }, (err2, res2) => { + if (err2) t.fail(err2); + + t.equal(res2.statusCode, 304, 'second request should be a 304'); + t.equal(res2.headers.etag.indexOf('W/'), 0, 'should return a weak etag'); + server.close(); + t.end(); + }); }); }); }); }); test('304_not_modified_strong_compare', (t) => { - const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4); - const file = 'b.txt'; - - const server = http.createServer( - ecstatic({ - root, - gzip: true, - baseDir, - autoIndex: true, - showDir: true, - weakEtags: false, - weakCompare: false, - }) - ); - - server.listen(port, () => { - const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; - const now = (new Date()).toString(); - let etag = null; - - request.get({ - uri, - followRedirect: false, - }, (err, res) => { - if (err) { - t.fail(err); - } - - t.equal(res.statusCode, 200, 'first request should be a 200'); - - etag = res.headers.etag; + portfinder.getPort((err, port) => { + const file = 'b.txt'; + + const server = http.createServer( + ecstatic({ + root, + gzip: true, + baseDir, + autoIndex: true, + showDir: true, + weakEtags: false, + weakCompare: false, + }) + ); + + server.listen(port, () => { + const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; + const now = (new Date()).toString(); + let etag = null; request.get({ uri, followRedirect: false, - headers: { 'if-modified-since': now, 'if-none-match': etag }, - }, (err2, res2) => { - if (err2) { - t.fail(err2); + }, (err, res) => { + if (err) { + t.fail(err); } - t.equal(res2.statusCode, 304, 'second request with a strong etag should be 304'); + t.equal(res.statusCode, 200, 'first request should be a 200'); + + etag = res.headers.etag; request.get({ uri, followRedirect: false, - headers: { 'if-modified-since': now, 'if-none-match': `W/${etag}` }, - }, (err3, res3) => { - if (err3) { - t.fail(err3); + headers: { 'if-modified-since': now, 'if-none-match': etag }, + }, (err2, res2) => { + if (err2) { + t.fail(err2); } - // Note that if both if-modified-since and if-none-match are - // provided, the server MUST NOT return a response status of 304 - // unless doing so is consistent with all of the conditional - // header fields in the request - // https://www.ietf.org/rfc/rfc2616.txt - t.equal(res3.statusCode, 200, 'third request with a weak etag should be 200'); - server.close(); - t.end(); + t.equal(res2.statusCode, 304, 'second request with a strong etag should be 304'); + + request.get({ + uri, + followRedirect: false, + headers: { 'if-modified-since': now, 'if-none-match': `W/${etag}` }, + }, (err3, res3) => { + if (err3) { + t.fail(err3); + } + + // Note that if both if-modified-since and if-none-match are + // provided, the server MUST NOT return a response status of 304 + // unless doing so is consistent with all of the conditional + // header fields in the request + // https://www.ietf.org/rfc/rfc2616.txt + t.equal(res3.statusCode, 200, 'third request with a weak etag should be 200'); + server.close(); + t.end(); + }); }); }); }); @@ -170,60 +174,61 @@ test('304_not_modified_strong_compare', (t) => { test('304_not_modified_weak_compare', (t) => { - const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4); - const file = 'c.js'; - - const server = http.createServer( - ecstatic({ - root, - gzip: true, - baseDir, - autoIndex: true, - showDir: true, - weakEtags: false, - }) - ); - - server.listen(port, () => { - const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; - const now = (new Date()).toString(); - let etag = null; - - request.get({ - uri, - followRedirect: false, - }, (err, res) => { - if (err) { - t.fail(err); - } - - t.equal(res.statusCode, 200, 'first request should be a 200'); - - etag = res.headers.etag; + portfinder.getPort((err, port) => { + const file = 'c.js'; + + const server = http.createServer( + ecstatic({ + root, + gzip: true, + baseDir, + autoIndex: true, + showDir: true, + weakEtags: false, + }) + ); + + server.listen(port, () => { + const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; + const now = (new Date()).toString(); + let etag = null; request.get({ uri, followRedirect: false, - headers: { 'if-modified-since': now, 'if-none-match': etag }, - }, (err2, res2) => { - if (err2) { - t.fail(err2); + }, (err, res) => { + if (err) { + t.fail(err); } - t.equal(res2.statusCode, 304, 'second request with a strong etag should be 304'); + t.equal(res.statusCode, 200, 'first request should be a 200'); + + etag = res.headers.etag; request.get({ uri, followRedirect: false, - headers: { 'if-modified-since': now, 'if-none-match': `W/${etag}` }, - }, (err3, res3) => { - if (err3) { - t.fail(err3); + headers: { 'if-modified-since': now, 'if-none-match': etag }, + }, (err2, res2) => { + if (err2) { + t.fail(err2); } - t.equal(res3.statusCode, 304, 'third request with a weak etag should be 304'); - server.close(); - t.end(); + t.equal(res2.statusCode, 304, 'second request with a strong etag should be 304'); + + request.get({ + uri, + followRedirect: false, + headers: { 'if-modified-since': now, 'if-none-match': `W/${etag}` }, + }, (err3, res3) => { + if (err3) { + t.fail(err3); + } + + t.equal(res3.statusCode, 304, 'third request with a weak etag should be 304'); + server.close(); + t.end(); + }); }); }); }); diff --git a/test/core-error.test.js b/test/core-error.test.js index 6f06279b7..6b82bbc1d 100644 --- a/test/core-error.test.js +++ b/test/core-error.test.js @@ -15,57 +15,58 @@ mkdirp.sync(`${root}/emptyDir`); const cases = require('./fixtures/common-cases-error'); test('core', (t) => { - const filenames = Object.keys(cases); - const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4); + require('portfinder').getPort((err, port) => { + const filenames = Object.keys(cases); - const server = http.createServer( - ecstatic({ - root, - gzip: true, - baseDir, - autoIndex: true, - showDir: true, - handleError: false, - }) - ); + const server = http.createServer( + ecstatic({ + root, + gzip: true, + baseDir, + autoIndex: true, + showDir: true, + handleError: false, + }) + ); - server.listen(port, () => { - let pending = filenames.length; - filenames.forEach((file) => { - const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; - const headers = cases[file].headers || {}; + server.listen(port, () => { + let pending = filenames.length; + filenames.forEach((file) => { + const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; + const headers = cases[file].headers || {}; - request.get({ - uri, - followRedirect: false, - headers, - }, (err, res, body) => { - if (err) { - t.fail(err); - } - const r = cases[file]; - t.equal(res.statusCode, r.code, `status code for \`${file}\``); + request.get({ + uri, + followRedirect: false, + headers, + }, (err, res, body) => { + if (err) { + t.fail(err); + } + const r = cases[file]; + t.equal(res.statusCode, r.code, `status code for \`${file}\``); - if (r.type !== undefined) { - t.equal( - res.headers['content-type'].split(';')[0], r.type, - `content-type for \`${file}\`` - ); - } + if (r.type !== undefined) { + t.equal( + res.headers['content-type'].split(';')[0], r.type, + `content-type for \`${file}\`` + ); + } - if (r.body !== undefined) { - t.equal(body, r.body, `body for \`${file}\``); - } + if (r.body !== undefined) { + t.equal(body, r.body, `body for \`${file}\``); + } - if (r.location !== undefined) { - t.equal(res.headers.location, path.join('/', baseDir, r.location), `location for \`${file}\``); - } + if (r.location !== undefined) { + t.equal(res.headers.location, path.join('/', baseDir, r.location), `location for \`${file}\``); + } - pending -= 1; - if (pending === 0) { - server.close(); - t.end(); - } + pending -= 1; + if (pending === 0) { + server.close(); + t.end(); + } + }); }); }); }); diff --git a/test/core.test.js b/test/core.test.js index d5b10572c..eca15af4d 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -16,58 +16,59 @@ mkdirp.sync(`${root}/emptyDir`); const cases = require('./fixtures/common-cases'); test('core', (t) => { - const filenames = Object.keys(cases); - const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4); + require('portfinder').getPort((err, port) => { + const filenames = Object.keys(cases); - const server = http.createServer( - ecstatic({ - root, - gzip: true, - baseDir, - autoIndex: true, - showDir: true, - defaultExt: 'html', - handleError: true, - }) - ); + const server = http.createServer( + ecstatic({ + root, + gzip: true, + baseDir, + autoIndex: true, + showDir: true, + defaultExt: 'html', + handleError: true, + }) + ); - server.listen(port, () => { - let pending = filenames.length; - filenames.forEach((file) => { - const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; - const headers = cases[file].headers || {}; + server.listen(port, () => { + let pending = filenames.length; + filenames.forEach((file) => { + const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; + const headers = cases[file].headers || {}; - request.get({ - uri, - followRedirect: false, - headers, - }, (err, res, body) => { - if (err) { - t.fail(err); - } - const r = cases[file]; - t.equal(res.statusCode, r.code, `status code for \`${file}\``); + request.get({ + uri, + followRedirect: false, + headers, + }, (err, res, body) => { + if (err) { + t.fail(err); + } + const r = cases[file]; + t.equal(res.statusCode, r.code, `status code for \`${file}\``); - if (r.type !== undefined) { - t.equal( - res.headers['content-type'].split(';')[0], r.type, - `content-type for \`${file}\`` - ); - } + if (r.type !== undefined) { + t.equal( + res.headers['content-type'].split(';')[0], r.type, + `content-type for \`${file}\`` + ); + } - if (r.body !== undefined) { - t.equal(eol.lf(body), r.body, `body for \`${file}\``); - } + if (r.body !== undefined) { + t.equal(eol.lf(body), r.body, `body for \`${file}\``); + } - if (r.location !== undefined) { - t.equal(path.normalize(res.headers.location), path.join('/', baseDir, r.location), `location for \`${file}\``); - } + if (r.location !== undefined) { + t.equal(path.normalize(res.headers.location), path.join('/', baseDir, r.location), `location for \`${file}\``); + } - pending -= 1; - if (pending === 0) { - server.close(); - t.end(); - } + pending -= 1; + if (pending === 0) { + server.close(); + t.end(); + } + }); }); }); }); diff --git a/test/express-error.test.js b/test/express-error.test.js index 2962f16fa..1129224e5 100644 --- a/test/express-error.test.js +++ b/test/express-error.test.js @@ -16,58 +16,58 @@ mkdirp.sync(`${root}/emptyDir`); const cases = require('./fixtures/common-cases-error'); test('express', (t) => { - const filenames = Object.keys(cases); - const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4); + require('portfinder').getPort((err, port) => { + const filenames = Object.keys(cases); + const app = express(); - const app = express(); + app.use(ecstatic({ + root, + gzip: true, + baseDir, + autoIndex: true, + showDir: true, + cache: 'no-cache', + handleError: false, + })); - app.use(ecstatic({ - root, - gzip: true, - baseDir, - autoIndex: true, - showDir: true, - cache: 'no-cache', - handleError: false, - })); + const server = http.createServer(app); - const server = http.createServer(app); + server.listen(port, () => { + let pending = filenames.length; + filenames.forEach((file) => { + const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; + const headers = cases[file].headers || {}; - server.listen(port, () => { - let pending = filenames.length; - filenames.forEach((file) => { - const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; - const headers = cases[file].headers || {}; + request.get({ + uri, + followRedirect: false, + headers, + }, (err, res, body) => { + if (err) t.fail(err); + const r = cases[file]; + t.equal(res.statusCode, r.code, `status code for \`${file}\``); - request.get({ - uri, - followRedirect: false, - headers, - }, (err, res, body) => { - if (err) t.fail(err); - const r = cases[file]; - t.equal(res.statusCode, r.code, `status code for \`${file}\``); + if (r.code === 200) { + t.equal(res.headers['cache-control'], 'no-cache', `cache control for \`${file}\``); + } - if (r.code === 200) { - t.equal(res.headers['cache-control'], 'no-cache', `cache control for \`${file}\``); - } + if (r.type !== undefined) { + t.equal( + res.headers['content-type'].split(';')[0], r.type, + `content-type for \`${file}\`` + ); + } - if (r.type !== undefined) { - t.equal( - res.headers['content-type'].split(';')[0], r.type, - `content-type for \`${file}\`` - ); - } + if (r.body !== undefined) { + t.equal(body, r.body, `body for \`${file}\``); + } - if (r.body !== undefined) { - t.equal(body, r.body, `body for \`${file}\``); - } - - pending -= 1; - if (pending === 0) { - server.close(); - t.end(); - } + pending -= 1; + if (pending === 0) { + server.close(); + t.end(); + } + }); }); }); }); diff --git a/test/express.test.js b/test/express.test.js index e645de5e4..62fe3f947 100644 --- a/test/express.test.js +++ b/test/express.test.js @@ -17,59 +17,60 @@ mkdirp.sync(`${root}/emptyDir`); const cases = require('./fixtures/common-cases'); test('express', (t) => { - const filenames = Object.keys(cases); - const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4); + require('portfinder').getPort((err, port) => { + const filenames = Object.keys(cases); - const app = express(); + const app = express(); - app.use(ecstatic({ - root, - gzip: true, - baseDir, - autoIndex: true, - showDir: true, - defaultExt: 'html', - cache: 'no-cache', - handleError: true, - })); + app.use(ecstatic({ + root, + gzip: true, + baseDir, + autoIndex: true, + showDir: true, + defaultExt: 'html', + cache: 'no-cache', + handleError: true, + })); - const server = http.createServer(app); + const server = http.createServer(app); - server.listen(port, () => { - let pending = filenames.length; - filenames.forEach((file) => { - const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; - const headers = cases[file].headers || {}; + server.listen(port, () => { + let pending = filenames.length; + filenames.forEach((file) => { + const uri = `http://localhost:${port}${path.join('/', baseDir, file)}`; + const headers = cases[file].headers || {}; - request.get({ - uri, - followRedirect: false, - headers, - }, (err, res, body) => { - if (err) t.fail(err); - const r = cases[file]; - t.equal(res.statusCode, r.code, `status code for \`${file}\``); + request.get({ + uri, + followRedirect: false, + headers, + }, (err, res, body) => { + if (err) t.fail(err); + const r = cases[file]; + t.equal(res.statusCode, r.code, `status code for \`${file}\``); - if (r.code === 200) { - t.equal(res.headers['cache-control'], 'no-cache', `cache control for \`${file}\``); - } + if (r.code === 200) { + t.equal(res.headers['cache-control'], 'no-cache', `cache control for \`${file}\``); + } - if (r.type !== undefined) { - t.equal( - res.headers['content-type'].split(';')[0], r.type, - `content-type for \`${file}\`` - ); - } + if (r.type !== undefined) { + t.equal( + res.headers['content-type'].split(';')[0], r.type, + `content-type for \`${file}\`` + ); + } - if (r.body !== undefined) { - t.equal(eol.lf(body), r.body, `body for \`${file}\``); - } + if (r.body !== undefined) { + t.equal(eol.lf(body), r.body, `body for \`${file}\``); + } - pending -= 1; - if (pending === 0) { - server.close(); - t.end(); - } + pending -= 1; + if (pending === 0) { + server.close(); + t.end(); + } + }); }); }); }); diff --git a/test/showdir-href-encoding.test.js b/test/showdir-href-encoding.test.js index 74f5ca7ed..70b5b9c02 100644 --- a/test/showdir-href-encoding.test.js +++ b/test/showdir-href-encoding.test.js @@ -10,25 +10,26 @@ const root = `${__dirname}/public`; const baseDir = 'base'; test('url encoding in href', (t) => { - const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4); - const uri = `http://localhost:${port}${path.join('/', baseDir, 'show-dir%24%24href_encoding%24%24')}`; + require('portfinder').getPort((err, port) => { + const uri = `http://localhost:${port}${path.join('/', baseDir, 'show-dir%24%24href_encoding%24%24')}`; - const server = http.createServer( - ecstatic({ - root, - baseDir, - showDir: true, - autoIndex: false, - }) - ); + const server = http.createServer( + ecstatic({ + root, + baseDir, + showDir: true, + autoIndex: false, + }) + ); - server.listen(port, () => { - request.get({ - uri, - }, (err, res, body) => { - t.match(body, /href="\.\/aname%2Baplus.txt"/, 'We found the right href'); - server.close(); - t.end(); + server.listen(port, () => { + request.get({ + uri, + }, (err, res, body) => { + t.match(body, /href="\.\/aname%2Baplus.txt"/, 'We found the right href'); + server.close(); + t.end(); + }); }); }); }); diff --git a/test/showdir-pathname-encoding.test.js b/test/showdir-pathname-encoding.test.js index 6e73a83d0..89122f735 100644 --- a/test/showdir-pathname-encoding.test.js +++ b/test/showdir-pathname-encoding.test.js @@ -24,27 +24,26 @@ test('create test directory', (t) => { }); test('directory listing with pathname including HTML characters', (t) => { - const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4); - - 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, //, 'We didn\'t find the unencoded pathname'); - t.match(body, /<dir>/, 'We found the encoded pathname'); - server.close(); - t.end(); + require('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, //, 'We didn\'t find the unencoded pathname'); + t.match(body, /<dir>/, 'We found the encoded pathname'); + server.close(); + t.end(); + }); }); }); }); diff --git a/test/showdir-search-encoding.test.js b/test/showdir-search-encoding.test.js index bcda82b0b..d46b6a013 100644 --- a/test/showdir-search-encoding.test.js +++ b/test/showdir-search-encoding.test.js @@ -10,26 +10,27 @@ const root = `${__dirname}/public`; const baseDir = 'base'; test('directory listing with query string specified', (t) => { - const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4); - const uri = `http://localhost:${port}${path.join('/', baseDir, '?a=1&b=2')}`; + require('portfinder').getPort((err, port) => { + const uri = `http://localhost:${port}${path.join('/', baseDir, '?a=1&b=2')}`; - const server = http.createServer( - ecstatic({ - root, - baseDir, - showDir: true, - autoIndex: false, - }) - ); + const server = http.createServer( + ecstatic({ + root, + baseDir, + showDir: true, + autoIndex: false, + }) + ); - server.listen(port, () => { - request.get({ - uri, - }, (err, res, body) => { - 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(); + server.listen(port, () => { + request.get({ + uri, + }, (err, res, body) => { + 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 870eade2d..30a1779cd 100644 --- a/test/showdir-with-spaces.test.js +++ b/test/showdir-with-spaces.test.js @@ -10,25 +10,26 @@ const root = `${__dirname}/public`; const baseDir = 'base'; test('directory listing when directory name contains spaces', (t) => { - const port = Math.floor((Math.random() * ((1 << 16) - 1e4)) + 1e4); - const uri = `http://localhost:${port}${path.join('/', baseDir, 'subdir_with%20space')}`; + require('portfinder').getPort((err, port) => { + const uri = `http://localhost:${port}${path.join('/', baseDir, 'subdir_with%20space')}`; - const server = http.createServer( - ecstatic({ - root, - baseDir, - showDir: true, - autoIndex: false, - }) - ); + const server = http.createServer( + ecstatic({ + root, + baseDir, + showDir: true, + autoIndex: false, + }) + ); - server.listen(port, () => { - request.get({ - uri, - }, (err, res, body) => { - t.ok(/href="\.\/index.html"/.test(body), 'We found the right href'); - server.close(); - t.end(); + server.listen(port, () => { + request.get({ + uri, + }, (err, res, body) => { + t.ok(/href="\.\/index.html"/.test(body), 'We found the right href'); + server.close(); + t.end(); + }); }); }); });