test(mailer): improve - send and fetch mail
continuous-integration/drone the build was successful
Details
continuous-integration/drone the build was successful
Details
This commit is contained in:
parent
0e411ba5e2
commit
2502a3bf67
|
@ -4,6 +4,8 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/go-mail/mail"
|
||||
)
|
||||
|
||||
func TestSetupAndPing(t *testing.T) {
|
||||
|
@ -23,3 +25,30 @@ func TestSetupAndPing(t *testing.T) {
|
|||
assert.Error(err)
|
||||
mock.Close()
|
||||
}
|
||||
|
||||
func TestSend(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mock, s := NewFakeServer()
|
||||
assert.NotNil(mock)
|
||||
// correct setup
|
||||
err := s.Setup()
|
||||
assert.NoError(err)
|
||||
|
||||
m := mail.NewMessage()
|
||||
m.SetHeader("From", "alex@example.com")
|
||||
m.SetHeader("To", "bob@example.com", "cora@example.com")
|
||||
m.SetAddressHeader("Cc", "dan@example.com", "Dan")
|
||||
m.SetHeader("Subject", "Hello!")
|
||||
m.SetBody("text/plain", "Hello Bob and Cora!")
|
||||
m.AddAlternative("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
|
||||
|
||||
err = s.Dailer.DialAndSend(m)
|
||||
assert.NoError(err)
|
||||
|
||||
msg := <-mock.MSGS
|
||||
mock.Close()
|
||||
assert.Equal("alex@example.com", msg.Header["From"][0])
|
||||
assert.Contains(msg.Body, "Bob and Cora!")
|
||||
|
||||
}
|
||||
|
|
|
@ -10,8 +10,14 @@ import (
|
|||
)
|
||||
|
||||
type fakeServer struct {
|
||||
s *Service
|
||||
l net.Listener
|
||||
s *Service
|
||||
l net.Listener
|
||||
MSGS chan msg
|
||||
}
|
||||
|
||||
type msg struct {
|
||||
Header textproto.MIMEHeader
|
||||
Body string
|
||||
}
|
||||
|
||||
// NewFakeServer - to get mocked Service for mail-service
|
||||
|
@ -28,7 +34,8 @@ func NewFakeServer() (*fakeServer, *Service) {
|
|||
|
||||
func newFakeServer(s *Service) (*fakeServer, *Service) {
|
||||
fs := &fakeServer{
|
||||
s: s,
|
||||
s: s,
|
||||
MSGS: make(chan msg),
|
||||
}
|
||||
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", fs.s.SMTPHost, fs.s.SMTPPort))
|
||||
if err != nil {
|
||||
|
@ -84,6 +91,30 @@ func (fs *fakeServer) handle(conn net.Conn) {
|
|||
case "QUIT":
|
||||
c.Cmd("221 Bye")
|
||||
return
|
||||
case "DATA":
|
||||
c.Cmd("354 End data with <CR><LF>.<CR><LF>")
|
||||
head, _ := c.ReadMIMEHeader()
|
||||
data := ""
|
||||
handleMsgData:
|
||||
for {
|
||||
s, _ := c.ReadLine()
|
||||
switch s {
|
||||
case ".":
|
||||
break handleMsgData
|
||||
default:
|
||||
data = fmt.Sprintf("%s%s\n", data, s)
|
||||
c.Cmd("250 Ok")
|
||||
}
|
||||
|
||||
}
|
||||
fs.MSGS <- msg{
|
||||
Header: head,
|
||||
Body: data,
|
||||
}
|
||||
default:
|
||||
// fmt.Println(s)
|
||||
// TODO : MAIL FROM: and RCPT TO:
|
||||
c.Cmd("250 Ok")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue