Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated deprecated sys.puts and added invalid ssl support #141

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/*.pem
12 changes: 8 additions & 4 deletions lib/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down 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 @@ -195,14 +196,15 @@ 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");
}
header = headersCase[header.toLowerCase()] || header;
headersCase[header.toLowerCase()] = header;
headers[header] = headers[header] ? headers[header] + ', ' + value : value;
return true;
};

/**
Expand Down Expand Up @@ -378,6 +380,7 @@ exports.XMLHttpRequest = function() {
method: settings.method,
headers: headers,
agent: false,
rejectUnauthorized: settings.rejectUnauthorized,
withCredentials: self.withCredentials
};

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

Expand Down
2 changes: 1 addition & 1 deletion tests/test-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
2 changes: 1 addition & 1 deletion tests/test-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 2 additions & 3 deletions tests/test-exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ xhr.onreadystatechange = function() {
assert.equal("", this.getAllResponseHeaders());
assert.equal(null, this.getResponseHeader("Connection"));

sys.puts("done");
console.log("done");
}
};

Expand Down
2 changes: 1 addition & 1 deletion tests/test-redirect-301.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
};

Expand Down
2 changes: 1 addition & 1 deletion tests/test-redirect-302.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
};

Expand Down
2 changes: 1 addition & 1 deletion tests/test-redirect-303.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
};

Expand Down
2 changes: 1 addition & 1 deletion tests/test-redirect-307.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
};

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]);
6 changes: 3 additions & 3 deletions tests/test-request-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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]);
}
}
Expand All @@ -58,5 +58,5 @@ function start(method) {
xhr.send();
}

sys.puts("Testing " + methods[curMethod]);
console.log("Testing " + methods[curMethod]);
start(methods[curMethod]);
2 changes: 1 addition & 1 deletion tests/test-request-protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/test-streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down