smtp-tester
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -70,3 +70,11 @@ /*jslint node:true, nomen:false */ | ||
smtpServer.listen(port); | ||
processMsg = function(email,msgid) { | ||
// catch-all handlers | ||
_.each(handlers.ALL || [],function(entry){ | ||
if (entry && typeof(entry) === "function") { | ||
entry(null,msgid,email); | ||
} | ||
}); | ||
// address-specific handlers | ||
_.each(email.receivers,function(val,key){ | ||
@@ -84,7 +92,15 @@ _.each(handlers[key] || [],function(entry){ | ||
// first bind for new messages | ||
handlers[rcpt] = handlers[rcpt] || []; | ||
handlers[rcpt].push(handler); | ||
// what if there is no rcpt given? then bind for ALL messages | ||
if (!handler && rcpt && typeof(rcpt) === "function") { | ||
handler = rcpt; | ||
rcpt = null; | ||
handlers.ALL = handlers.ALL || []; | ||
handlers.ALL.push(handler); | ||
} else { | ||
handlers[rcpt] = handlers[rcpt] || []; | ||
handlers[rcpt].push(handler); | ||
} | ||
// next see if any were already in queue | ||
_.each(msgs,function(email,msgid){ | ||
if (email.receivers && email.receivers[rcpt]) { | ||
if ((email.receivers && email.receivers[rcpt]) || !rcpt) { | ||
handler(rcpt,msgid,email); | ||
@@ -96,2 +112,7 @@ } | ||
var tmp = []; | ||
// what if there is no rcpt given? then bind for ALL messages | ||
if (!handler && rcpt && typeof(rcpt) === "function") { | ||
handler = rcpt; | ||
rcpt = "ALL"; | ||
} | ||
_.each(handlers || [],function(ary,r2){ | ||
@@ -98,0 +119,0 @@ if (r2 === rcpt && ary) { |
{ | ||
"name": "smtp-tester", | ||
"description": "Quick and dirty smtp server, that accepts handlers to process messages", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"url": "http://github.com/deitch/smtp-tester", | ||
@@ -6,0 +6,0 @@ "author": "Avi Deitcher <avi@deitcher.net>", |
@@ -53,2 +53,18 @@ smtp-tester | ||
You can have as many handlers as you want, they are all executed, even for the same address. However, execution order, while usually in the order in which they were added, is not guaranteed. | ||
# Catch-All Handlers | ||
If you want a handler to catch every email that is sent through the system, just bind with no address at all. | ||
````JavaScript | ||
handler = function(addr,id,email) { | ||
// do something interesting | ||
// because this is a catch-all, the addr will be null | ||
}; | ||
mailServer.bind(handler); | ||
```` | ||
Catch-All handlers are *always* run before specific handlers. | ||
Stopping Receipt | ||
@@ -62,2 +78,14 @@ ---------------- | ||
# Catch-All Handlers | ||
If you want to remove a catch-all handler that catches every email that is sent through the system, just unbind with no address at all. | ||
````JavaScript | ||
handler = function(addr,id,email) { | ||
// do something interesting | ||
// because this is a catch-all, the addr will be null | ||
}; | ||
mailServer.bind(handler); // this adds it | ||
mailServer.unbind(handler); // this removes it | ||
```` | ||
Removing Messages | ||
@@ -84,3 +112,3 @@ ----------------- | ||
* addr: Address to which the email was addressed, and for which the handler was bound. | ||
* addr: Address to which the email was addressed, and for which the handler was bound. If this is a catch-all handler, then this is null. | ||
* id: Internal ID of the email in this mail server process. Useful for removing messages or checking against something in our cache. | ||
@@ -87,0 +115,0 @@ * email: JavaScript object of the email, containing "sender", "receivers", "data" (raw text), "headers" and "body". |
@@ -5,3 +5,3 @@ /*jslint node:true */ | ||
mailServer = ms.init(mailPort); | ||
//mailServer = ms.init(mailPort); | ||
@@ -27,4 +27,12 @@ mailer.SMTP = { | ||
sendMail: nodeunit.testCase({ | ||
setUp: function(callback) { | ||
mailServer = ms.init(mailPort); | ||
callback(); | ||
}, | ||
tearDown: function(callback) { | ||
mailServer.stop(); | ||
callback(); | ||
}, | ||
// not logged in should give unauthenticated | ||
receiveMail : function(test) { | ||
specificHandler : function(test) { | ||
var handler, checkDone, count = 0, expected = 2, addr = "foo@bar.com", subject = "email test", body = "This is a test email"; | ||
@@ -57,2 +65,31 @@ // bind a handler | ||
}); | ||
}, | ||
catchAllHandler : function(test) { | ||
var handler, checkDone, count = 0, expected = 2, addr = "foo@bar.com", subject = "email test", body = "This is a test email"; | ||
// bind a handler | ||
handler = function(address,id,email) { | ||
test.equal(address,null,"Should have address 'null' sent to handler"); | ||
test.equal(email.body,body+"\r\n","Body should match"); | ||
test.equal(email.headers.To,addr,"Should have header address To match"); | ||
test.equal(email.headers.From,from,"Should have header address From match"); | ||
checkDone(); | ||
}; | ||
checkDone = function() { | ||
count++; | ||
if (count >= expected) { | ||
test.done(); | ||
} | ||
}; | ||
mailServer.bind(handler); | ||
// send out the email with the activation code | ||
sendmail(addr,subject,body, function(error, success){ | ||
// indicate we are done | ||
test.equal(true,success,"Should have success in sending mail"); | ||
if (success) { | ||
checkDone(); | ||
} else { | ||
test.done(); | ||
} | ||
}); | ||
} | ||
@@ -63,5 +100,3 @@ }) | ||
nodeunit.reporters["default"].run(testFn,null,function(){ | ||
mailServer.stop(); | ||
}); | ||
nodeunit.reporters["default"].run(testFn,null); | ||
12612
227
131