Adding first tests

This commit is contained in:
Winni Neessen 2022-03-06 15:15:42 +01:00
parent 261481344f
commit 2fcc0f59cd
Signed by: wneessen
GPG key ID: 385AC9889632126E
3 changed files with 39 additions and 10 deletions

View file

@ -103,6 +103,13 @@ func WithSSL() Option {
}
}
// WithHELO tells the client to use the provided string as HELO/EHLO greeting host
func WithHELO(h string) Option {
return func(c *Client) {
c.helo = h
}
}
// Dial establishes a connection cto the SMTP server with a default context.Background
func (c *Client) Dial() error {
ctx := context.Background()
@ -146,15 +153,6 @@ func (c *Client) Send() error {
// Close closes the connection cto the SMTP server
func (c *Client) Close() error {
if err := c.sc.Close(); err != nil {
fmt.Printf("failed close: %s\n", err)
return err
}
if ok, auth := c.sc.Extension("PIPELINING"); ok {
fmt.Printf("PIPELINING Support: %s\n", auth)
} else {
fmt.Println("No PIPELINING")
}
return c.sc.Close()
}

31
client_test.go Normal file
View file

@ -0,0 +1,31 @@
package mail
import (
"testing"
)
// DefaultHost is used as default hostname for the Client
const DefaultHost = "localhost"
// TestWithHELo tests the WithHELO() option for the NewClient() method
func TestWithHELO(t *testing.T) {
tests := []struct {
name string
value string
want string
}{
{"HELO test.de", "test.de", "test.de"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := NewClient(DefaultHost, WithHELO(tt.value))
if err != nil {
t.Errorf("failed to create new client: %s", err)
return
}
if c.helo != tt.want {
t.Errorf("failed to set custom HELO. Want: %s, got: %s", tt.want, c.helo)
}
})
}
}

View file

@ -9,7 +9,7 @@ import (
)
func main() {
c, err := mail.NewClient("192.168.178.60", mail.WithTimeout(time.Millisecond*500), mail.WithSSL())
c, err := mail.NewClient("localhost", mail.WithTimeout(time.Millisecond*500))
if err != nil {
fmt.Printf("failed to create new client: %s\n", err)
os.Exit(1)