go-mail/client_test.go

155 lines
3.8 KiB
Go
Raw Permalink Normal View History

2022-03-06 15:15:42 +01:00
package mail
import (
"testing"
2022-03-07 16:24:49 +01:00
"time"
2022-03-06 15:15:42 +01:00
)
// DefaultHost is used as default hostname for the Client
const DefaultHost = "localhost"
2022-03-07 16:24:49 +01:00
// TestWithHELO tests the WithHELO() option for the NewClient() method
2022-03-06 15:15:42 +01:00
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)
}
})
}
}
2022-03-07 16:24:49 +01:00
// TestWithPort tests the WithPort() option for the NewClient() method
func TestWithPort(t *testing.T) {
tests := []struct {
name string
value int
want int
}{
{"set port to 25", 25, 25},
{"set port to 465", 465, 465},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := NewClient(DefaultHost, WithPort(tt.value))
if err != nil {
t.Errorf("failed to create new client: %s", err)
return
}
if c.port != tt.want {
t.Errorf("failed to set custom port. Want: %d, got: %d", tt.want, c.port)
}
})
}
}
// TestWithTimeout tests the WithTimeout() option for the NewClient() method
func TestWithTimeout(t *testing.T) {
tests := []struct {
name string
value time.Duration
want time.Duration
}{
{"set timeout to 5s", time.Second * 5, time.Second * 5},
{"set timeout to 30s", time.Second * 30, time.Second * 30},
{"set timeout to 1m", time.Minute, time.Minute},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := NewClient(DefaultHost, WithTimeout(tt.value))
if err != nil {
t.Errorf("failed to create new client: %s", err)
return
}
if c.cto != tt.want {
t.Errorf("failed to set custom timeout. Want: %d, got: %d", tt.want, c.cto)
}
})
}
}
// TestWithSSL tests the WithSSL() option for the NewClient() method
func TestWithSSL(t *testing.T) {
tests := []struct {
name string
want bool
}{
{"set SSL to true", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := NewClient(DefaultHost, WithSSL())
if err != nil {
t.Errorf("failed to create new client: %s", err)
return
}
if c.ssl != tt.want {
t.Errorf("failed to set SSL. Want: %t, got: %t", tt.want, c.ssl)
}
})
}
}
2022-03-09 16:52:23 +01:00
// TestWithTLSPolicy tests the WithTLSPolicy() option for the NewClient() method
func TestWithTLSPolicy(t *testing.T) {
tests := []struct {
name string
value TLSPolicy
want TLSPolicy
}{
{"Policy: TLSMandatory", TLSMandatory, TLSMandatory},
{"Policy: TLSOpportunistic", TLSOpportunistic, TLSOpportunistic},
{"Policy: NoTLS", NoTLS, NoTLS},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := NewClient(DefaultHost, WithTLSPolicy(tt.value))
if err != nil {
t.Errorf("failed to create new client: %s", err)
return
}
if c.tlspolicy != tt.want {
t.Errorf("failed to set TLSPolicy. Want: %s, got: %s", tt.want, c.tlspolicy)
}
})
}
}
// TestSetTLSPolicy tests the SetTLSPolicy() method for the Client object
func TestSetTLSPolicy(t *testing.T) {
tests := []struct {
name string
value TLSPolicy
want TLSPolicy
}{
{"Policy: TLSMandatory", TLSMandatory, TLSMandatory},
{"Policy: TLSOpportunistic", TLSOpportunistic, TLSOpportunistic},
{"Policy: NoTLS", NoTLS, NoTLS},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := NewClient(DefaultHost, WithTLSPolicy(NoTLS))
if err != nil {
t.Errorf("failed to create new client: %s", err)
return
}
c.SetTLSPolicy(tt.value)
if c.tlspolicy != tt.want {
t.Errorf("failed to set TLSPolicy. Want: %s, got: %s", tt.want, c.tlspolicy)
}
})
}
}