-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathtest-request-methods-https.js
More file actions
62 lines (51 loc) · 1.46 KB
/
test-request-methods-https.js
File metadata and controls
62 lines (51 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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]);