Repository: mailgun/mailgun-go
Pull request #250 (2021-02-22 19:02:50)

Added test for multiple attachments
Attempt to duplicate reported issue #246 


Patch:
diff --git a/CHANGELOG b/CHANGELOG
index 2b8f584..7dbe89f 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
+## [4.3.4] - 2021-02-22
+### Changed
+* Paging through results now clears the previous result before new fetch
+* Added test for multiple attachments
+
 ## [4.3.3] - 2021-01-29
 ### Added
 * Added UpdateDomainTrackingWebPrefix()
diff --git a/mock_messages.go b/mock_messages.go
index a9467ec..4b806d6 100644
--- a/mock_messages.go
+++ b/mock_messages.go
@@ -60,6 +60,17 @@ func (ms *MockServer) createMessages(w http.ResponseWriter, r *http.Request) {
 		accepted.Message.Headers.MessageID = accepted.ID
 		accepted.Message.Headers.Subject = r.FormValue("subject")
 
+		if r.MultipartForm.File != nil {
+			for _, fh := range r.MultipartForm.File {
+				for _, fd := range fh {
+					accepted.Message.Attachments = append(accepted.Message.Attachments, events.Attachment{
+						FileName:    fd.Filename,
+						ContentType: fd.Header.Get("Content-Type"),
+						Size:        int(fd.Size),
+					})
+				}
+			}
+		}
 		accepted.Recipient = r.FormValue("to")
 		accepted.RecipientDomain = strings.Split(to.Address, "@")[1]
 		accepted.Flags = events.Flags{


Test patch:
diff --git a/attachments_test.go b/attachments_test.go
new file mode 100644
index 0000000..74d9d4e
--- /dev/null
+++ b/attachments_test.go
@@ -0,0 +1,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)
+}