forked from sendgrid/sendgrid-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendgrid_test.go
106 lines (90 loc) · 2.74 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package sendgrid
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/elazarl/goproxy"
)
// @note fill this out before testing
const (
APIUser = "API_USER"
APIPassword = "API_PASSWORD"
TestRecipient = "Test! <test@email.com>"
)
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(TestRecipient)
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 TestSendThroughProxy(t *testing.T) {
// Start own proxy server locally
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = true
fakeProxy := httptest.NewServer(proxy)
defer fakeProxy.Close()
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(TestRecipient)
m.SetSubject("Test")
m.SetText("Text")
if e := client.SendThroughProxy(m, fakeProxy.URL); e != nil {
t.Errorf("Send failed to send email through proxy. 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(TestRecipient)
m.SetSubject("Test")
m.SetText("Text")
if e := client.Send(m); e != nil {
t.Errorf("Send failed to send email. Returned error: %v", e)
}
}