-
Notifications
You must be signed in to change notification settings - Fork 6
/
util_test.go
61 lines (49 loc) · 1.43 KB
/
util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package sendmail_test
import (
"bytes"
"net/mail"
"reflect"
"testing"
"github.com/n0madic/sendmail"
)
func TestGetDumbMessage(t *testing.T) {
expectedHeader := mail.Header{
"From": []string{"sender@localhost"},
"To": []string{"user@example.com"},
}
expectedBody := []byte("TEST\r\n")
_, err := sendmail.GetDumbMessage("", []string{}, []byte{})
if err.Error() != "empty recipients list" {
t.Error("Expected empty recipients list")
}
msg, err := sendmail.GetDumbMessage("sender@localhost", []string{"user@example.com"}, []byte("TEST"))
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(msg.Header, expectedHeader) {
t.Error("Expected", expectedHeader, "got", msg.Header)
}
buf := new(bytes.Buffer)
buf.ReadFrom(msg.Body)
if !reflect.DeepEqual(buf.Bytes(), expectedBody) {
t.Error("Expected", expectedBody, "got", buf.Bytes())
}
}
func TestAddressListToSlice(t *testing.T) {
expected := []string{"user@example.com"}
list := []*mail.Address{{Address: "user@example.com"}}
slice := sendmail.AddressListToSlice(list)
if !reflect.DeepEqual(slice, expected) {
t.Error("Expected", expected, "got", slice)
}
}
func TestGetDomainFromAddress(t *testing.T) {
expected := "example.com"
domain := sendmail.GetDomainFromAddress("user@example.com")
if domain != expected {
t.Error("Expected", expected, "got", domain)
}
if sendmail.GetDomainFromAddress("example.com") != "" {
t.Error("Expected empty string")
}
}