Skip to content

Instantly share code, notes, and snippets.

@leedm777
Created March 17, 2015 21:04
Show Gist options
  • Save leedm777/226ef34e44340990b0d0 to your computer and use it in GitHub Desktop.
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
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