-
Notifications
You must be signed in to change notification settings - Fork 143
/
mock_ips.go
71 lines (61 loc) · 1.66 KB
/
mock_ips.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
package mailgun
import (
"net/http"
"github.com/go-chi/chi/v5"
)
func (ms *mockServer) addIPRoutes(r chi.Router) {
r.Get("/ips", ms.listIPS)
r.Get("/ips/{ip}", ms.getIPAddress)
r.Route("/domains/{domain}/ips", func(r chi.Router) {
r.Get("/", ms.listDomainIPS)
r.Get("/{ip}", ms.getIPAddress)
r.Post("/", ms.postDomainIPS)
r.Delete("/{ip}", ms.deleteDomainIPS)
})
}
func (ms *mockServer) listIPS(w http.ResponseWriter, _ *http.Request) {
toJSON(w, ipAddressListResponse{
TotalCount: 2,
Items: []string{"172.0.0.1", "192.168.1.1"},
})
}
func (ms *mockServer) getIPAddress(w http.ResponseWriter, r *http.Request) {
toJSON(w, IPAddress{
IP: chi.URLParam(r, "ip"),
RDNS: "luna.mailgun.net",
Dedicated: true,
})
}
func (ms *mockServer) listDomainIPS(w http.ResponseWriter, _ *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
toJSON(w, ipAddressListResponse{
TotalCount: 2,
Items: ms.domainIPS,
})
}
func (ms *mockServer) postDomainIPS(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
ms.domainIPS = append(ms.domainIPS, r.FormValue("ip"))
toJSON(w, okResp{Message: "success"})
}
func (ms *mockServer) deleteDomainIPS(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
result := ms.domainIPS[:0]
for _, ip := range ms.domainIPS {
if ip == chi.URLParam(r, "ip") {
continue
}
result = append(result, ip)
}
if len(result) != len(ms.domainIPS) {
toJSON(w, okResp{Message: "success"})
ms.domainIPS = result
return
}
// Not the actual error returned by the mailgun API
w.WriteHeader(http.StatusNotFound)
toJSON(w, okResp{Message: "ip not found"})
}