-
Notifications
You must be signed in to change notification settings - Fork 143
/
mock_bounces.go
196 lines (167 loc) · 4.53 KB
/
mock_bounces.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
package mailgun
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/go-chi/chi/v5"
)
func (ms *mockServer) addBouncesRoutes(r chi.Router) {
r.Get("/{domain}/bounces", ms.listBounces)
r.Get("/{domain}/bounces/{address}", ms.getBounce)
r.Delete("/{domain}/bounces/{address}", ms.deleteBounce)
r.Delete("/{domain}/bounces", ms.deleteBouncesList)
r.Post("/{domain}/bounces", ms.createBounce)
ms.bounces = append(ms.bounces, Bounce{
CreatedAt: RFC2822Time(time.Now()),
Error: "invalid address",
Code: "INVALID",
Address: "[email protected]",
})
ms.bounces = append(ms.bounces, Bounce{
CreatedAt: RFC2822Time(time.Now()),
Error: "non existing address",
Code: "NOT_EXIST",
Address: "[email protected]",
})
}
func (ms *mockServer) listBounces(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
var idx []string
for _, t := range ms.bounces {
idx = append(idx, t.Address)
}
limit := stringToInt(r.FormValue("limit"))
if limit == 0 {
limit = 100
}
page := r.FormValue("page")
var pivot string
if len(page) != 0 {
pivot = r.FormValue("p")
if pivot == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("{\"message\": \"Invalid parameter: pivot \"}"))
return
}
}
start, end := pageOffsets(idx, page, pivot, limit)
var nextAddress, prevAddress string
var results []Bounce
if start != end {
results = ms.bounces[start:end]
nextAddress = results[len(results)-1].Address
prevAddress = results[0].Address
} else {
results = []Bounce{}
nextAddress = pivot
prevAddress = pivot
}
toJSON(w, bouncesListResponse{
Paging: Paging{
First: getPageURL(r, url.Values{
"page": []string{"first"},
}),
Last: getPageURL(r, url.Values{
"page": []string{"last"},
}),
Next: getPageURL(r, url.Values{
"page": []string{"next"},
"p": []string{nextAddress},
}),
Previous: getPageURL(r, url.Values{
"page": []string{"prev"},
"p": []string{prevAddress},
}),
},
Items: results,
})
}
func (ms *mockServer) getBounce(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
for _, bounce := range ms.bounces {
if bounce.Address == chi.URLParam(r, "address") {
toJSON(w, bounce)
return
}
}
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("{\"message\": \"Address not found in bounces table\"}"))
}
func (ms *mockServer) createBounce(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
var bounces []Bounce
if r.Header.Get("Content-Type") == "application/json" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("{\"message\": \"Can't read request body\"}"))
return
}
err = json.Unmarshal(body, &bounces)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("{\"message\": \"Invalid json: %s\"}", err.Error())))
return
}
} else {
if err := r.ParseForm(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
bounceError := r.FormValue("error")
code := r.FormValue("code")
address := r.FormValue("address")
if len(address) == 0 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("{\"message\": \"Invalid format for parameter address: \"}"))
return
}
bounces = append(bounces, Bounce{Address: address, Code: code, Error: bounceError})
}
for _, bounce := range bounces {
var addressExist bool
for _, existingBounce := range ms.bounces {
if existingBounce.Address == bounce.Address {
addressExist = true
}
}
if !addressExist {
ms.bounces = append(ms.bounces, bounce)
}
}
toJSON(w, map[string]interface{}{
"message": "Address has been added to the bounces table",
"address": fmt.Sprint(bounces),
})
}
func (ms *mockServer) deleteBounce(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
for i, bounce := range ms.bounces {
if bounce.Address == chi.URLParam(r, "address") {
ms.bounces = append(ms.bounces[:i], ms.bounces[i+1:len(ms.bounces)]...)
toJSON(w, map[string]interface{}{
"message": "Bounce has been removed",
})
return
}
}
toJSON(w, map[string]interface{}{
"message": "Address not found in bounces table",
})
}
func (ms *mockServer) deleteBouncesList(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
ms.bounces = []Bounce{}
toJSON(w, map[string]interface{}{
"message": "All bounces has been deleted",
})
}