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
|
package provider_test
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"sync"
"testing"
)
const (
testAPIKey = "pk1_test"
testSecretKey = "sk1_test"
)
type fakeRecord struct {
name string
typ string
content string
ttl int
prio int
}
type fakePorkbun struct {
t *testing.T
mu sync.Mutex
nextID int
records map[string]map[string]*fakeRecord
createCalls, editCalls, deleteCalls, retrieveCalls int
}
func newFakePorkbun(t *testing.T) *fakePorkbun {
return &fakePorkbun{t: t, nextID: 1000, records: map[string]map[string]*fakeRecord{}}
}
func (f *fakePorkbun) seed(domain string, rec fakeRecord) string {
f.mu.Lock()
defer f.mu.Unlock()
f.nextID++
id := strconv.Itoa(f.nextID)
if f.records[domain] == nil {
f.records[domain] = map[string]*fakeRecord{}
}
f.records[domain][id] = &rec
return id
}
func (f *fakePorkbun) record(domain, id string) *fakeRecord {
f.mu.Lock()
defer f.mu.Unlock()
rec, ok := f.records[domain][id]
if !ok {
return nil
}
cp := *rec
return &cp
}
func (f *fakePorkbun) recordCount() int {
f.mu.Lock()
defer f.mu.Unlock()
n := 0
for _, recs := range f.records {
n += len(recs)
}
return n
}
func (f *fakePorkbun) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.mu.Lock()
defer f.mu.Unlock()
var body map[string]any
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
f.fail(w, http.StatusBadRequest, "Invalid JSON.")
return
}
if body["apikey"] != testAPIKey || body["secretapikey"] != testSecretKey {
f.fail(w, http.StatusForbidden, "Invalid API key. (002)")
return
}
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
switch {
case len(parts) == 1 && parts[0] == "ping":
f.ok(w, map[string]any{"yourIp": "203.0.113.7"})
case len(parts) == 3 && parts[0] == "dns" && parts[1] == "create":
f.createCalls++
domain := parts[2]
f.nextID++
id := strconv.Itoa(f.nextID)
if f.records[domain] == nil {
f.records[domain] = map[string]*fakeRecord{}
}
f.records[domain][id] = &fakeRecord{
name: str(body["name"]),
typ: str(body["type"]),
content: str(body["content"]),
ttl: atoi(f.t, str(body["ttl"])),
prio: atoi(f.t, str(body["prio"])),
}
f.ok(w, map[string]any{"id": f.nextID})
case len(parts) == 4 && parts[0] == "dns" && parts[1] == "edit":
f.editCalls++
rec, ok := f.records[parts[2]][parts[3]]
if !ok {
f.fail(w, http.StatusBadRequest, "Invalid record ID.")
return
}
rec.typ = str(body["type"])
rec.content = str(body["content"])
rec.ttl = atoi(f.t, str(body["ttl"]))
rec.prio = atoi(f.t, str(body["prio"]))
f.ok(w, nil)
case len(parts) == 4 && parts[0] == "dns" && parts[1] == "delete":
f.deleteCalls++
if _, ok := f.records[parts[2]][parts[3]]; !ok {
f.fail(w, http.StatusBadRequest, "Invalid record ID.")
return
}
delete(f.records[parts[2]], parts[3])
f.ok(w, nil)
case len(parts) == 4 && parts[0] == "dns" && parts[1] == "retrieve":
f.retrieveCalls++
domain, id := parts[2], parts[3]
records := []map[string]any{}
if rec, ok := f.records[domain][id]; ok {
fullName := domain
if rec.name != "" {
fullName = rec.name + "." + domain
}
records = append(records, map[string]any{
"id": id, "name": fullName, "type": rec.typ, "content": rec.content,
"ttl": strconv.Itoa(rec.ttl), "prio": strconv.Itoa(rec.prio), "notes": "",
})
}
f.ok(w, map[string]any{"records": records})
default:
f.fail(w, http.StatusNotFound, fmt.Sprintf("Unknown endpoint %q.", r.URL.Path))
}
}
func (f *fakePorkbun) ok(w http.ResponseWriter, extra map[string]any) {
resp := map[string]any{"status": "SUCCESS"}
for k, v := range extra {
resp[k] = v
}
json.NewEncoder(w).Encode(resp)
}
func (f *fakePorkbun) fail(w http.ResponseWriter, code int, message string) {
w.WriteHeader(code)
json.NewEncoder(w).Encode(map[string]any{"status": "ERROR", "message": message})
}
func str(v any) string {
s, _ := v.(string)
return s
}
func atoi(t *testing.T, s string) int {
t.Helper()
if s == "" {
return 0
}
n, err := strconv.Atoi(s)
if err != nil {
t.Errorf("fake porkbun: expected numeric string, got %q", s)
}
return n
}
|