Created
October 12, 2014 11:58
-
-
Save tmichel/a57bd4033db3e90f5516 to your computer and use it in GitHub Desktop.
Revisions
-
tmichel created this gist
Oct 12, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ package email import ( "net/smtp" "testing" ) type EmailConfig struct { Username string Password string ServerHost string ServerPort string SenderAddr string } type EmailSender interface { Send(to []string, body []byte) error } func NewEmailSender(conf EmailConfig) EmailSender { return &emailSender{conf, smtp.SendMail} } type emailSender struct { conf EmailConfig send func(string, smtp.Auth, string, []string, []byte) error } func (e *emailSender) Send(to []string, body []byte) error { addr := e.conf.ServerHost + ":" + e.conf.ServerPort auth := smtp.PlainAuth("", e.conf.Username, e.conf.Password, e.conf.ServerHost) return e.send(addr, auth, e.conf.SenderAddr, to, body) } /****** testing ******/ func TestEmail_SendSuccessful(t *testing.T) { f, r := mockSend(nil) sender := &emailSender{send: f} body := "Hello World" err := sender.Send([]string{"me@example.com"}, []byte(body)) if err != nil { t.Errorf("unexpected error: %s", err) } if string(r.msg) != body { t.Errorf("wrong message body.\n\nexpected: %\n got: %s", body, r.msg) } } func mockSend(errToReturn error) (func(string, smtp.Auth, string, []string, []byte) error, *emailRecorder) { r := new(emailRecorder) return func(addr string, a smtp.Auth, from string, to []string, msg []byte) error { *r = emailRecorder{addr, a, from, to, msg} return errToReturn }, r } type emailRecorder struct { addr string auth smtp.Auth from string to []string msg []byte }