forked from keighl/postmark
-
Notifications
You must be signed in to change notification settings - Fork 15
/
email_test.go
111 lines (97 loc) · 2.45 KB
/
email_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
107
108
109
110
111
package postmark
import (
"context"
"net/http"
"testing"
"goji.io/pat"
)
var testEmail = Email{
From: "sender@example.com",
To: "receiver@example.com",
Cc: "copied@example.com",
Bcc: "blank-copied@example.com",
Subject: "Test",
Tag: "Invitation",
HTMLBody: "<b>Hello</b>",
TextBody: "Hello",
ReplyTo: "reply@example.com",
Headers: []Header{
{
Name: "CUSTOM-HEADER",
Value: "value",
},
},
TrackOpens: true,
Attachments: []Attachment{
{
Name: "readme.txt",
Content: "dGVzdCBjb250ZW50",
ContentType: "text/plain",
},
{
Name: "report.pdf",
Content: "dGVzdCBjb250ZW50",
ContentType: "application/octet-stream",
},
},
}
func TestSendEmail(t *testing.T) {
responseJSON := `{
"To": "receiver@example.com",
"SubmittedAt": "2014-02-17T07:25:01.4178645-05:00",
"MessageID": "0a129aee-e1cd-480d-b08d-4f48548ff48d",
"ErrorCode": 0,
"Message": "OK"
}`
tMux.HandleFunc(pat.Post("/email"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})
// Success
res, err := client.SendEmail(context.Background(), testEmail)
if err != nil {
t.Fatalf("SendEmail: %s", err.Error())
}
if res.MessageID != "0a129aee-e1cd-480d-b08d-4f48548ff48d" {
t.Fatalf("SendEmail: wrong id!")
}
// Failure
responseJSON = `{
"To": "receiver@example.com",
"SubmittedAt": "2014-02-17T07:25:01.4178645-05:00",
"MessageID": "0a129aee-e1cd-480d-b08d-4f48548ff48d",
"ErrorCode": 401,
"Message": "Sender signature not confirmed"
}`
_, err = client.SendEmail(context.Background(), testEmail)
if err == nil {
t.Fatalf("SendEmail should have failed")
}
}
func TestSendEmailBatch(t *testing.T) {
responseJSON := `[
{
"ErrorCode": 0,
"Message": "OK",
"MessageID": "b7bc2f4a-e38e-4336-af7d-e6c392c2f817",
"SubmittedAt": "2010-11-26T12:01:05.1794748-05:00",
"To": "receiver1@example.com"
},
{
"ErrorCode": 0,
"Message": "OK",
"MessageID": "e2ecbbfc-fe12-463d-b933-9fe22915106d",
"SubmittedAt": "2010-11-26T12:01:05.1794748-05:00",
"To": "receiver2@example.com"
}
]`
tMux.HandleFunc(pat.Post("/email/batch"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})
res, err := client.SendEmailBatch(context.Background(), []Email{testEmail, testEmail})
if err != nil {
t.Fatalf("SendEmailBatch: %s", err.Error())
}
if len(res) != 2 {
t.Fatalf("SendEmailBatch: wrong response array size!")
}
}