-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathmock.go
194 lines (169 loc) · 3.91 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
package mailgun
import (
"crypto/rand"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/mail"
"net/url"
"strconv"
"strings"
"github.com/go-chi/chi"
)
// 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
webhooks WebHooksListResponse
}
// 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.addValidationRoutes(r)
ms.addRoutes(r)
ms.addWebhookRoutes(r)
})
// Start the server
ms.srv = httptest.NewServer(r)
return ms
}
// Stop the server
func (ms *MockServer) Stop() {
ms.srv.Close()
}
// 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))
}