forked from mailgun/mailgun-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.go
284 lines (248 loc) · 5.79 KB
/
mock.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package mailgun
import (
"crypto/rand"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/mail"
"net/url"
"strconv"
"strings"
"sync"
"github.com/go-chi/chi/v5"
)
type MockServer interface {
Stop()
URL4() string
URL() string
DomainIPS() []string
DomainList() []DomainContainer
ExportList() []Export
MailingList() []MailingListContainer
RouteList() []Route
Events() []Event
Webhooks() WebHooksListResponse
Templates() []Template
}
// A mailgun api mock suitable for testing
type mockServer struct {
srv *httptest.Server
domainIPS []string
domainList []DomainContainer
exportList []Export
mailingList []MailingListContainer
routeList []Route
events []Event
templates []Template
templateVersions map[string][]TemplateVersion
unsubscribes []Unsubscribe
complaints []Complaint
bounces []Bounce
credentials []Credential
stats []Stats
tags []Tag
webhooks WebHooksListResponse
mutex sync.Mutex
}
func (ms *mockServer) DomainIPS() []string {
defer ms.mutex.Unlock()
ms.mutex.Lock()
return ms.domainIPS
}
func (ms *mockServer) DomainList() []DomainContainer {
defer ms.mutex.Unlock()
ms.mutex.Lock()
return ms.domainList
}
func (ms *mockServer) ExportList() []Export {
defer ms.mutex.Unlock()
ms.mutex.Lock()
return ms.exportList
}
func (ms *mockServer) MailingList() []MailingListContainer {
defer ms.mutex.Unlock()
ms.mutex.Lock()
return ms.mailingList
}
func (ms *mockServer) RouteList() []Route {
defer ms.mutex.Unlock()
ms.mutex.Lock()
return ms.routeList
}
func (ms *mockServer) Events() []Event {
defer ms.mutex.Unlock()
ms.mutex.Lock()
return ms.events
}
func (ms *mockServer) Webhooks() WebHooksListResponse {
defer ms.mutex.Unlock()
ms.mutex.Lock()
return ms.webhooks
}
func (ms *mockServer) Templates() []Template {
defer ms.mutex.Unlock()
ms.mutex.Lock()
return ms.templates
}
func (ms *mockServer) Unsubscribes() []Unsubscribe {
defer ms.mutex.Unlock()
ms.mutex.Lock()
return ms.unsubscribes
}
// Create a new instance of the mailgun API mock server
func NewMockServer() MockServer {
ms := mockServer{}
// Add all our handlers
r := chi.NewRouter()
r.Route("/v3", func(r chi.Router) {
ms.addIPRoutes(r)
ms.addExportRoutes(r)
ms.addDomainRoutes(r)
ms.addMailingListRoutes(r)
ms.addEventRoutes(r)
ms.addMessagesRoutes(r)
ms.addRoutes(r)
ms.addWebhookRoutes(r)
ms.addTemplateRoutes(r)
ms.addTemplateVersionRoutes(r)
ms.addUnsubscribesRoutes(r)
ms.addComplaintsRoutes(r)
ms.addBouncesRoutes(r)
ms.addCredentialsRoutes(r)
ms.addStatsRoutes(r)
ms.addTagsRoutes(r)
})
ms.addValidationRoutes(r)
// Start the server
ms.srv = httptest.NewServer(r)
return &ms
}
// Stop the server
func (ms *mockServer) Stop() {
ms.srv.Close()
}
func (ms *mockServer) URL4() string {
return ms.srv.URL + "/v4"
}
// URL returns the URL used to connect to the mock server
func (ms *mockServer) URL() string {
return ms.srv.URL + "/v3"
}
func toJSON(w http.ResponseWriter, obj interface{}) {
if err := json.NewEncoder(w).Encode(obj); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "application/json")
}
func stringToBool(v string) bool {
lower := strings.ToLower(v)
if lower == "yes" || lower == "no" {
return lower == "yes"
}
if v == "" {
return false
}
result, err := strconv.ParseBool(v)
if err != nil {
panic(err)
}
return result
}
func stringToInt(v string) int {
if v == "" {
return 0
}
result, err := strconv.ParseInt(v, 10, 64)
if err != nil {
panic(err)
}
return int(result)
}
func stringToMap(v string) map[string]interface{} {
if v == "" {
return nil
}
result := make(map[string]interface{})
err := json.Unmarshal([]byte(v), &result)
if err != nil {
panic(err)
}
return result
}
func parseAddress(v string) string {
if v == "" {
return ""
}
e, err := mail.ParseAddress(v)
if err != nil {
panic(err)
}
return e.Address
}
// Given the page direction, pivot value and limit, calculate the offsets for the slice
func pageOffsets(pivotIdx []string, pivotDir, pivotVal string, limit int) (int, int) {
switch pivotDir {
case "first":
if limit < len(pivotIdx) {
return 0, limit
}
return 0, len(pivotIdx)
case "last":
if limit < len(pivotIdx) {
return len(pivotIdx) - limit, len(pivotIdx)
}
return 0, len(pivotIdx)
case "next":
for i, item := range pivotIdx {
if item == pivotVal {
offset := i + 1 + limit
if offset > len(pivotIdx) {
offset = len(pivotIdx)
}
return i + 1, offset
}
}
return 0, 0
case "prev":
for i, item := range pivotIdx {
if item == pivotVal {
if i == 0 {
return 0, 0
}
offset := i - limit
if offset < 0 {
offset = 0
}
return offset, i
}
}
return 0, 0
}
if limit > len(pivotIdx) {
return 0, len(pivotIdx)
}
return 0, limit
}
func getPageURL(r *http.Request, params url.Values) string {
if r.FormValue("limit") != "" {
params.Add("limit", r.FormValue("limit"))
}
return "http://" + r.Host + r.URL.EscapedPath() + "?" + params.Encode()
}
// randomString generates a string of given length, but random content.
// All content will be within the ASCII graphic character set.
// (Implementation from Even Shaw's contribution on
// http://stackoverflow.com/questions/12771930/what-is-the-fastest-way-to-generate-a-long-random-string-in-go).
func randomString(n int, prefix string) string {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
return prefix + string(bytes)
}
func randomEmail(prefix, domain string) string {
return strings.ToLower(fmt.Sprintf("%s@%s", randomString(20, prefix), domain))
}