test-events.js (1241B)
1 var sys = require("util") 2 , assert = require("assert") 3 , http = require("http") 4 , XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest 5 , xhr; 6 7 // Test server 8 var server = http.createServer(function (req, res) { 9 var body = (req.method != "HEAD" ? "Hello World" : ""); 10 11 res.writeHead(200, { 12 "Content-Type": "text/plain", 13 "Content-Length": Buffer.byteLength(body) 14 }); 15 // HEAD has no body 16 if (req.method != "HEAD") { 17 res.write(body); 18 } 19 res.end(); 20 assert.equal(onreadystatechange, true); 21 assert.equal(readystatechange, true); 22 assert.equal(removed, true); 23 sys.puts("done"); 24 this.close(); 25 }).listen(8000); 26 27 xhr = new XMLHttpRequest(); 28 29 // Track event calls 30 var onreadystatechange = false; 31 var readystatechange = false; 32 var removed = true; 33 var removedEvent = function() { 34 removed = false; 35 }; 36 37 xhr.onreadystatechange = function() { 38 onreadystatechange = true; 39 }; 40 41 xhr.addEventListener("readystatechange", function() { 42 readystatechange = true; 43 }); 44 45 // This isn't perfect, won't guarantee it was added in the first place 46 xhr.addEventListener("readystatechange", removedEvent); 47 xhr.removeEventListener("readystatechange", removedEvent); 48 49 xhr.open("GET", "http://localhost:8000"); 50 xhr.send();