Created
August 25, 2011 20:16
-
-
Save brianmcd/1171770 to your computer and use it in GitHub Desktop.
test for https
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
// This is in response to issue # 280 - scripts don't load over https. | |
// See: https://github.com/tmpvar/jsdom/issues/280 | |
// | |
// When a transfer is done, HTTPS servers in the wild might emit 'close', or | |
// might emit 'end'. Node's HTTPS server always emits 'end', so we need to | |
// fake a 'close' to test this fix. | |
env_with_https : function (test) { | |
var https = require('https'); | |
// Save the real https.request so we can restore it later. | |
var oldRequest = https.request; | |
var EventEmitter = require('events').EventEmitter; | |
// Mock response object | |
var res = { setEncoding : function () {} }; | |
res.__proto__ = new EventEmitter(); | |
// Monkey patch https.request so it emits 'close' instead of 'end. | |
https.request = function () { | |
// Mock the request object. | |
var req = { setHeader : function () {} }; | |
req.__proto__ = new EventEmitter(); | |
process.nextTick(function () { | |
req.emit('response', res); | |
process.nextTick(function () { | |
res.emit('data', 'window.attachedHere = 123'); | |
res.emit('close'); | |
}); | |
}); | |
return req; | |
}; | |
jsdom.env({ | |
html: "<a href='/path/to/hello'>World</a>", | |
// The script url doesn't matter as long as its https, since our mocked | |
// request doens't actually fetch anything. | |
scripts: 'https://doesntmatter.com/script.js', | |
done: function(errors, window) { | |
if (errors) { | |
test.ok(false, errors.message) | |
} else { | |
test.notEqual(window.location, null, 'window.location should not be null'); | |
test.equal(window.attachedHere, 123, 'script should execute on our window'); | |
test.equal(window.document.getElementsByTagName("a").item(0).innerHTML, 'World', 'anchor text'); | |
} | |
https.request = oldRequest; | |
test.done(); | |
} | |
}); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment