-
Notifications
You must be signed in to change notification settings - Fork 146
/
events_test.go
166 lines (140 loc) · 4.83 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
165
166
package mailgun_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/mailgun/mailgun-go/v4"
"github.com/mailgun/mailgun-go/v4/events"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
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()
require.True(t, it.Next(ctx, &firstPage))
require.NotEqual(t, "", it.Paging.Next)
require.True(t, len(firstPage) != 0)
firstIterator := *it
require.True(t, it.Next(ctx, &secondPage))
require.True(t, len(secondPage) != 0)
// Pages should be different
require.NotEqual(t, firstPage, secondPage)
require.NotEqual(t, firstIterator.Paging.Next, it.Paging.Next)
require.NotEqual(t, firstIterator.Paging.Previous, it.Paging.Previous)
require.NoError(t, it.Err())
// Previous()
require.True(t, it.First(ctx, &firstPage))
require.True(t, it.Next(ctx, &secondPage))
require.True(t, it.Previous(ctx, &previousPage))
require.True(t, len(previousPage) != 0)
require.Equal(t, previousPage[0].GetID(), firstPage[0].GetID())
// First()
require.True(t, it.First(ctx, &firstPage))
require.True(t, len(firstPage) != 0)
// Calling first resets the iterator to the first page
require.True(t, it.Next(ctx, &secondPage))
require.NotEqual(t, firstPage, secondPage)
// Last()
var lastPage []mailgun.Event
require.True(t, it.Next(ctx, &firstPage))
require.True(t, len(firstPage) != 0)
// Calling Last() is invalid unless you first use First() or Next()
require.True(t, it.Last(ctx, &lastPage))
require.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())
defer cancel()
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 := mailgun.NewMessage("root@"+testDomain, "Subject", "Text Body", "user@"+testDomain)
msg, id, err := mg.Send(ctx, m)
require.NoError(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
require.NotNil(t, it.Err())
require.NotNil(t, accepted)
assert.Equal(t, "user@"+testDomain, accepted.Recipient)
}
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
}