2014-10-22 23:47:24 +08:00
|
|
|
package gomail
|
|
|
|
|
|
|
|
import (
|
2015-07-03 05:08:18 +08:00
|
|
|
"bytes"
|
2014-10-22 23:47:24 +08:00
|
|
|
"io"
|
2015-06-30 17:55:03 +08:00
|
|
|
"reflect"
|
2014-10-22 23:47:24 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
const (
|
|
|
|
testTo1 = "to1@example.com"
|
|
|
|
testTo2 = "to2@example.com"
|
|
|
|
testFrom = "from@example.com"
|
|
|
|
testBody = "Test message"
|
|
|
|
testMsg = "To: " + testTo1 + ", " + testTo2 + "\r\n" +
|
|
|
|
"From: " + testFrom + "\r\n" +
|
|
|
|
"Mime-Version: 1.0\r\n" +
|
|
|
|
"Date: Wed, 25 Jun 2014 17:46:00 +0000\r\n" +
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: quoted-printable\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
testBody
|
2014-10-22 23:47:24 +08:00
|
|
|
)
|
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
type mockSender SendFunc
|
2014-10-22 23:47:24 +08:00
|
|
|
|
2015-07-03 05:08:18 +08:00
|
|
|
func (s mockSender) Send(from string, to []string, msg io.WriterTo) error {
|
2015-06-30 17:55:03 +08:00
|
|
|
return s(from, to, msg)
|
2014-10-22 23:47:24 +08:00
|
|
|
}
|
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
type mockSendCloser struct {
|
|
|
|
mockSender
|
|
|
|
close func() error
|
2014-10-22 23:55:36 +08:00
|
|
|
}
|
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
func (s *mockSendCloser) Close() error {
|
|
|
|
return s.close()
|
2014-10-22 23:47:24 +08:00
|
|
|
}
|
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
func TestSend(t *testing.T) {
|
|
|
|
s := &mockSendCloser{
|
|
|
|
mockSender: stubSend(t, testFrom, []string{testTo1, testTo2}, testMsg),
|
|
|
|
close: func() error {
|
|
|
|
t.Error("Close() should not be called in Send()")
|
|
|
|
return nil
|
|
|
|
},
|
2014-10-22 23:47:24 +08:00
|
|
|
}
|
2015-06-30 17:55:03 +08:00
|
|
|
if err := Send(s, getTestMessage()); err != nil {
|
|
|
|
t.Errorf("Send(): %v", err)
|
2014-10-22 23:47:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
func getTestMessage() *Message {
|
|
|
|
m := NewMessage()
|
|
|
|
m.SetHeader("From", testFrom)
|
|
|
|
m.SetHeader("To", testTo1, testTo2)
|
|
|
|
m.SetBody("text/plain", testBody)
|
2014-10-22 23:47:24 +08:00
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
return m
|
2014-10-22 23:47:24 +08:00
|
|
|
}
|
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
func stubSend(t *testing.T, wantFrom string, wantTo []string, wantBody string) mockSender {
|
2015-07-03 05:08:18 +08:00
|
|
|
return func(from string, to []string, msg io.WriterTo) error {
|
2015-06-30 17:55:03 +08:00
|
|
|
if from != wantFrom {
|
|
|
|
t.Errorf("invalid from, got %q, want %q", from, wantFrom)
|
2014-10-22 23:55:36 +08:00
|
|
|
}
|
2015-06-30 17:55:03 +08:00
|
|
|
if !reflect.DeepEqual(to, wantTo) {
|
|
|
|
t.Errorf("invalid to, got %v, want %v", to, wantTo)
|
2014-10-22 23:55:36 +08:00
|
|
|
}
|
2014-10-22 23:47:24 +08:00
|
|
|
|
2015-07-03 05:08:18 +08:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
_, err := msg.WriteTo(buf)
|
2015-06-30 17:55:03 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-07-03 05:08:18 +08:00
|
|
|
compareBodies(t, buf.String(), wantBody)
|
2014-10-22 23:47:24 +08:00
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
return nil
|
2014-10-22 23:47:24 +08:00
|
|
|
}
|
|
|
|
}
|