From e522f2c38cc9f38145f5c6ac0bc8daa356f92125 Mon Sep 17 00:00:00 2001 From: Ilya Priven Date: Mon, 28 Oct 2024 11:12:05 +0000 Subject: [PATCH] fix: pick ephemeral port once per agent --- index.js | 4 ++-- lib/agent.js | 4 ++-- test/supertest.js | 15 +++++++++------ 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 8db8e43..6e610f1 100644 --- a/index.js +++ b/index.js @@ -32,9 +32,9 @@ module.exports = function(app, options = {}) { 'supertest: this version of Node.js does not support http2' ); } - app = http2.createServer(app); // eslint-disable-line no-param-reassign + app = http2.createServer(app).listen(0); // eslint-disable-line no-param-reassign } else { - app = http.createServer(app); // eslint-disable-line no-param-reassign + app = http.createServer(app).listen(0); // eslint-disable-line no-param-reassign } } diff --git a/lib/agent.js b/lib/agent.js index 6e4e7d8..956c7ca 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -38,9 +38,9 @@ function TestAgent(app, options = {}) { 'supertest: this version of Node.js does not support http2' ); } - app = http2.createServer(app); // eslint-disable-line no-param-reassign + app = http2.createServer(app).listen(0); // eslint-disable-line no-param-reassign } else { - app = http.createServer(app); // eslint-disable-line no-param-reassign + app = http.createServer(app).listen(0); // eslint-disable-line no-param-reassign } } this.app = app; diff --git a/test/supertest.js b/test/supertest.js index 9eebf31..04a1e0c 100644 --- a/test/supertest.js +++ b/test/supertest.js @@ -1390,13 +1390,16 @@ describeHttp2('http2', function() { res.end('hey'); }; - it('should fire up the app on an ephemeral port', function (done) { - api(app, { http2: true }) + it('should fire up the app on a single ephemeral port', function (done) { + const agent = api(app, { http2: true }); + agent .get('/') - .end(function (err, res) { - res.status.should.equal(200); - res.text.should.equal('hey'); - done(); + .end(function (err1, res1) { + res1.request.url.should.match(/^http:\/\/127.0.0.1:(\d+)\/$/); + agent.get('/').end(function (err2, res2) { + res2.request.url.should.equal(res1.request.url); + done(); + }); }); });