From 2fcc0f59cda03c1dacf3495d8fbb2dfc8eb18228 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sun, 6 Mar 2022 15:15:42 +0100 Subject: [PATCH] Adding first tests --- client.go | 16 +++++++--------- client_test.go | 31 +++++++++++++++++++++++++++++++ cmd/main.go | 2 +- 3 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 client_test.go diff --git a/client.go b/client.go index 78ae6a2..88dafc4 100644 --- a/client.go +++ b/client.go @@ -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() } diff --git a/client_test.go b/client_test.go new file mode 100644 index 0000000..572e094 --- /dev/null +++ b/client_test.go @@ -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) + } + }) + } +} diff --git a/cmd/main.go b/cmd/main.go index 1b95f5d..f43139a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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)