Created
March 17, 2015 21:04
-
-
Save leedm777/226ef34e44340990b0d0 to your computer and use it in GitHub Desktop.
Really ugly code to test whether a namespace is maintained in http callbacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var cls = require('continuation-local-storage'); | |
var ns = cls.createNamespace('test'); | |
// Really ugly code to test whether a namespace is maintained in http callbacks | |
function testURL(url) { | |
ns.run(function () { | |
var request = require('http'); | |
ns.set('url', url); | |
request.get(url, function (res) { | |
if (ns.get('url') === url) { | |
console.log("pass", url, res.statusCode); | |
} else { | |
console.error("FAIL", url, res.statusCode); | |
} | |
res.on('data', function() { | |
if (ns.get('url') === url) { | |
console.log("pass", url, 'data'); | |
} else { | |
console.error("FAIL", url, 'data'); | |
} | |
}); | |
res.on('end', function() { | |
if (ns.get('url') === url) { | |
console.log("pass", url, 'end'); | |
} else { | |
console.error("FAIL", url, 'end'); | |
} | |
}); | |
}).on('error', function(e) { | |
if (ns.get('url') === url) { | |
console.log("pass", url, e); | |
} else { | |
console.error("FAIL", url, e); | |
} | |
}); | |
}); | |
} | |
// successful URL | |
testURL('http://ifconfig.co/'); | |
// invalid domain name | |
testURL('http://dne.test'); | |
// closed port | |
testURL("http://localhost:80628"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment