From dd2a0c23cf7f2165b4dc43266d76be7d8347c55a Mon Sep 17 00:00:00 2001 From: Adam Hobaugh Date: Fri, 3 Feb 2017 10:21:54 -0500 Subject: [PATCH 1/4] updated to deal with sys.puts being deprecated --- tests/test-constants.js | 2 +- tests/test-events.js | 2 +- tests/test-headers.js | 2 +- tests/test-redirect-301.js | 2 +- tests/test-redirect-302.js | 2 +- tests/test-redirect-303.js | 2 +- tests/test-redirect-307.js | 2 +- tests/test-request-methods.js | 6 +++--- tests/test-request-protocols.js | 2 +- tests/test-streaming.js | 4 ++-- 10 files changed, 13 insertions(+), 13 deletions(-) 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-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.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() { From dd278d3208ab7685e80ff47054c6158130c37f01 Mon Sep 17 00:00:00 2001 From: Adam Hobaugh Date: Fri, 3 Feb 2017 10:50:47 -0500 Subject: [PATCH 2/4] Updated the setRequestHeader to be directly testable and updated the tests to match the behavior set in the test-headers.js file --- lib/XMLHttpRequest.js | 5 +++-- tests/test-exceptions.js | 5 ++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/XMLHttpRequest.js b/lib/XMLHttpRequest.js index 4893913..9066a37 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; @@ -195,7 +195,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 +203,7 @@ exports.XMLHttpRequest = function() { header = headersCase[header.toLowerCase()] || header; headersCase[header.toLowerCase()] = header; headers[header] = headers[header] ? headers[header] + ', ' + value : value; + return true; }; /** 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); } } From e2ae9aa96f35563c56eb71c6d2530f0232aca817 Mon Sep 17 00:00:00 2001 From: Adam Hobaugh Date: Fri, 3 Feb 2017 19:10:44 -0500 Subject: [PATCH 3/4] added pem files to gitignore so that certs aren't in the repo --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa7502d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tests/*.pem From 70faa2847d052320ce59c254090942a797b867bb Mon Sep 17 00:00:00 2001 From: Adam Hobaugh Date: Fri, 3 Feb 2017 19:13:37 -0500 Subject: [PATCH 4/4] Made it so that you can send an argument to open 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]);