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