forked from sendgrid/sendgrid-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendgrid_test.go
77 lines (67 loc) · 1.97 KB
/
sendgrid_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package sendgrid
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
const (
APIUser = "API_USER"
APIPassword = "API_PASSWORD"
)
func TestSendGridVersion(t *testing.T) {
if Version != "2.0.0" {
t.Error("SendGrid version does not match")
}
}
func TestNewSendGridClient(t *testing.T) {
client := NewSendGridClient(APIUser, APIPassword)
if client == nil {
t.Error("NewSendGridClient should never return nil")
}
}
func TestNewSendGridClientUsernamePassword(t *testing.T) {
client := NewSendGridClient(APIUser, APIPassword)
if client.apiUser == "" || client.apiPwd == "" {
t.Error("NewSendGridClient should have username and password set")
}
}
func TestNewSendGridClientApiKey(t *testing.T) {
client := NewSendGridClientWithApiKey(APIPassword)
if client.apiUser != "" && client.apiPwd == APIPassword {
t.Error("NewSendGridClient should have api ket set")
}
}
func TestSend(t *testing.T) {
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{\"message\": \"success\"}")
}))
defer fakeServer.Close()
m := NewMail()
client := NewSendGridClient(APIUser, APIPassword)
client.APIMail = fakeServer.URL
m.AddTo("Test! <test@email.com>")
m.SetSubject("Test")
m.SetText("Text")
if e := client.Send(m); e != nil {
t.Errorf("Send failed to send email. Returned error: %v", e)
}
}
func TestSendForAuthorizationHeader(t *testing.T) {
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer "+APIPassword {
t.Error("Send failed to have authorization header")
}
fmt.Fprintln(w, "{\"message\": \"success\"}")
}))
defer fakeServer.Close()
m := NewMail()
client := NewSendGridClientWithApiKey(APIPassword)
client.APIMail = fakeServer.URL
m.AddTo("Test! <test@email.com>")
m.SetSubject("Test")
m.SetText("Text")
if e := client.Send(m); e != nil {
t.Errorf("Send failed to send email. Returned error: %v", e)
}
}