test-events.js 1.2 KB

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