Skip to content

Commit

Permalink
Made it so that you can send an argument to send that allows the user…
Browse files Browse the repository at this point in the history
… to ignore an invalid certificate. This is so you can write apps to connect to self signed cert servers.
  • Loading branch information
greenmanspirit committed Feb 4, 2017
1 parent e2ae9aa commit df80728
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -379,6 +380,7 @@ exports.XMLHttpRequest = function() {
method: settings.method,
headers: headers,
agent: false,
rejectUnauthorized: settings.rejectUnauthorized,
withCredentials: self.withCredentials
};

Expand Down Expand Up @@ -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
};

Expand Down
68 changes: 68 additions & 0 deletions tests/test-request-methods-https-invalid.js
Original file line number Diff line number Diff line change
@@ -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]);
62 changes: 62 additions & 0 deletions tests/test-request-methods-https.js
Original file line number Diff line number Diff line change
@@ -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]);

0 comments on commit df80728

Please sign in to comment.