test-redirect-302.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. var sys = require("util")
  2. , assert = require("assert")
  3. , XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest
  4. , xhr = new XMLHttpRequest()
  5. , http = require("http");
  6. // Test server
  7. var server = http.createServer(function (req, res) {
  8. if (req.url === '/redirectingResource') {
  9. res.writeHead(302, {'Location': 'http://localhost:8000/'});
  10. res.end();
  11. return;
  12. }
  13. var body = "Hello World";
  14. res.writeHead(200, {
  15. "Content-Type": "text/plain",
  16. "Content-Length": Buffer.byteLength(body),
  17. "Date": "Thu, 30 Aug 2012 18:17:53 GMT",
  18. "Connection": "close"
  19. });
  20. res.write("Hello World");
  21. res.end();
  22. this.close();
  23. }).listen(8000);
  24. xhr.onreadystatechange = function() {
  25. if (this.readyState == 4) {
  26. assert.equal(xhr.getRequestHeader('Location'), '');
  27. assert.equal(xhr.responseText, "Hello World");
  28. sys.puts("done");
  29. }
  30. };
  31. try {
  32. xhr.open("GET", "http://localhost:8000/redirectingResource");
  33. xhr.send();
  34. } catch(e) {
  35. console.log("ERROR: Exception raised", e);
  36. }