test-headers.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // Test setRequestHeader
  9. assert.equal("Foobar", req.headers["x-test"]);
  10. // Test non-conforming allowed header
  11. assert.equal("node-XMLHttpRequest-test", req.headers["user-agent"]);
  12. // Test header set with blacklist disabled
  13. assert.equal("http://github.com", req.headers["referer"]);
  14. var body = "Hello World";
  15. res.writeHead(200, {
  16. "Content-Type": "text/plain",
  17. "Content-Length": Buffer.byteLength(body),
  18. // Set cookie headers to see if they're correctly suppressed
  19. // Actual values don't matter
  20. "Set-Cookie": "foo=bar",
  21. "Set-Cookie2": "bar=baz",
  22. "Date": "Thu, 30 Aug 2012 18:17:53 GMT",
  23. "Connection": "close"
  24. });
  25. res.write("Hello World");
  26. res.end();
  27. this.close();
  28. }).listen(8000);
  29. xhr.onreadystatechange = function() {
  30. if (this.readyState == 4) {
  31. // Test getAllResponseHeaders()
  32. var headers = "content-type: text/plain\r\ncontent-length: 11\r\ndate: Thu, 30 Aug 2012 18:17:53 GMT\r\nconnection: close";
  33. assert.equal(headers, this.getAllResponseHeaders());
  34. // Test case insensitivity
  35. assert.equal('text/plain', this.getResponseHeader('Content-Type'));
  36. assert.equal('text/plain', this.getResponseHeader('Content-type'));
  37. assert.equal('text/plain', this.getResponseHeader('content-Type'));
  38. assert.equal('text/plain', this.getResponseHeader('content-type'));
  39. // Test aborted getAllResponseHeaders
  40. this.abort();
  41. assert.equal("", this.getAllResponseHeaders());
  42. assert.equal(null, this.getResponseHeader("Connection"));
  43. sys.puts("done");
  44. }
  45. };
  46. assert.equal(null, xhr.getResponseHeader("Content-Type"));
  47. try {
  48. xhr.open("GET", "http://localhost:8000/");
  49. // Valid header
  50. xhr.setRequestHeader("X-Test", "Foobar");
  51. // Invalid header
  52. xhr.setRequestHeader("Content-Length", 0);
  53. // Allowed header outside of specs
  54. xhr.setRequestHeader("user-agent", "node-XMLHttpRequest-test");
  55. // Test getRequestHeader
  56. assert.equal("Foobar", xhr.getRequestHeader("X-Test"));
  57. // Test invalid header
  58. assert.equal("", xhr.getRequestHeader("Content-Length"));
  59. // Test allowing all headers
  60. xhr.setDisableHeaderCheck(true);
  61. xhr.setRequestHeader("Referer", "http://github.com");
  62. assert.equal("http://github.com", xhr.getRequestHeader("Referer"));
  63. xhr.send();
  64. } catch(e) {
  65. console.log("ERROR: Exception raised", e);
  66. }