mirror of https://github.com/lukechilds/node.git
Rhys Jones
15 years ago
2 changed files with 110 additions and 83 deletions
@ -1,110 +1,135 @@ |
|||||
require("../common"); |
require("../common"); |
||||
var http = require("http"); |
net = require("net"); |
||||
var url = require("url"); |
http = require("http"); |
||||
|
url = require("url"); |
||||
|
qs = require("querystring"); |
||||
var fs = require('fs'); |
var fs = require('fs'); |
||||
|
var sys = require('sys'); |
||||
|
|
||||
HOST = "localhost"; |
var have_openssl; |
||||
|
|
||||
var have_tls; |
|
||||
try { |
try { |
||||
var dummy_server = http.createServer(); |
var crypto = require('crypto'); |
||||
|
var dummy_server = http.createServer(function(){}); |
||||
dummy_server.setSecure(); |
dummy_server.setSecure(); |
||||
have_tls=true; |
have_openssl=true; |
||||
} catch (e) { |
} catch (e) { |
||||
have_tls=false; |
have_openssl=false; |
||||
puts("Not compiled with TLS support."); |
puts("Not compiled with OPENSSL support."); |
||||
process.exit(); |
process.exit(); |
||||
} |
} |
||||
|
|
||||
|
var request_number = 0; |
||||
var responses_sent = 0; |
var requests_sent = 0; |
||||
var responses_recvd = 0; |
var server_response = ""; |
||||
var body0 = ""; |
var client_got_eof = false; |
||||
var body1 = ""; |
|
||||
var caPem = fs.readFileSync(fixturesDir+"/test_ca.pem"); |
var caPem = fs.readFileSync(fixturesDir+"/test_ca.pem"); |
||||
var certPem = fs.readFileSync(fixturesDir+"/test_cert.pem"); |
var certPem = fs.readFileSync(fixturesDir+"/test_cert.pem"); |
||||
var keyPem = fs.readFileSync(fixturesDir+"/test_key.pem"); |
var keyPem = fs.readFileSync(fixturesDir+"/test_key.pem"); |
||||
|
|
||||
|
var credentials = crypto.createCredentials({key:keyPem, cert:certPem, ca:caPem}); |
||||
|
|
||||
|
var https_server = http.createServer(function (req, res) { |
||||
|
res.id = request_number; |
||||
|
req.id = request_number++; |
||||
|
|
||||
|
var verified = c.verifyPeer(); |
||||
|
var peerDN = JSON.stringify(c.getPeerCertificate()); |
||||
|
assert.equal(verified, true); |
||||
|
assert.equal(peerDN, '{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones' |
||||
|
+ '/O=node.js/OU=Test TLS Certificate/CN=localhost",' |
||||
|
+ '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' |
||||
|
+ '/OU=Test TLS Certificate/CN=localhost","valid_from":' |
||||
|
+ '"Nov 11 09:52:22 2009 GMT","valid_to":' |
||||
|
+ '"Nov 6 09:52:22 2029 GMT"}'); |
||||
|
|
||||
var http_server=http.createServer(function (req, res) { |
if (req.id == 0) { |
||||
var verified = req.connection.verifyPeer(); |
|
||||
var peerDN = req.connection.getPeerCertificate("DNstring"); |
|
||||
assert.equal(verified, 1); |
|
||||
assert.equal(peerDN, "C=UK,ST=Acknack Ltd,L=Rhys Jones,O=node.js," |
|
||||
+ "OU=Test TLS Certificate,CN=localhost"); |
|
||||
|
|
||||
if (responses_sent == 0) { |
|
||||
assert.equal("GET", req.method); |
assert.equal("GET", req.method); |
||||
assert.equal("/hello", url.parse(req.url).pathname); |
assert.equal("/hello", url.parse(req.url).pathname); |
||||
|
assert.equal("world", qs.parse(url.parse(req.url).query).hello); |
||||
|
assert.equal("b==ar", qs.parse(url.parse(req.url).query).foo); |
||||
|
} |
||||
|
|
||||
p(req.headers); |
if (req.id == 1) { |
||||
assert.equal(true, "accept" in req.headers); |
assert.equal("POST", req.method); |
||||
assert.equal("*/*", req.headers["accept"]); |
assert.equal("/quit", url.parse(req.url).pathname); |
||||
|
} |
||||
|
|
||||
assert.equal(true, "foo" in req.headers); |
if (req.id == 2) { |
||||
assert.equal("bar", req.headers["foo"]); |
assert.equal("foo", req.headers['x-x']); |
||||
} |
} |
||||
|
|
||||
if (responses_sent == 1) { |
if (req.id == 3) { |
||||
assert.equal("POST", req.method); |
assert.equal("bar", req.headers['x-x']); |
||||
assert.equal("/world", url.parse(req.url).pathname); |
|
||||
this.close(); |
this.close(); |
||||
|
//puts("server closed");
|
||||
} |
} |
||||
|
setTimeout(function () { |
||||
req.addListener('end', function () { |
|
||||
res.writeHead(200, {"Content-Type": "text/plain"}); |
res.writeHead(200, {"Content-Type": "text/plain"}); |
||||
res.write("The path was " + url.parse(req.url).pathname); |
res.write(url.parse(req.url).pathname); |
||||
res.end(); |
res.end(); |
||||
responses_sent += 1; |
}, 1); |
||||
}); |
|
||||
|
|
||||
//assert.equal("127.0.0.1", res.connection.remoteAddress);
|
|
||||
}); |
}); |
||||
http_server.setSecure("X509_PEM", caPem, 0, keyPem, certPem); |
https_server.setSecure(credentials); |
||||
http_server.listen(PORT); |
https_server.listen(PORT); |
||||
|
|
||||
var client = http.createClient(PORT, HOST); |
var c = net.createConnection(PORT); |
||||
client.setSecure("x509_PEM", caPem, 0, keyPem, certPem); |
|
||||
var req = client.request("/hello", {"Accept": "*/*", "Foo": "bar"}); |
c.setEncoding("utf8"); |
||||
req.addListener('response', function (res) { |
|
||||
var verified = res.connection.verifyPeer(); |
c.addListener("connect", function () { |
||||
var peerDN = res.connection.getPeerCertificate("DNstring"); |
c.setSecure(credentials); |
||||
assert.equal(verified, 1); |
|
||||
assert.equal(peerDN, "C=UK,ST=Acknack Ltd,L=Rhys Jones,O=node.js," |
|
||||
+ "OU=Test TLS Certificate,CN=localhost"); |
|
||||
assert.equal(200, res.statusCode); |
|
||||
responses_recvd += 1; |
|
||||
res.setBodyEncoding("ascii"); |
|
||||
res.addListener('data', function (chunk) { body0 += chunk; }); |
|
||||
debug("Got /hello response"); |
|
||||
}); |
}); |
||||
req.end(); |
|
||||
|
|
||||
setTimeout(function () { |
|
||||
req = client.request("POST", "/world"); |
|
||||
req.addListener('response', function (res) { |
|
||||
var verified = res.connection.verifyPeer(); |
|
||||
var peerDN = res.connection.getPeerCertificate("DNstring"); |
|
||||
assert.equal(verified, 1); |
|
||||
assert.equal(peerDN, "C=UK,ST=Acknack Ltd,L=Rhys Jones,O=node.js," |
|
||||
+ "OU=Test TLS Certificate,CN=localhost"); |
|
||||
assert.equal(200, res.statusCode); |
|
||||
responses_recvd += 1; |
|
||||
res.setBodyEncoding("utf8"); |
|
||||
res.addListener('data', function (chunk) { body1 += chunk; }); |
|
||||
debug("Got /world response"); |
|
||||
}); |
|
||||
req.end(); |
|
||||
}, 1); |
|
||||
|
|
||||
process.addListener("exit", function () { |
c.addListener("secure", function () { |
||||
debug("responses_recvd: " + responses_recvd); |
var verified = c.verifyPeer(); |
||||
assert.equal(2, responses_recvd); |
var peerDN = JSON.stringify(c.getPeerCertificate()); |
||||
|
//assert.equal(verified, 1);
|
||||
|
assert.equal(peerDN, '{"subject":"/C=UK/ST=Acknack Ltd/L=Rhys Jones' |
||||
|
+ '/O=node.js/OU=Test TLS Certificate/CN=localhost",' |
||||
|
+ '"issuer":"/C=UK/ST=Acknack Ltd/L=Rhys Jones/O=node.js' |
||||
|
+ '/OU=Test TLS Certificate/CN=localhost","valid_from":' |
||||
|
+ '"Nov 11 09:52:22 2009 GMT","valid_to":' |
||||
|
+ '"Nov 6 09:52:22 2029 GMT"}'); |
||||
|
c.write( "GET /hello?hello=world&foo=b==ar HTTP/1.1\r\n\r\n" ); |
||||
|
requests_sent += 1; |
||||
|
}); |
||||
|
|
||||
debug("responses_sent: " + responses_sent); |
c.addListener("data", function (chunk) { |
||||
assert.equal(2, responses_sent); |
server_response += chunk; |
||||
|
|
||||
|
if (requests_sent == 1) { |
||||
|
c.write("POST /quit HTTP/1.1\r\n\r\n"); |
||||
|
requests_sent += 1; |
||||
|
} |
||||
|
|
||||
|
if (requests_sent == 2) { |
||||
|
c.write("GET / HTTP/1.1\r\nX-X: foo\r\n\r\n" |
||||
|
+"GET / HTTP/1.1\r\nX-X: bar\r\n\r\n"); |
||||
|
c.end(); |
||||
|
assert.equal(c.readyState, "readOnly"); |
||||
|
requests_sent += 2; |
||||
|
} |
||||
|
|
||||
assert.equal("The path was /hello", body0); |
|
||||
assert.equal("The path was /world", body1); |
|
||||
}); |
}); |
||||
|
|
||||
|
c.addListener("end", function () { |
||||
|
client_got_eof = true; |
||||
|
}); |
||||
|
|
||||
|
c.addListener("close", function () { |
||||
|
assert.equal(c.readyState, "closed"); |
||||
|
}); |
||||
|
|
||||
|
process.addListener("exit", function () { |
||||
|
assert.equal(4, request_number); |
||||
|
assert.equal(4, requests_sent); |
||||
|
|
||||
|
var hello = new RegExp("/hello"); |
||||
|
assert.equal(true, hello.exec(server_response) != null); |
||||
|
|
||||
|
var quit = new RegExp("/quit"); |
||||
|
assert.equal(true, quit.exec(server_response) != null); |
||||
|
|
||||
|
assert.equal(true, client_got_eof); |
||||
|
}); |
||||
|
Loading…
Reference in new issue