forked from mailgun/mailgun-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attachments_test.go
71 lines (57 loc) · 1.61 KB
/
attachments_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
package mailgun_test
import (
"context"
"fmt"
"os"
"strings"
"testing"
"github.com/facebookgo/ensure"
"github.com/mailgun/mailgun-go/v4"
"github.com/mailgun/mailgun-go/v4/events"
)
func createAttachment(t *testing.T) string {
t.Helper()
name := "/tmp/" + randomString(10, "attachment-")
f, err := os.Create(name)
ensure.Nil(t, err)
_, err = f.Write([]byte(randomString(100, "")))
ensure.Nil(t, err)
ensure.Nil(t, f.Close())
return name
}
func TestMultipleAttachments(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
var ctx = context.Background()
m := mg.NewMessage("root@"+testDomain, "Subject", "Text Body", "attachment@"+testDomain)
// Add 2 attachments
m.AddAttachment(createAttachment(t))
m.AddAttachment(createAttachment(t))
msg, id, err := mg.Send(ctx, m)
ensure.Nil(t, err)
id = strings.Trim(id, "<>")
t.Logf("New Email: %s Id: %s\n", msg, id)
e, err := findAcceptedMessage(mg, id)
ensure.NotNil(t, e)
ensure.DeepEqual(t, e.ID, id)
ensure.DeepEqual(t, len(e.Message.Attachments), 2)
for _, f := range e.Message.Attachments {
t.Logf("attachment: %v\n", f)
ensure.DeepEqual(t, f.Size, 100)
}
}
func findAcceptedMessage(mg mailgun.Mailgun, id string) (*events.Accepted, error) {
it := mg.ListEvents(nil)
var page []mailgun.Event
for it.Next(context.Background(), &page) {
for _, event := range page {
if event.GetName() == events.EventAccepted && event.GetID() == id {
return event.(*events.Accepted), nil
}
}
}
if it.Err() != nil {
return nil, it.Err()
}
return nil, fmt.Errorf("no accepted messages found for '%s'", id)
}