rob@77: var sys = require("util") rob@77: , assert = require("assert") rob@77: , XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest rob@77: , xhr = new XMLHttpRequest() rob@77: , http = require("http"); rob@77: rob@77: // Test server rob@77: var server = http.createServer(function (req, res) { rob@77: // Test setRequestHeader rob@77: assert.equal("Foobar", req.headers["x-test"]); rob@77: // Test non-conforming allowed header rob@77: assert.equal("node-XMLHttpRequest-test", req.headers["user-agent"]); rob@77: // Test header set with blacklist disabled rob@77: assert.equal("http://github.com", req.headers["referer"]); rob@77: rob@77: var body = "Hello World"; rob@77: res.writeHead(200, { rob@77: "Content-Type": "text/plain", rob@77: "Content-Length": Buffer.byteLength(body), rob@77: // Set cookie headers to see if they're correctly suppressed rob@77: // Actual values don't matter rob@77: "Set-Cookie": "foo=bar", rob@77: "Set-Cookie2": "bar=baz", rob@77: "Date": "Thu, 30 Aug 2012 18:17:53 GMT", rob@77: "Connection": "close" rob@77: }); rob@77: res.write("Hello World"); rob@77: res.end(); rob@77: rob@77: this.close(); rob@77: }).listen(8000); rob@77: rob@77: xhr.onreadystatechange = function() { rob@77: if (this.readyState == 4) { rob@77: // Test getAllResponseHeaders() rob@77: var headers = "content-type: text/plain\r\ncontent-length: 11\r\ndate: Thu, 30 Aug 2012 18:17:53 GMT\r\nconnection: close"; rob@77: assert.equal(headers, this.getAllResponseHeaders()); rob@77: rob@77: // Test case insensitivity rob@77: assert.equal('text/plain', this.getResponseHeader('Content-Type')); rob@77: assert.equal('text/plain', this.getResponseHeader('Content-type')); rob@77: assert.equal('text/plain', this.getResponseHeader('content-Type')); rob@77: assert.equal('text/plain', this.getResponseHeader('content-type')); rob@77: rob@77: // Test aborted getAllResponseHeaders rob@77: this.abort(); rob@77: assert.equal("", this.getAllResponseHeaders()); rob@77: assert.equal(null, this.getResponseHeader("Connection")); rob@77: rob@77: sys.puts("done"); rob@77: } rob@77: }; rob@77: rob@77: assert.equal(null, xhr.getResponseHeader("Content-Type")); rob@77: try { rob@77: xhr.open("GET", "http://localhost:8000/"); rob@77: // Valid header rob@77: xhr.setRequestHeader("X-Test", "Foobar"); rob@77: // Invalid header rob@77: xhr.setRequestHeader("Content-Length", 0); rob@77: // Allowed header outside of specs rob@77: xhr.setRequestHeader("user-agent", "node-XMLHttpRequest-test"); rob@77: // Test getRequestHeader rob@77: assert.equal("Foobar", xhr.getRequestHeader("X-Test")); rob@77: // Test invalid header rob@77: assert.equal("", xhr.getRequestHeader("Content-Length")); rob@77: rob@77: // Test allowing all headers rob@77: xhr.setDisableHeaderCheck(true); rob@77: xhr.setRequestHeader("Referer", "http://github.com"); rob@77: assert.equal("http://github.com", xhr.getRequestHeader("Referer")); rob@77: rob@77: xhr.send(); rob@77: } catch(e) { rob@77: console.log("ERROR: Exception raised", e); rob@77: }