Skip to content
This repository was archived by the owner on Nov 28, 2024. It is now read-only.

Smtp client reset #9

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Sender interface {
type SendCloser interface {
Sender
Close() error
Reset() error
}

// A SendFunc is a function that sends emails to the given addresses.
Expand Down
9 changes: 9 additions & 0 deletions send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,28 @@ func (s mockSender) Send(from string, to []string, msg io.WriterTo) error {
type mockSendCloser struct {
mockSender
close func() error
reset func() error
}

func (s *mockSendCloser) Close() error {
return s.close()
}

func (s *mockSendCloser) Reset() error {
return s.reset()
}

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
},
reset: func() error {
t.Error("Reset() should not be called in Send()")
return nil
},
}
if err := Send(s, getTestMessage()); err != nil {
t.Errorf("Send(): %v", err)
Expand Down
5 changes: 5 additions & 0 deletions smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ func (c *smtpSender) Close() error {
return c.Quit()
}

func (c *smtpSender) Reset() error {
return c.smtpClient.Reset()
}

// Stubbed out for tests.
var (
netDialTimeout = net.DialTimeout
Expand All @@ -196,6 +200,7 @@ type smtpClient interface {
Auth(smtp.Auth) error
Mail(string) error
Rcpt(string) error
Reset() error
Data() (io.WriteCloser, error)
Quit() error
Close() error
Expand Down
67 changes: 67 additions & 0 deletions smtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ func TestDialerTimeout(t *testing.T) {
})
}

func TestDialerReset(t *testing.T) {
d := NewDialer(testHost, testPort, "user", "pwd")
doTestSMTPReset(t, d, []string{
"Extension STARTTLS",
"StartTLS",
"Extension AUTH",
"Auth",
"Reset",
"Quit",
"Close",
})
}

type mockClient struct {
t *testing.T
i int
Expand Down Expand Up @@ -195,6 +208,11 @@ func (c *mockClient) Quit() error {
return nil
}

func (c *mockClient) Reset() error {
c.do("Reset")
return nil
}

func (c *mockClient) Close() error {
c.do("Close")
return nil
Expand Down Expand Up @@ -239,6 +257,55 @@ func testSendMailTimeout(t *testing.T, d *Dialer, want []string) {
doTestSendMail(t, d, want, true)
}

func doTestSMTPReset(t *testing.T, d *Dialer, want []string){
testClient := &mockClient{
t: t,
want: want,
addr: addr(d.Host, d.Port),
config: d.TLSConfig,
timeout: false,
}

netDialTimeout = func(network, address string, d time.Duration) (net.Conn, error) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the build error, the problem is here. If you capitalize NetDialTimeout that should fix it. (We exported this in #4).

if network != "tcp" {
t.Errorf("Invalid network, got %q, want tcp", network)
}
if address != testClient.addr {
t.Errorf("Invalid address, got %q, want %q",
address, testClient.addr)
}
return testConn, nil
}

tlsClient = func(conn net.Conn, config *tls.Config) *tls.Conn {
if conn != testConn {
t.Errorf("Invalid conn, got %#v, want %#v", conn, testConn)
}
assertConfig(t, config, testClient.config)
return testTLSConn
}

smtpNewClient = func(conn net.Conn, host string) (smtpClient, error) {
if host != testHost {
t.Errorf("Invalid host, got %q, want %q", host, testHost)
}
return testClient, nil
}
//Start "daemon" mode
s, err := d.Dial()
if err != nil {
t.Error(err)
}

/*
Call the reset for testing purposes. In practice, this should be called after an error while attempting to send
a message. But per RFC 5321 RSET can be called at any time.
*/
s.Reset()
s.Close()

}

func doTestSendMail(t *testing.T, d *Dialer, want []string, timeout bool) {
testClient := &mockClient{
t: t,
Expand Down