Skip to content

Commit

Permalink
Manually close the server
Browse files Browse the repository at this point in the history
  • Loading branch information
richardm-stripe committed Jun 25, 2021
1 parent a859519 commit 87f1e7b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
74 changes: 43 additions & 31 deletions test/StripeResource.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand All @@ -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();
});
}
Expand Down Expand Up @@ -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) => {
Expand All @@ -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();
});
}
);
});
});
});
6 changes: 4 additions & 2 deletions testUtils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand All @@ -31,7 +31,9 @@ const utils = (module.exports = {
...clientOptions,
}
);
return callback(null, stripe);
return callback(null, stripe, () => {
server.close();
});
});
},

Expand Down

0 comments on commit 87f1e7b

Please sign in to comment.