From df80728366f164e38bad13a5d7f3055f00a0799b Mon Sep 17 00:00:00 2001 From: Adam Hobaugh Date: Fri, 3 Feb 2017 19:13:37 -0500 Subject: [PATCH] Made it so that you can send an argument to send that allows the user to ignore an invalid certificate. This is so you can write apps to connect to self signed cert servers. --- lib/XMLHttpRequest.js | 7 ++- tests/test-request-methods-https-invalid.js | 68 +++++++++++++++++++++ tests/test-request-methods-https.js | 62 +++++++++++++++++++ 3 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 tests/test-request-methods-https-invalid.js create mode 100644 tests/test-request-methods-https.js diff --git a/lib/XMLHttpRequest.js b/lib/XMLHttpRequest.js index 9066a37..eca6362 100644 --- a/lib/XMLHttpRequest.js +++ b/lib/XMLHttpRequest.js @@ -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); @@ -379,6 +380,7 @@ exports.XMLHttpRequest = function() { method: settings.method, headers: headers, agent: false, + rejectUnauthorized: settings.rejectUnauthorized, withCredentials: self.withCredentials }; @@ -416,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-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]);