forked from keighl/postmark
-
Notifications
You must be signed in to change notification settings - Fork 17
/
bounce_test.go
230 lines (204 loc) · 5.61 KB
/
bounce_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package postmark
import (
"context"
"net/http"
"testing"
"goji.io/pat"
)
func TestGetDeliveryStats(t *testing.T) {
responseJSON := `{
"InactiveMails": 192,
"Bounces": [
{
"Name": "All",
"Count": 253
},
{
"Type": "HardBounce",
"Name": "Hard bounce",
"Count": 195
},
{
"Type": "Transient",
"Name": "Message delayed",
"Count": 10
},
{
"Type": "AutoResponder",
"Name": "Auto responder",
"Count": 14
},
{
"Type": "SpamNotification",
"Name": "Spam notification",
"Count": 3
},
{
"Type": "SoftBounce",
"Name": "Soft bounce",
"Count": 30
},
{
"Type": "SpamComplaint",
"Name": "Spam complaint",
"Count": 1
}
]}`
tMux.HandleFunc(pat.Get("/deliverystats"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})
res, err := client.GetDeliveryStats(context.Background())
if err != nil {
t.Fatalf("GetDeliveryStats: %s", err.Error())
}
if res.InactiveMails != 192 {
t.Fatalf("GetDeliveryStats: wrong inactive mail count %d", res.InactiveMails)
}
}
func TestGetBounces(t *testing.T) {
responseJSON := `{
"TotalCount": 253,
"Bounces": [
{
"ID": 692560173,
"Type": "HardBounce",
"TypeCode": 1,
"Name": "Hard bounce",
"Tag": "Invitation",
"MessageID": "2c1b63fe-43f2-4db5-91b0-8bdfa44a9316",
"Description": "The server was unable to deliver your message (ex: unknown user, mailbox not found).",
"Details": "action: failed\r\n",
"Email": "anything@blackhole.postmarkap.com",
"BouncedAt": "2014-01-15T16:09:19.6421112-05:00",
"DumpAvailable": false,
"Inactive": false,
"CanActivate": true,
"Subject": "SC API5 Test"
},
{
"ID": 676862817,
"Type": "HardBounce",
"TypeCode": 1,
"Name": "Hard bounce",
"Tag": "Invitation",
"MessageID": "623b2e90-82d0-4050-ae9e-2c3a734ba091",
"Description": "The server was unable to deliver your message (ex: unknown user, mailbox not found).",
"Details": "smtp;554 delivery error: dd This user doesn't have a yahoo.com account (vicelcown@yahoo.com) [0] - mta1543.mail.ne1.yahoo.com",
"Email": "vicelcown@yahoo.com",
"BouncedAt": "2013-10-18T09:49:59.8253577-04:00",
"DumpAvailable": false,
"Inactive": true,
"CanActivate": true,
"Subject": "Production API Test"
}
]
}`
tMux.HandleFunc(pat.Get("/bounces"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})
_, total, err := client.GetBounces(context.Background(), 100, 0, map[string]interface{}{
"tag": "Invitation",
})
if err != nil {
t.Fatalf("GetBounces: %s", err.Error())
}
if total != 253 {
t.Fatalf("GetBounces: wrong total (%d)", total)
}
}
func TestGetBounce(t *testing.T) {
responseJSON := `{
"ID": 692560173,
"Type": "HardBounce",
"TypeCode": 1,
"Name": "Hard bounce",
"Tag": "Invitation",
"MessageID": "2c1b63fe-43f2-4db5-91b0-8bdfa44a9316",
"Description": "The server was unable to deliver your message (ex: unknown user, mailbox not found).",
"Details": "action: failed\r\n",
"Email": "anything@blackhole.postmarkap.com",
"BouncedAt": "2014-01-15T16:09:19.6421112-05:00",
"DumpAvailable": false,
"Inactive": false,
"CanActivate": true,
"Subject": "SC API5 Test",
"Content": "Return-Path: <>\r\nReceived: …"
}`
tMux.HandleFunc(pat.Get("/bounces/692560173"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})
res, err := client.GetBounce(context.Background(), 692560173)
if err != nil {
t.Fatalf("GetBounce: %s", err.Error())
}
if res.ID != 692560173 {
t.Fatalf("GetBounce: wrong ID (%v)", res.ID)
}
}
func TestGetBounceDump(t *testing.T) {
responseJSON := `{
"Body": "..."
}`
tMux.HandleFunc(pat.Get("/bounces/692560173/dump"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})
res, err := client.GetBounceDump(context.Background(), 692560173)
if err != nil {
t.Fatalf("GetBounceDump: %s", err.Error())
}
if res != "..." {
t.Fatalf("GetBounceDump: wrong dump body (%v)", res)
}
}
func TestActivateBounce(t *testing.T) {
responseJSON := `{
"Message": "OK",
"Bounce": {
"ID": 692560173,
"Type": "HardBounce",
"TypeCode": 1,
"Name": "Hard bounce",
"Tag": "Invitation",
"MessageID": "2c1b63fe-43f2-4db5-91b0-8bdfa44a9316",
"Description": "The server was unable to deliver your message (ex: unknown user, mailbox not found).",
"Details": "action: failed\r\n",
"Email": "anything@blackhole.postmarkap.com",
"BouncedAt": "2014-01-15T16:09:19.6421112-05:00",
"DumpAvailable": false,
"Inactive": false,
"CanActivate": true,
"Subject": "SC API5 Test",
"Content": "Return-Path: <>\r\nReceived: …"
}
}`
tMux.HandleFunc(pat.Put("/bounces/692560173/activate"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})
res, mess, err := client.ActivateBounce(context.Background(), 692560173)
if err != nil {
t.Fatalf("ActivateBounce: %s", err.Error())
}
if res.ID != 692560173 {
t.Fatalf("ActivateBounce: wrong bounce ID (%v)", res.ID)
}
if mess != "OK" {
t.Fatalf("ActivateBounce: wrong message (%v)", mess)
}
}
func TestGetBouncedTags(t *testing.T) {
responseJSON := `[
"tag1",
"tag2",
"tag3"]
`
tMux.HandleFunc(pat.Get("/bounces/tags"), func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(responseJSON))
})
res, err := client.GetBouncedTags(context.Background())
if err != nil {
t.Fatalf("GetBouncedTags: %s", err.Error())
}
if len(res) != 3 {
t.Fatalf("GetBouncedTags: wrong tag result (%v)", res)
}
}