diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa7502d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tests/*.pem diff --git a/lib/XMLHttpRequest.js b/lib/XMLHttpRequest.js index 4893913..eca6362 100644 --- a/lib/XMLHttpRequest.js +++ b/lib/XMLHttpRequest.js @@ -111,7 +111,7 @@ exports.XMLHttpRequest = function() { this.responseXML = ""; this.status = null; this.statusText = null; - + // Whether cross-site Access-Control requests should be made using // credentials such as cookies or authorization headers this.withCredentials = false; @@ -153,7 +153,7 @@ exports.XMLHttpRequest = function() { * @param string user Username for basic authentication (optional) * @param string password Password for basic authentication (optional) */ - this.open = function(method, url, async, user, password) { + this.open = function(method, url, async, user, password, rejectUnauthorized) { this.abort(); errorFlag = false; @@ -167,7 +167,8 @@ exports.XMLHttpRequest = function() { "url": url.toString(), "async": (typeof async !== "boolean" ? true : async), "user": user || null, - "password": password || null + "password": password || null, + "rejectUnauthorized": (typeof rejectUnauthorized !== "boolean" ? true : rejectUnauthorized) }; setState(this.OPENED); @@ -195,7 +196,7 @@ exports.XMLHttpRequest = function() { } if (!isAllowedHttpHeader(header)) { console.warn("Refused to set unsafe header \"" + header + "\""); - return; + return false; } if (sendFlag) { throw new Error("INVALID_STATE_ERR: send flag is true"); @@ -203,6 +204,7 @@ exports.XMLHttpRequest = function() { header = headersCase[header.toLowerCase()] || header; headersCase[header.toLowerCase()] = header; headers[header] = headers[header] ? headers[header] + ', ' + value : value; + return true; }; /** @@ -378,6 +380,7 @@ exports.XMLHttpRequest = function() { method: settings.method, headers: headers, agent: false, + rejectUnauthorized: settings.rejectUnauthorized, withCredentials: self.withCredentials }; @@ -415,6 +418,7 @@ exports.XMLHttpRequest = function() { path: url.path, method: response.statusCode === 303 ? "GET" : settings.method, headers: headers, + rejectUnauthorized: settings.rejectUnauthorized, withCredentials: self.withCredentials }; diff --git a/tests/test-constants.js b/tests/test-constants.js index 372e46c..4c697e2 100644 --- a/tests/test-constants.js +++ b/tests/test-constants.js @@ -10,4 +10,4 @@ assert.equal(2, xhr.HEADERS_RECEIVED); assert.equal(3, xhr.LOADING); assert.equal(4, xhr.DONE); -sys.puts("done"); +console.log("done"); diff --git a/tests/test-events.js b/tests/test-events.js index c72f001..3296f8b 100644 --- a/tests/test-events.js +++ b/tests/test-events.js @@ -20,7 +20,7 @@ var server = http.createServer(function (req, res) { assert.equal(onreadystatechange, true); assert.equal(readystatechange, true); assert.equal(removed, true); - sys.puts("done"); + console.log("done"); this.close(); }).listen(8000); diff --git a/tests/test-exceptions.js b/tests/test-exceptions.js index f1edd71..88eb137 100644 --- a/tests/test-exceptions.js +++ b/tests/test-exceptions.js @@ -44,15 +44,14 @@ var forbiddenRequestHeaders = [ "trailer", "transfer-encoding", "upgrade", - "user-agent", "via" ]; for (var i in forbiddenRequestHeaders) { try { - xhr.setRequestHeader(forbiddenRequestHeaders[i], "Test"); - console.log("ERROR: " + forbiddenRequestHeaders[i] + " should have thrown exception"); + assert.equal(xhr.setRequestHeader(forbiddenRequestHeaders[i], "Test"), false); } catch(e) { + console.log("ERROR: Exception raised", e); } } diff --git a/tests/test-headers.js b/tests/test-headers.js index 23a419e..29d4d78 100644 --- a/tests/test-headers.js +++ b/tests/test-headers.js @@ -47,7 +47,7 @@ xhr.onreadystatechange = function() { assert.equal("", this.getAllResponseHeaders()); assert.equal(null, this.getResponseHeader("Connection")); - sys.puts("done"); + console.log("done"); } }; diff --git a/tests/test-redirect-301.js b/tests/test-redirect-301.js index 4d844c1..be93651 100644 --- a/tests/test-redirect-301.js +++ b/tests/test-redirect-301.js @@ -30,7 +30,7 @@ xhr.onreadystatechange = function() { assert.equal(xhr.status, 200); assert.equal(xhr.getRequestHeader('Location'), ''); assert.equal(xhr.responseText, "Hello World"); - sys.puts("done"); + console.log("done"); } }; diff --git a/tests/test-redirect-302.js b/tests/test-redirect-302.js index d884f78..0b87192 100644 --- a/tests/test-redirect-302.js +++ b/tests/test-redirect-302.js @@ -29,7 +29,7 @@ xhr.onreadystatechange = function() { if (this.readyState == 4) { assert.equal(xhr.getRequestHeader('Location'), ''); assert.equal(xhr.responseText, "Hello World"); - sys.puts("done"); + console.log("done"); } }; diff --git a/tests/test-redirect-303.js b/tests/test-redirect-303.js index 60d9343..aa85b2d 100644 --- a/tests/test-redirect-303.js +++ b/tests/test-redirect-303.js @@ -29,7 +29,7 @@ xhr.onreadystatechange = function() { if (this.readyState == 4) { assert.equal(xhr.getRequestHeader('Location'), ''); assert.equal(xhr.responseText, "Hello World"); - sys.puts("done"); + console.log("done"); } }; diff --git a/tests/test-redirect-307.js b/tests/test-redirect-307.js index 3abc906..a73819f 100644 --- a/tests/test-redirect-307.js +++ b/tests/test-redirect-307.js @@ -31,7 +31,7 @@ xhr.onreadystatechange = function() { if (this.readyState == 4) { assert.equal(xhr.getRequestHeader('Location'), ''); assert.equal(xhr.responseText, "Hello World"); - sys.puts("done"); + console.log("done"); } }; diff --git a/tests/test-request-methods-https-invalid.js b/tests/test-request-methods-https-invalid.js new file mode 100644 index 0000000..71bf0d9 --- /dev/null +++ b/tests/test-request-methods-https-invalid.js @@ -0,0 +1,68 @@ +var sys = require("util") + , assert = require("assert") + , XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest + , https = require("https") + , fs = require('fs') + , xhr; + +var options = { + key: fs.readFileSync('key.pem'), + cert: fs.readFileSync('cert.pem') +}; + +// Test server +var server = https.createServer(options, function (req, res) { + // Check request method and URL + assert.equal(methods[curMethod], req.method); + assert.equal("/" + methods[curMethod], req.url); + + var body = (req.method != "HEAD" ? "Hello World" : ""); + + res.writeHead(200, { + "Content-Type": "text/plain", + "Content-Length": Buffer.byteLength(body) + }); + // HEAD has no body + if (req.method != "HEAD") { + res.write(body); + } + res.end(); + + if (curMethod == methods.length - 1) { + this.close(); + console.log("done"); + } +}).listen(8000); + +// Test standard methods +var methods = ["GET", "POST", "HEAD", "PUT", "DELETE"]; +var curMethod = 0; + +function start(method) { + // Reset each time + xhr = new XMLHttpRequest(); + + xhr.onreadystatechange = function() { + if (this.readyState == 4) { + if (method == "HEAD") { + assert.equal("", this.responseText); + } else { + assert.equal("Hello World", this.responseText); + } + + curMethod++; + + if (curMethod < methods.length) { + console.log("Testing " + methods[curMethod]); + start(methods[curMethod]); + } + } + }; + + var url = "https://localhost:8000/" + method; + xhr.open(method, url, true, 'foo', 'bar', false); + xhr.send(); +} + +console.log("Testing " + methods[curMethod]); +start(methods[curMethod]); diff --git a/tests/test-request-methods-https.js b/tests/test-request-methods-https.js new file mode 100644 index 0000000..07c1421 --- /dev/null +++ b/tests/test-request-methods-https.js @@ -0,0 +1,62 @@ +var sys = require("util") + , assert = require("assert") + , XMLHttpRequest = require("../lib/XMLHttpRequest").XMLHttpRequest + , https = require("https") + , fs = require('fs') + , xhr; + +var options = { + key: fs.readFileSync('key.pem'), + cert: fs.readFileSync('cert.pem') +}; + +// Test server +var server = https.createServer(options, function (req, res) { + // Check request method and URL + assert.equal(methods[curMethod], req.method); + assert.equal("/" + methods[curMethod], req.url); + + var body = (req.method != "HEAD" ? "Hello World" : ""); + + res.writeHead(200, { + "Content-Type": "text/plain", + "Content-Length": Buffer.byteLength(body) + }); + // HEAD has no body + if (req.method != "HEAD") { + res.write(body); + } + res.end(); +}).listen(8000); + +// Test standard methods +var methods = ["GET", "POST", "HEAD", "PUT", "DELETE"]; +var curMethod = 0; + +function start(method) { + // Reset each time + xhr = new XMLHttpRequest(); + + xhr.onreadystatechange = function() { + if (this.readyState == 4) { + assert.equal(this.responseText.indexOf("Error: self") > -1, true); + + curMethod++; + + if (curMethod < methods.length) { + console.log("Testing " + methods[curMethod]); + start(methods[curMethod]); + } else { + server.close(); + console.log('done'); + } + } + }; + + var url = "https://localhost:8000/" + method; + xhr.open(method, url); + xhr.send(); +} + +console.log("Testing " + methods[curMethod]); +start(methods[curMethod]); diff --git a/tests/test-request-methods.js b/tests/test-request-methods.js index fa1b1be..f8d66d8 100644 --- a/tests/test-request-methods.js +++ b/tests/test-request-methods.js @@ -24,7 +24,7 @@ var server = http.createServer(function (req, res) { if (curMethod == methods.length - 1) { this.close(); - sys.puts("done"); + console.log("done"); } }).listen(8000); @@ -47,7 +47,7 @@ function start(method) { curMethod++; if (curMethod < methods.length) { - sys.puts("Testing " + methods[curMethod]); + console.log("Testing " + methods[curMethod]); start(methods[curMethod]); } } @@ -58,5 +58,5 @@ function start(method) { xhr.send(); } -sys.puts("Testing " + methods[curMethod]); +console.log("Testing " + methods[curMethod]); start(methods[curMethod]); diff --git a/tests/test-request-protocols.js b/tests/test-request-protocols.js index 543917d..f745cb8 100644 --- a/tests/test-request-protocols.js +++ b/tests/test-request-protocols.js @@ -24,7 +24,7 @@ var runSync = function() { xhr.onreadystatechange = function() { if (this.readyState == 4) { assert.equal("Hello World", this.responseText); - sys.puts("done"); + console.log("done"); } }; xhr.open("GET", url, false); diff --git a/tests/test-streaming.js b/tests/test-streaming.js index ea0fb82..5d02a18 100644 --- a/tests/test-streaming.js +++ b/tests/test-streaming.js @@ -12,7 +12,7 @@ function completeResponse(res,server,body) { assert.equal(readystatechange, true); assert.equal(removed, true); assert.equal(loadCount, body.length); - sys.puts("done"); + console.log("done"); server.close(); } function push(res,piece) { @@ -26,7 +26,7 @@ var server = http.createServer(function (req, res) { "Content-Type": "text/plain", "Content-Length": Buffer.byteLength(body.join("")) }); - + var nextPiece = 0; var self = this; var interval = setInterval(function() {