-
Notifications
You must be signed in to change notification settings - Fork 143
/
mock_messages.go
157 lines (138 loc) · 4.04 KB
/
mock_messages.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
package mailgun
import (
"net/http"
"net/mail"
"strings"
"time"
"github.com/go-chi/chi/v5"
"github.com/mailgun/mailgun-go/v4/events"
)
func (ms *mockServer) addMessagesRoutes(r chi.Router) {
r.Post("/{domain}/messages", ms.createMessages)
// This path is made up; it could be anything as the storage url could change over time
r.Get("/se.storage.url/messages/{id}", ms.getStoredMessages)
r.Post("/se.storage.url/messages/{id}", ms.sendStoredMessages)
}
// TODO: This implementation doesn't support multiple recipients
func (ms *mockServer) createMessages(w http.ResponseWriter, r *http.Request) {
to, err := mail.ParseAddress(r.FormValue("to"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
toJSON(w, okResp{Message: "invalid 'to' address"})
return
}
id := randomString(16, "ID-")
switch to.Address {
case "[email protected]":
stored := new(events.Stored)
stored.Name = events.EventStored
stored.Timestamp = TimeToFloat(time.Now().UTC())
stored.ID = id
stored.Storage.URL = ms.URL() + "/se.storage.url/messages/" + id
stored.Storage.Key = id
stored.Message.Headers = events.MessageHeaders{
Subject: r.FormValue("subject"),
From: r.FormValue("from"),
To: to.Address,
MessageID: id,
}
stored.Message.Recipients = []string{
r.FormValue("to"),
}
stored.Message.Size = 10
stored.Flags = events.Flags{
IsTestMode: false,
}
ms.mutex.Lock()
ms.events = append(ms.events, stored)
ms.mutex.Unlock()
default:
accepted := new(events.Accepted)
accepted.Name = events.EventAccepted
accepted.ID = id
accepted.Timestamp = TimeToFloat(time.Now().UTC())
accepted.Message.Headers.From = r.FormValue("from")
accepted.Message.Headers.To = r.FormValue("to")
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{
IsAuthenticated: true,
}
ms.mutex.Lock()
ms.events = append(ms.events, accepted)
ms.mutex.Unlock()
}
tags := r.Form["o:tag"]
for _, newTag := range tags {
var tagExists bool
for _, existingTag := range ms.tags {
if newTag == existingTag.Value {
tagExists = true
break
}
}
if !tagExists {
ms.tags = append(ms.tags, Tag{Value: newTag})
}
}
toJSON(w, okResp{ID: "<" + id + ">", Message: "Queued. Thank you."})
}
func (ms *mockServer) getStoredMessages(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
defer ms.mutex.Unlock()
ms.mutex.Lock()
// Find our stored event
var stored *events.Stored
for _, event := range ms.events {
if event.GetID() == id {
stored = event.(*events.Stored)
}
}
if stored == nil {
w.WriteHeader(http.StatusNotFound)
toJSON(w, okResp{Message: "not found"})
}
toJSON(w, StoredMessage{
Recipients: strings.Join(stored.Message.Recipients, ","),
Sender: stored.Message.Headers.From,
Subject: stored.Message.Headers.Subject,
From: stored.Message.Headers.From,
MessageHeaders: [][]string{
{"Sender", stored.Message.Headers.From},
{"To", stored.Message.Headers.To},
{"Subject", stored.Message.Headers.Subject},
{"Content-Type", "text/plain"},
},
})
}
func (ms *mockServer) sendStoredMessages(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
defer ms.mutex.Unlock()
ms.mutex.Lock()
// Find our stored event
var stored *events.Stored
for _, event := range ms.events {
if event.GetID() == id {
stored = event.(*events.Stored)
}
}
if stored == nil {
w.WriteHeader(http.StatusNotFound)
toJSON(w, okResp{Message: "not found"})
}
// DO NOTHING
toJSON(w, okResp{ID: "<" + id + ">", Message: "Queued. Thank you."})
}