Browse Source

Fix http and net tests failing due to race condition

Plus some minor cosmetic corrections
v0.7.4-release
Bert Belder 15 years ago
committed by Ryan Dahl
parent
commit
7059be19ec
  1. 16
      test/simple/test-http-1.0.js
  2. 6
      test/simple/test-http-chunked.js
  3. 24
      test/simple/test-http-client-race-2.js
  4. 16
      test/simple/test-http-client-race.js
  5. 20
      test/simple/test-http-client-upload.js
  6. 12
      test/simple/test-http-head-response-has-no-body.js
  7. 16
      test/simple/test-http-malformed-request.js
  8. 2
      test/simple/test-http-proxy.js
  9. 25
      test/simple/test-http-server.js
  10. 24
      test/simple/test-http-tls.js
  11. 22
      test/simple/test-http-wget.js
  12. 23
      test/simple/test-http.js
  13. 18
      test/simple/test-net-binary.js
  14. 11
      test/simple/test-net-keepalive.js
  15. 22
      test/simple/test-net-tls.js

16
test/simple/test-http-1.0.js

@ -16,23 +16,25 @@ var server = http.createServer(function (req, res) {
})
server.listen(common.PORT);
var c = net.createConnection(common.PORT);
server.addListener("listening", function() {
var c = net.createConnection(common.PORT);
c.setEncoding("utf8");
c.setEncoding("utf8");
c.addListener("connect", function () {
c.addListener("connect", function () {
c.write( "GET / HTTP/1.0\r\n\r\n" );
});
});
c.addListener("data", function (chunk) {
c.addListener("data", function (chunk) {
console.log(chunk);
server_response += chunk;
});
});
c.addListener("end", function () {
c.addListener("end", function () {
client_got_eof = true;
c.end();
server.close();
});
});
process.addListener("exit", function () {

6
test/simple/test-http-chunked.js

@ -10,11 +10,13 @@ var server = http.createServer(function(req, res) {
});
server.listen(common.PORT);
http.cat("http://127.0.0.1:"+common.PORT+"/", "utf8", function (err, data) {
server.addListener("listening", function() {
http.cat("http://127.0.0.1:"+common.PORT+"/", "utf8", function (err, data) {
if (err) throw err;
assert.equal('string', typeof data);
console.log('here is the response:');
assert.equal(UTF8_STRING, data);
console.log(data);
server.close();
})
});
});

24
test/simple/test-http-client-race-2.js

@ -29,19 +29,20 @@ var server = http.createServer(function (req, res) {
});
server.listen(common.PORT);
var client = http.createClient(common.PORT);
var body1 = "";
var body2 = "";
var body3 = "";
//
// Client #1 is assigned Parser #1
//
var req1 = client.request("/1")
req1.end();
req1.addListener('response', function (res1) {
res1.setBodyEncoding("utf8");
server.addListener("listening", function() {
var client = http.createClient(common.PORT);
//
// Client #1 is assigned Parser #1
//
var req1 = client.request("/1")
req1.end();
req1.addListener('response', function (res1) {
res1.setEncoding("utf8");
res1.addListener('data', function (chunk) {
body1 += chunk;
@ -67,7 +68,7 @@ req1.addListener('response', function (res1) {
var req2 = client.request("/2");
req2.end();
req2.addListener('response', function (res2) {
res2.setBodyEncoding("utf8");
res2.setEncoding("utf8");
res2.addListener('data', function (chunk) { body2 += chunk; });
res2.addListener('end', function () {
@ -78,7 +79,7 @@ req1.addListener('response', function (res1) {
var req3 = client2.request("/3");
req3.end();
req3.addListener('response', function (res3) {
res3.setBodyEncoding("utf8");
res3.setEncoding("utf8");
res3.addListener('data', function (chunk) { body3 += chunk });
res3.addListener('end', function() { server.close(); });
});
@ -86,6 +87,7 @@ req1.addListener('response', function (res1) {
});
}, 500);
});
});
});
process.addListener("exit", function () {

16
test/simple/test-http-client-race.js

@ -15,15 +15,16 @@ var server = http.createServer(function (req, res) {
});
server.listen(common.PORT);
var client = http.createClient(common.PORT);
var body1 = "";
var body2 = "";
var req1 = client.request("/1")
req1.end();
req1.addListener('response', function (res1) {
res1.setBodyEncoding("utf8");
server.addListener("listening", function() {
var client = http.createClient(common.PORT);
var req1 = client.request("/1")
req1.end();
req1.addListener('response', function (res1) {
res1.setEncoding("utf8");
res1.addListener('data', function (chunk) {
body1 += chunk;
@ -33,11 +34,12 @@ req1.addListener('response', function (res1) {
var req2 = client.request("/2");
req2.end();
req2.addListener('response', function (res2) {
res2.setBodyEncoding("utf8");
res2.setEncoding("utf8");
res2.addListener('data', function (chunk) { body2 += chunk; });
res2.addListener('end', function () { server.close(); });
});
});
});
});
process.addListener("exit", function () {

20
test/simple/test-http-client-upload.js

@ -8,7 +8,7 @@ var client_res_complete = false;
var server = http.createServer(function(req, res) {
assert.equal("POST", req.method);
req.setBodyEncoding("utf8");
req.setEncoding("utf8");
req.addListener('data', function (chunk) {
console.log("server got: " + JSON.stringify(chunk));
@ -25,16 +25,17 @@ var server = http.createServer(function(req, res) {
});
server.listen(common.PORT);
var client = http.createClient(common.PORT);
var req = client.request('POST', '/');
req.write('1\n');
req.write('2\n');
req.write('3\n');
req.end();
server.addListener("listening", function() {
var client = http.createClient(common.PORT);
var req = client.request('POST', '/');
req.write('1\n');
req.write('2\n');
req.write('3\n');
req.end();
common.error("client finished sending request");
common.error("client finished sending request");
req.addListener('response', function(res) {
req.addListener('response', function(res) {
res.setEncoding("utf8");
res.addListener('data', function(chunk) {
console.log(chunk);
@ -43,6 +44,7 @@ req.addListener('response', function(res) {
client_res_complete = true;
server.close();
});
});
});
process.addListener("exit", function () {

12
test/simple/test-http-head-response-has-no-body.js

@ -13,18 +13,20 @@ var server = http.createServer(function(req, res) {
});
server.listen(common.PORT);
responseComplete = false;
var responseComplete = false;
var req = http.createClient(common.PORT).request('HEAD', '/')
common.error('req');
req.end();
req.addListener('response', function (res) {
server.addListener("listening", function() {
var req = http.createClient(common.PORT).request('HEAD', '/')
common.error('req');
req.end();
req.addListener('response', function (res) {
common.error('response');
res.addListener('end', function() {
common.error('response end');
server.close();
responseComplete = true;
});
});
});
process.addListener('exit', function () {

16
test/simple/test-http-malformed-request.js

@ -10,24 +10,26 @@ url = require("url");
nrequests_completed = 0;
nrequests_expected = 1;
var s = http.createServer(function (req, res) {
var server = http.createServer(function (req, res) {
console.log("req: " + JSON.stringify(url.parse(req.url)));
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("Hello World");
res.end();
if (++nrequests_completed == nrequests_expected) s.close();
if (++nrequests_completed == nrequests_expected) server.close();
});
s.listen(common.PORT);
server.listen(common.PORT);
var c = net.createConnection(common.PORT);
c.addListener("connect", function () {
server.addListener("listening", function() {
var c = net.createConnection(common.PORT);
c.addListener("connect", function () {
c.write("GET /hello?foo=%99bar HTTP/1.1\r\n\r\n");
c.end();
});
});
// TODO add more!
// TODO add more!
});
process.addListener("exit", function () {
assert.equal(nrequests_expected, nrequests_completed);

2
test/simple/test-http-proxy.js

@ -43,7 +43,7 @@ function startReq () {
req.addListener('response', function (res) {
common.debug("got res");
assert.equal(200, res.statusCode);
res.setBodyEncoding("utf8");
res.setEncoding("utf8");
res.addListener('data', function (chunk) { body += chunk; });
res.addListener('end', function () {
proxy.close();

25
test/simple/test-http-server.js

@ -10,7 +10,7 @@ var requests_sent = 0;
var server_response = "";
var client_got_eof = false;
http.createServer(function (req, res) {
var server = http.createServer(function (req, res) {
res.id = request_number;
req.id = request_number++;
@ -45,18 +45,20 @@ http.createServer(function (req, res) {
res.end();
}, 1);
}).listen(common.PORT);
});
server.listen(common.PORT);
var c = net.createConnection(common.PORT);
server.addListener("listening", function() {
var c = net.createConnection(common.PORT);
c.setEncoding("utf8");
c.setEncoding("utf8");
c.addListener("connect", function () {
c.addListener("connect", function () {
c.write( "GET /hello?hello=world&foo=b==ar HTTP/1.1\r\n\r\n" );
requests_sent += 1;
});
});
c.addListener("data", function (chunk) {
c.addListener("data", function (chunk) {
server_response += chunk;
if (requests_sent == 1) {
@ -72,14 +74,15 @@ c.addListener("data", function (chunk) {
requests_sent += 2;
}
});
});
c.addListener("end", function () {
c.addListener("end", function () {
client_got_eof = true;
});
});
c.addListener("close", function () {
c.addListener("close", function () {
assert.equal(c.readyState, "closed");
});
});
process.addListener("exit", function () {

24
test/simple/test-http-tls.js

@ -74,15 +74,16 @@ var https_server = http.createServer(function (req, res) {
https_server.setSecure(credentials);
https_server.listen(common.PORT);
var c = net.createConnection(common.PORT);
https_server.addListener("listening", function() {
var c = net.createConnection(common.PORT);
c.setEncoding("utf8");
c.setEncoding("utf8");
c.addListener("connect", function () {
c.addListener("connect", function () {
c.setSecure(credentials);
});
});
c.addListener("secure", function () {
c.addListener("secure", function () {
var verified = c.verifyPeer();
var peerDN = JSON.stringify(c.getPeerCertificate());
assert.equal(verified, true);
@ -94,9 +95,9 @@ c.addListener("secure", function () {
+ '"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;
});
});
c.addListener("data", function (chunk) {
c.addListener("data", function (chunk) {
server_response += chunk;
if (requests_sent == 1) {
@ -112,14 +113,15 @@ c.addListener("data", function (chunk) {
requests_sent += 2;
}
});
});
c.addListener("end", function () {
c.addListener("end", function () {
client_got_eof = true;
});
});
c.addListener("close", function () {
c.addListener("close", function () {
assert.equal(c.readyState, "closed");
});
});
process.addListener("exit", function () {

22
test/simple/test-http-wget.js

@ -27,33 +27,35 @@ var server = http.createServer(function (req, res) {
res.write("hello ");
res.write("world\n");
res.end();
})
});
server.listen(common.PORT);
var c = net.createConnection(common.PORT);
server.addListener("listening", function() {
var c = net.createConnection(common.PORT);
c.setEncoding("utf8");
c.setEncoding("utf8");
c.addListener("connect", function () {
c.addListener("connect", function () {
c.write("GET / HTTP/1.0\r\n" +
"Connection: Keep-Alive\r\n\r\n");
});
});
c.addListener("data", function (chunk) {
c.addListener("data", function (chunk) {
console.log(chunk);
server_response += chunk;
});
});
c.addListener("end", function () {
c.addListener("end", function () {
client_got_eof = true;
console.log('got end');
c.end();
});
});
c.addListener("close", function () {
c.addListener("close", function () {
connection_was_closed = true;
console.log('got close');
server.close();
});
});
process.addListener("exit", function () {

23
test/simple/test-http.js

@ -12,7 +12,7 @@ var responses_recvd = 0;
var body0 = "";
var body1 = "";
http.createServer(function (req, res) {
var server = http.createServer(function (req, res) {
if (responses_sent == 0) {
assert.equal("GET", req.method);
assert.equal("/hello", url.parse(req.url).pathname);
@ -39,30 +39,33 @@ http.createServer(function (req, res) {
});
//assert.equal("127.0.0.1", res.connection.remoteAddress);
}).listen(common.PORT);
});
server.listen(common.PORT);
var client = http.createClient(common.PORT);
var req = client.request("/hello", {"Accept": "*/*", "Foo": "bar"});
req.end();
req.addListener('response', function (res) {
server.addListener("listening", function() {
var client = http.createClient(common.PORT);
var req = client.request("/hello", {"Accept": "*/*", "Foo": "bar"});
req.end();
req.addListener('response', function (res) {
assert.equal(200, res.statusCode);
responses_recvd += 1;
res.setEncoding("utf8");
res.addListener('data', function (chunk) { body0 += chunk; });
common.debug("Got /hello response");
});
});
setTimeout(function () {
setTimeout(function () {
req = client.request("POST", "/world");
req.end();
req.addListener('response',function (res) {
assert.equal(200, res.statusCode);
responses_recvd += 1;
res.setBodyEncoding("utf8");
res.setEncoding("utf8");
res.addListener('data', function (chunk) { body1 += chunk; });
common.debug("Got /world response");
});
}, 1);
}, 1);
});
process.addListener("exit", function () {
common.debug("responses_recvd: " + responses_recvd);

18
test/simple/test-net-binary.js

@ -32,12 +32,13 @@ var echoServer = tcp.createServer(function (connection) {
echoServer.listen(common.PORT);
var recv = "";
var j = 0;
var c = tcp.createConnection(common.PORT);
echoServer.addListener("listening", function() {
var j = 0;
var c = tcp.createConnection(common.PORT);
c.setEncoding("binary");
c.addListener("data", function (chunk) {
c.setEncoding("binary");
c.addListener("data", function (chunk) {
if (j < 256) {
common.error("write " + j);
c.write(String.fromCharCode(j), "binary");
@ -46,15 +47,16 @@ c.addListener("data", function (chunk) {
c.end();
}
recv += chunk;
});
});
c.addListener("connect", function () {
c.addListener("connect", function () {
c.write(binaryString, "binary");
});
});
c.addListener("close", function () {
c.addListener("close", function () {
p(recv);
echoServer.close();
});
});
process.addListener("exit", function () {

11
test/simple/test-net-keepalive.js

@ -15,15 +15,16 @@ var echoServer = net.createServer(function (connection) {
});
echoServer.listen(common.PORT);
var clientConnection = net.createConnection(common.PORT);
clientConnection.setTimeout(0);
echoServer.addListener("listening", function() {
var clientConnection = net.createConnection(common.PORT);
clientConnection.setTimeout(0);
setTimeout( function() {
setTimeout( function() {
// make sure both connections are still open
assert.equal(serverConnection.readyState,"open");
assert.equal(clientConnection.readyState,"open");
serverConnection.end();
clientConnection.end();
echoServer.close();
}, 1200);
}, 1200);
});

22
test/simple/test-net-tls.js

@ -58,14 +58,15 @@ var secureServer = net.createServer(function (connection) {
});
secureServer.listen(common.PORT);
var secureClient = net.createConnection(common.PORT);
secureServer.addListener("listening", function() {
var secureClient = net.createConnection(common.PORT);
secureClient.setEncoding("UTF8");
secureClient.addListener("connect", function () {
secureClient.setEncoding("UTF8");
secureClient.addListener("connect", function () {
secureClient.setSecure(credentials);
});
});
secureClient.addListener("secure", function () {
secureClient.addListener("secure", function () {
gotSecureClient = true;
var verified = secureClient.verifyPeer();
var peerDN = JSON.stringify(secureClient.getPeerCertificate());
@ -79,19 +80,18 @@ secureClient.addListener("secure", function () {
secureClient.write(testData);
secureClient.end();
});
});
secureClient.addListener("data", function (chunk) {
secureClient.addListener("data", function (chunk) {
clientData += chunk;
});
});
secureClient.addListener("end", function () {
secureClient.addListener("end", function () {
assert.equal(clientData, testData);
});
});
process.addListener("exit", function () {
assert.ok(gotSecureServer, "Did not get secure event for server");
assert.ok(gotSecureClient, "Did not get secure event for clientr");
});

Loading…
Cancel
Save