forked from digitalocean/godo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domains_test.go
352 lines (289 loc) · 8.85 KB
/
domains_test.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package godo
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestDomains_ListDomains(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/domains", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"domains": [{"name":"foo.com"},{"name":"bar.com"}]}`)
})
domains, _, err := client.Domains.List(ctx, nil)
if err != nil {
t.Errorf("Domains.List returned error: %v", err)
}
expected := []Domain{{Name: "foo.com"}, {Name: "bar.com"}}
if !reflect.DeepEqual(domains, expected) {
t.Errorf("Domains.List returned %+v, expected %+v", domains, expected)
}
}
func TestDomains_ListDomainsMultiplePages(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/domains", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"domains": [{"id":1},{"id":2}], "links":{"pages":{"next":"http://example.com/v2/domains/?page=2"}}}`)
})
_, resp, err := client.Domains.List(ctx, nil)
if err != nil {
t.Fatal(err)
}
checkCurrentPage(t, resp, 1)
}
func TestDomains_RetrievePageByNumber(t *testing.T) {
setup()
defer teardown()
jBlob := `
{
"domains": [{"id":1},{"id":2}],
"links":{
"pages":{
"next":"http://example.com/v2/domains/?page=3",
"prev":"http://example.com/v2/domains/?page=1",
"last":"http://example.com/v2/domains/?page=3",
"first":"http://example.com/v2/domains/?page=1"
}
}
}`
mux.HandleFunc("/v2/domains", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
opt := &ListOptions{Page: 2}
_, resp, err := client.Domains.List(ctx, opt)
if err != nil {
t.Fatal(err)
}
checkCurrentPage(t, resp, 2)
}
func TestDomains_GetDomain(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/domains/example.com", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"domain":{"name":"example.com"}}`)
})
domains, _, err := client.Domains.Get(ctx, "example.com")
if err != nil {
t.Errorf("domain.Get returned error: %v", err)
}
expected := &Domain{Name: "example.com"}
if !reflect.DeepEqual(domains, expected) {
t.Errorf("domains.Get returned %+v, expected %+v", domains, expected)
}
}
func TestDomains_Create(t *testing.T) {
setup()
defer teardown()
createRequest := &DomainCreateRequest{
Name: "example.com",
IPAddress: "127.0.0.1",
}
mux.HandleFunc("/v2/domains", func(w http.ResponseWriter, r *http.Request) {
v := new(DomainCreateRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatal(err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, createRequest) {
t.Errorf("Request body = %+v, expected %+v", v, createRequest)
}
fmt.Fprint(w, `{"domain":{"name":"example.com"}}`)
})
domain, _, err := client.Domains.Create(ctx, createRequest)
if err != nil {
t.Errorf("Domains.Create returned error: %v", err)
}
expected := &Domain{Name: "example.com"}
if !reflect.DeepEqual(domain, expected) {
t.Errorf("Domains.Create returned %+v, expected %+v", domain, expected)
}
}
func TestDomains_Destroy(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/domains/example.com", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.Domains.Delete(ctx, "example.com")
if err != nil {
t.Errorf("Domains.Delete returned error: %v", err)
}
}
func TestDomains_AllRecordsForDomainName(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/domains/example.com/records", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"domain_records":[{"id":1},{"id":2}]}`)
})
records, _, err := client.Domains.Records(ctx, "example.com", nil)
if err != nil {
t.Errorf("Domains.List returned error: %v", err)
}
expected := []DomainRecord{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(records, expected) {
t.Errorf("Domains.List returned %+v, expected %+v", records, expected)
}
}
func TestDomains_AllRecordsForDomainName_PerPage(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/domains/example.com/records", func(w http.ResponseWriter, r *http.Request) {
perPage := r.URL.Query().Get("per_page")
if perPage != "2" {
t.Fatalf("expected '2', got '%s'", perPage)
}
fmt.Fprint(w, `{"domain_records":[{"id":1},{"id":2}]}`)
})
dro := &ListOptions{PerPage: 2}
records, _, err := client.Domains.Records(ctx, "example.com", dro)
if err != nil {
t.Errorf("Domains.List returned error: %v", err)
}
expected := []DomainRecord{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(records, expected) {
t.Errorf("Domains.List returned %+v, expected %+v", records, expected)
}
}
func TestDomains_GetRecordforDomainName(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/domains/example.com/records/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"domain_record":{"id":1}}`)
})
record, _, err := client.Domains.Record(ctx, "example.com", 1)
if err != nil {
t.Errorf("Domains.GetRecord returned error: %v", err)
}
expected := &DomainRecord{ID: 1}
if !reflect.DeepEqual(record, expected) {
t.Errorf("Domains.GetRecord returned %+v, expected %+v", record, expected)
}
}
func TestDomains_DeleteRecordForDomainName(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/domains/example.com/records/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.Domains.DeleteRecord(ctx, "example.com", 1)
if err != nil {
t.Errorf("Domains.RecordDelete returned error: %v", err)
}
}
func TestDomains_CreateRecordForDomainName(t *testing.T) {
setup()
defer teardown()
createRequest := &DomainRecordEditRequest{
Type: "CNAME",
Name: "example",
Data: "@",
Priority: 10,
Port: 10,
TTL: 1800,
Weight: 10,
Flags: 1,
Tag: "test",
}
mux.HandleFunc("/v2/domains/example.com/records",
func(w http.ResponseWriter, r *http.Request) {
v := new(DomainRecordEditRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, http.MethodPost)
if !reflect.DeepEqual(v, createRequest) {
t.Errorf("Request body = %+v, expected %+v", v, createRequest)
}
fmt.Fprintf(w, `{"domain_record": {"id":1}}`)
})
record, _, err := client.Domains.CreateRecord(ctx, "example.com", createRequest)
if err != nil {
t.Errorf("Domains.CreateRecord returned error: %v", err)
}
expected := &DomainRecord{ID: 1}
if !reflect.DeepEqual(record, expected) {
t.Errorf("Domains.CreateRecord returned %+v, expected %+v", record, expected)
}
}
func TestDomains_EditRecordForDomainName(t *testing.T) {
setup()
defer teardown()
editRequest := &DomainRecordEditRequest{
Type: "CNAME",
Name: "example",
Data: "@",
Priority: 10,
Port: 10,
TTL: 1800,
Weight: 10,
Flags: 1,
Tag: "test",
}
mux.HandleFunc("/v2/domains/example.com/records/1", func(w http.ResponseWriter, r *http.Request) {
v := new(DomainRecordEditRequest)
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
testMethod(t, r, http.MethodPut)
if !reflect.DeepEqual(v, editRequest) {
t.Errorf("Request body = %+v, expected %+v", v, editRequest)
}
fmt.Fprintf(w, `{"domain_record": {"id":1, "type": "CNAME", "name": "example"}}`)
})
record, _, err := client.Domains.EditRecord(ctx, "example.com", 1, editRequest)
if err != nil {
t.Errorf("Domains.EditRecord returned error: %v", err)
}
expected := &DomainRecord{ID: 1, Type: "CNAME", Name: "example"}
if !reflect.DeepEqual(record, expected) {
t.Errorf("Domains.EditRecord returned %+v, expected %+v", record, expected)
}
}
func TestDomainRecord_String(t *testing.T) {
record := &DomainRecord{
ID: 1,
Type: "CNAME",
Name: "example",
Data: "@",
Priority: 10,
Port: 10,
TTL: 1800,
Weight: 10,
Flags: 1,
Tag: "test",
}
stringified := record.String()
expected := `godo.DomainRecord{ID:1, Type:"CNAME", Name:"example", Data:"@", Priority:10, Port:10, TTL:1800, Weight:10, Flags:1, Tag:"test"}`
if expected != stringified {
t.Errorf("DomainRecord.String returned %+v, expected %+v", stringified, expected)
}
}
func TestDomainRecordEditRequest_String(t *testing.T) {
record := &DomainRecordEditRequest{
Type: "CNAME",
Name: "example",
Data: "@",
Priority: 10,
Port: 10,
TTL: 1800,
Weight: 10,
Flags: 1,
Tag: "test",
}
stringified := record.String()
expected := `godo.DomainRecordEditRequest{Type:"CNAME", Name:"example", Data:"@", Priority:10, Port:10, TTL:1800, Weight:10, Flags:1, Tag:"test"}`
if expected != stringified {
t.Errorf("DomainRecordEditRequest.String returned %+v, expected %+v", stringified, expected)
}
}