0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-29 23:16:30 +01:00
nodejs/test/pummel/test-multipart.js

141 lines
3.9 KiB
JavaScript
Raw Normal View History

2010-02-25 07:36:17 +01:00
process.mixin(require("../common"));
2009-09-27 16:52:33 +02:00
var http = require("http"),
multipart = require("multipart"),
sys = require("sys"),
2010-02-25 07:36:17 +01:00
fixture = require("../fixtures/multipart"),
events = require("events"),
testPart = function (expect, part) {
if (!expect) {
throw new Error("Got more parts than expected: "+
JSON.stringify(part.headers));
}
for (var i in expect) {
assert.equal(expect[i], part[i]);
2010-01-05 22:44:09 +01:00
}
};
2009-09-27 16:52:33 +02:00
var emails = fixture.messages.slice(0),
chunkSize = 1; // set to minimum to forcibly expose boundary conditions.
// in a real scenario, this would be much much bigger.
// test streaming messages through directly, as if they were in a file or something.
sys.puts("test "+emails.length+" emails");
(function testEmails () {
var email = emails.pop(),
curr = 0;
if (!email) {
sys.puts("done testing emails");
testGoodMessages();
return;
}
sys.puts("testing email "+emails.length);
var expect = email.expect;
2009-09-27 16:52:33 +02:00
var message = new (events.EventEmitter);
message.headers = email.headers;
2009-09-27 16:52:33 +02:00
var mp = multipart.parse(message);
mp.addListener("partBegin", function (part) {
sys.puts(">> testing part #"+curr);
testPart(email.expect[curr ++], part);
});
mp.addListener("complete", function () {
sys.puts("done with email "+emails.length);
process.nextTick(testEmails);
});
// stream it through in chunks.
var emailBody = email.body;
process.nextTick(function s () {
if (emailBody) {
message.emit("data", emailBody.substr(0, chunkSize));
emailBody = emailBody.substr(chunkSize);
process.nextTick(s);
} else {
message.emit("end");
2009-09-27 16:52:33 +02:00
}
});
})();
// run good HTTP messages test after previous test ends.
var server = http.createServer(function (req, res) {
sys.puts("HTTP mp request");
var mp = multipart.parse(req),
curr = 0;
req.setBodyEncoding("binary");
if (req.url !== "/bad") {
sys.puts("expected to be good");
mp.addListener("partBegin", function (part) {
sys.puts(">> testing part #"+curr);
testPart(message.expect[curr ++], part);
});
} else {
sys.puts("expected to be bad");
}
mp.addListener("error", function (er) {
sys.puts("!! error occurred");
2010-02-25 21:54:48 +01:00
res.writeHead(400, {});
res.write("bad");
res.close();
2009-09-27 16:52:33 +02:00
});
mp.addListener("complete", function () {
2010-02-25 21:54:48 +01:00
res.writeHead(200, {});
res.write("ok");
res.close();
2009-09-27 16:52:33 +02:00
});
}),
message,
client = http.createClient(PORT);
2010-01-05 22:44:09 +01:00
server.listen(PORT);
2009-09-27 16:52:33 +02:00
// could dry these two up a bit.
function testGoodMessages () {
var httpMessages = fixture.messages.slice(0);
sys.puts("testing "+httpMessages.length+" good messages");
(function testHTTP () {
message = httpMessages.pop();
if (!message) {
testBadMessages();
return;
}
sys.puts("test message "+httpMessages.length);
var req = client.request("POST", "/", message.headers);
req.write(message.body, "binary");
req.addListener('response', function (res) {
var buff = "";
res.addListener("data", function (chunk) { buff += chunk });
res.addListener("end", function () {
assert.equal(buff, "ok");
process.nextTick(testHTTP);
});
});
req.close();
})();
}
function testBadMessages () {
var httpMessages = fixture.badMessages.slice(0);
sys.puts("testing "+httpMessages.length+" bad messages");
(function testHTTP () {
message = httpMessages.pop();
if (!message) {
server.close()
return;
}
sys.puts("test message "+httpMessages.length);
var req = client.request("POST", "/bad", message.headers);
req.write(message.body, "binary");
req.addListener('response', function (res) {
var buff = "";
res.addListener("data", function (chunk) { buff += chunk });
res.addListener("end", function () {
assert.equal(buff, "bad");
process.nextTick(testHTTP);
});
});
req.close();
})();
}