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

49 lines
1.2 KiB
JavaScript
Raw Normal View History

common = require("../common");
assert = common.assert
http = require("http");
url = require("url");
var body1_s = "1111111111111111";
var body2_s = "22222";
2009-09-28 18:36:36 +08:00
var server = http.createServer(function (req, res) {
var body = url.parse(req.url).pathname === "/1" ? body1_s : body2_s;
2010-02-26 04:54:48 +08:00
res.writeHead(200, { "Content-Type": "text/plain"
, "Content-Length": body.length
});
res.end(body);
});
server.listen(common.PORT);
var body1 = "";
var body2 = "";
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;
});
res1.addListener('end', function () {
var req2 = client.request("/2");
req2.end();
req2.addListener('response', function (res2) {
res2.setEncoding("utf8");
res2.addListener('data', function (chunk) { body2 += chunk; });
res2.addListener('end', function () { server.close(); });
});
});
2009-06-28 02:40:43 +08:00
});
});
process.addListener("exit", function () {
assert.equal(body1_s, body1);
assert.equal(body2_s, body2);
});