From 87f1e7b1e8e2a436a1af2e208420af32a3a8389d Mon Sep 17 00:00:00 2001 From: Richard Marmorstein Date: Thu, 24 Jun 2021 22:58:55 -0400 Subject: [PATCH] Manually close the server --- test/StripeResource.spec.js | 74 +++++++++++++++++++++---------------- testUtils/index.js | 6 ++- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/test/StripeResource.spec.js b/test/StripeResource.spec.js index c15e5d3fca..18b1f2a585 100644 --- a/test/StripeResource.spec.js +++ b/test/StripeResource.spec.js @@ -195,12 +195,13 @@ describe('StripeResource', () => { (req, res) => { // Do nothing. This will trigger a timeout. }, - (err, stripe) => { + (err, stripe, closeServer) => { if (err) { return done(err); } stripe.charges.create(options.data, (err, result) => { expect(err.detail.message).to.deep.equal('ETIMEDOUT'); + closeServer(); done(); }); } @@ -216,13 +217,14 @@ describe('StripeResource', () => { // Do nothing. This will trigger a timeout. return {shouldStayOpen: nRequestsReceived < 3}; }, - (err, stripe) => { + (err, stripe, closeServer) => { if (err) { return done(err); } stripe.charges.create(options.data, (err, result) => { expect(err.detail.message).to.deep.equal('ETIMEDOUT'); expect(nRequestsReceived).to.equal(3); + closeServer(); done(); }); } @@ -665,27 +667,32 @@ describe('StripeResource', () => { setTimeout(() => res.end(), 40); }; - testUtils.getTestServerStripe({}, handleRequest, (err, stripe) => { - const foos = makeResourceWithPDFMethod(stripe); - if (err) { - return callback(err); - } - - return foos.pdf({id: 'foo_123'}, {host: 'localhost'}, (err, res) => { + testUtils.getTestServerStripe( + {}, + handleRequest, + (err, stripe, closeServer) => { + const foos = makeResourceWithPDFMethod(stripe); if (err) { return callback(err); } - const chunks = []; - res.on('data', (chunk) => chunks.push(chunk)); - res.on('error', callback); - res.on('end', () => { - expect(Buffer.concat(chunks).toString()).to.equal( - 'pretend this is a pdf' - ); - return callback(); + + return foos.pdf({id: 'foo_123'}, {host: 'localhost'}, (err, res) => { + closeServer(); + if (err) { + return callback(err); + } + const chunks = []; + res.on('data', (chunk) => chunks.push(chunk)); + res.on('error', callback); + res.on('end', () => { + expect(Buffer.concat(chunks).toString()).to.equal( + 'pretend this is a pdf' + ); + return callback(); + }); }); - }); - }); + } + ); }); it('failure', (callback) => { @@ -701,20 +708,25 @@ describe('StripeResource', () => { setTimeout(() => res.end(), 20); }; - testUtils.getTestServerStripe({}, handleRequest, (err, stripe) => { - if (err) { - return callback(err); - } + testUtils.getTestServerStripe( + {}, + handleRequest, + (err, stripe, closeServer) => { + if (err) { + return callback(err); + } - const foos = makeResourceWithPDFMethod(stripe); + const foos = makeResourceWithPDFMethod(stripe); - return foos.pdf({id: 'foo_123'}, {host: 'localhost'}, (err, res) => { - expect(err).to.exist; - expect(err.raw.type).to.equal('api_error'); - expect(err.raw.message).to.equal('this is bad'); - return callback(); - }); - }); + return foos.pdf({id: 'foo_123'}, {host: 'localhost'}, (err, res) => { + closeServer(); + expect(err).to.exist; + expect(err.raw.type).to.equal('api_error'); + expect(err.raw.message).to.equal('this is bad'); + return callback(); + }); + } + ); }); }); }); diff --git a/testUtils/index.js b/testUtils/index.js index 9cfa57fa90..e230262334 100644 --- a/testUtils/index.js +++ b/testUtils/index.js @@ -15,7 +15,7 @@ const utils = (module.exports = { const server = http.createServer((req, res) => { const {shouldStayOpen} = handler(req, res) || {}; if (!shouldStayOpen) { - res.on('finish', () => { + res.on('close', () => { server.close(); }); } @@ -31,7 +31,9 @@ const utils = (module.exports = { ...clientOptions, } ); - return callback(null, stripe); + return callback(null, stripe, () => { + server.close(); + }); }); },