Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
test: refactoring test with common.mustCall
  • Loading branch information
weewey committed Apr 28, 2017
commit 4ca46bb266e1d214c1d6045fcb5410ab7bf63285
25 changes: 7 additions & 18 deletions test/parallel/test-https-simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,6 @@ const options = {
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
};

const tests = 2;
let successful = 0;

const testSucceeded = function() {
successful = successful + 1;
if (successful === tests) {
server.close();
}
};

const body = 'hello world\n';

const serverCallback = common.mustCall(function(req, res) {
Expand All @@ -55,7 +45,7 @@ const serverCallback = common.mustCall(function(req, res) {

const server = https.createServer(options, serverCallback);

server.listen(0, function() {
server.listen(0, function(){
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the space back. Does this pass make jslint?

// Do a request ignoring the unauthorized server certs
const noCertCheckOptions = {
hostname: '127.0.0.1',
Expand All @@ -66,16 +56,15 @@ server.listen(0, function() {
};
noCertCheckOptions.Agent = new https.Agent(noCertCheckOptions);

const req = https.request(noCertCheckOptions, function(res) {
const req = https.request(noCertCheckOptions, function(res){
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here.

let responseBody = '';
res.on('data', function(d) {
responseBody = responseBody + d;
});

res.on('end', function() {
res.on('end', common.mustCall(() => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The enclosing https.request() and server.listen() callbacks need to have common.mustCall() too, or there is no guarantee that this will execute.

assert.strictEqual(responseBody, body);
testSucceeded();
});
}));
});
req.end();

Expand Down Expand Up @@ -108,6 +97,6 @@ server.listen(0, function() {
});
});

process.on('exit', function() {
assert.strictEqual(successful, tests);
});
function testSucceeded (){
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function testSucceeded() {

server.close();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is only called from one place, you can just inline the server.close() there.

};