forked from mailgun/mailgun-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events_test.go
164 lines (138 loc) · 4.74 KB
/
events_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
package mailgun_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/facebookgo/ensure"
"github.com/mailgun/mailgun-go/v4"
"github.com/mailgun/mailgun-go/v4/events"
)
func TestEventIteratorGetNext(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
it := mg.ListEvents(&mailgun.ListEventOptions{Limit: 5})
var firstPage, secondPage, previousPage []mailgun.Event
var ctx = context.Background()
ensure.True(t, it.Next(ctx, &firstPage))
ensure.True(t, it.Paging.Next != "")
ensure.True(t, len(firstPage) != 0)
firstIterator := *it
ensure.True(t, it.Next(ctx, &secondPage))
ensure.True(t, len(secondPage) != 0)
// Pages should be different
ensure.NotDeepEqual(t, firstPage, secondPage)
ensure.True(t, firstIterator.Paging.Next != it.Paging.Next)
ensure.True(t, firstIterator.Paging.Previous != it.Paging.Previous)
ensure.Nil(t, it.Err())
// Previous()
ensure.True(t, it.First(ctx, &firstPage))
ensure.True(t, it.Next(ctx, &secondPage))
ensure.True(t, it.Previous(ctx, &previousPage))
ensure.True(t, len(previousPage) != 0)
ensure.DeepEqual(t, previousPage[0].GetID(), firstPage[0].GetID())
// First()
ensure.True(t, it.First(ctx, &firstPage))
ensure.True(t, len(firstPage) != 0)
// Calling first resets the iterator to the first page
ensure.True(t, it.Next(ctx, &secondPage))
ensure.NotDeepEqual(t, firstPage, secondPage)
// Last()
var lastPage []mailgun.Event
ensure.True(t, it.Next(ctx, &firstPage))
ensure.True(t, len(firstPage) != 0)
// Calling Last() is invalid unless you first use First() or Next()
ensure.True(t, it.Last(ctx, &lastPage))
ensure.True(t, len(lastPage) != 0)
}
func TestEventPoller(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
// Very short poll interval
it := mg.PollEvents(&mailgun.ListEventOptions{
// Only events with a timestamp after this date/time will be returned
Begin: time.Now().Add(time.Second * -3),
// How often we poll the api for new events
PollInterval: time.Second * 4})
eventChan := make(chan mailgun.Event, 1)
ctx, cancel := context.WithCancel(context.Background())
go func() {
// Poll until our email event arrives
var page []mailgun.Event
for it.Poll(ctx, &page) {
for _, e := range page {
eventChan <- e
}
}
close(eventChan)
}()
// Send an email
m := mg.NewMessage("root@"+testDomain, "Subject", "Text Body", "user@"+testDomain)
msg, id, err := mg.Send(ctx, m)
ensure.Nil(t, err)
t.Logf("New Email: %s Id: %s\n", msg, id)
var accepted *events.Accepted
for e := range eventChan {
switch event := e.(type) {
case *events.Accepted:
t.Logf("Accepted Event: %s - %v", event.Message.Headers.MessageID, event.GetTimestamp())
// If we find our accepted email event
if id == ("<" + event.Message.Headers.MessageID + ">") {
accepted = event
cancel()
}
}
}
// Ensure we found our email
ensure.NotNil(t, it.Err())
ensure.True(t, accepted != nil)
ensure.DeepEqual(t, accepted.Recipient, "user@"+testDomain)
}
func ExampleMailgunImpl_ListEvents() {
mg := mailgun.NewMailgun("your-domain.com", "your-api-key")
mg.SetAPIBase(server.URL())
it := mg.ListEvents(&mailgun.ListEventOptions{Limit: 100})
var page []mailgun.Event
// The entire operation should not take longer than 30 seconds
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
// For each page of 100 events
for it.Next(ctx, &page) {
for _, e := range page {
// You can access some fields via the interface
//fmt.Printf("Event: '%s' TimeStamp: '%s'\n", e.GetName(), e.GetTimestamp())
// and you can act upon each event by type
switch event := e.(type) {
case *events.Accepted:
fmt.Printf("Accepted: auth: %t\n", event.Flags.IsAuthenticated)
case *events.Delivered:
fmt.Printf("Delivered transport: %s\n", event.Envelope.Transport)
case *events.Failed:
fmt.Printf("Failed reason: %s\n", event.Reason)
case *events.Clicked:
fmt.Printf("Clicked GeoLocation: %s\n", event.GeoLocation.Country)
case *events.Opened:
fmt.Printf("Opened GeoLocation: %s\n", event.GeoLocation.Country)
case *events.Rejected:
fmt.Printf("Rejected reason: %s\n", event.Reject.Reason)
case *events.Stored:
fmt.Printf("Stored URL: %s\n", event.Storage.URL)
case *events.Unsubscribed:
fmt.Printf("Unsubscribed client OS: %s\n", event.ClientInfo.ClientOS)
}
}
}
// Accepted: auth: false
// Accepted: auth: true
// Delivered transport: smtp
// Delivered transport: http
// Stored URL: http://mailgun.text/some/url
// Clicked GeoLocation: US
// Clicked GeoLocation: US
// Clicked GeoLocation: US
// Opened GeoLocation: US
// Opened GeoLocation: US
// Opened GeoLocation: US
// Unsubscribed client OS: OS X
// Unsubscribed client OS: OS X
}