-
-
Notifications
You must be signed in to change notification settings - Fork 978
/
client_test.go
204 lines (159 loc) · 4.97 KB
/
client_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
package selectel
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setupTest(t *testing.T) (*Client, *http.ServeMux) {
t.Helper()
mux := http.NewServeMux()
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
client := NewClient("token")
client.BaseURL, _ = url.Parse(server.URL)
client.HTTPClient = server.Client()
return client, mux
}
func TestClient_ListRecords(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/123/records/", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
return
}
fixture := "./fixtures/list_records.json"
err := writeResponse(rw, fixture)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
records, err := client.ListRecords(context.Background(), 123)
require.NoError(t, err)
expected := []Record{
{ID: 123, Name: "example.com", Type: "TXT", TTL: 60, Email: "email@example.com", Content: "txttxttxtA"},
{ID: 1234, Name: "example.org", Type: "TXT", TTL: 60, Email: "email@example.org", Content: "txttxttxtB"},
{ID: 12345, Name: "example.net", Type: "TXT", TTL: 60, Email: "email@example.net", Content: "txttxttxtC"},
}
assert.Equal(t, expected, records)
}
func TestClient_ListRecords_error(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/123/records/", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
return
}
rw.WriteHeader(http.StatusUnauthorized)
err := writeResponse(rw, "./fixtures/error.json")
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
records, err := client.ListRecords(context.Background(), 123)
require.EqualError(t, err, "request failed with status code 401: API error: 400 - error description - field that the error occurred in")
assert.Nil(t, records)
}
func TestClient_GetDomainByName(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/sub.sub.example.org", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
return
}
rw.WriteHeader(http.StatusNotFound)
})
mux.HandleFunc("/sub.example.org", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
return
}
rw.WriteHeader(http.StatusNotFound)
})
mux.HandleFunc("/example.org", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
return
}
fixture := "./fixtures/domains.json"
err := writeResponse(rw, fixture)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
domain, err := client.GetDomainByName(context.Background(), "sub.sub.example.org")
require.NoError(t, err)
expected := &Domain{
ID: 123,
Name: "example.org",
}
assert.Equal(t, expected, domain)
}
func TestClient_AddRecord(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/123/records/", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
return
}
rec := Record{}
err := json.NewDecoder(req.Body).Decode(&rec)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
rec.ID = 456
err = json.NewEncoder(rw).Encode(rec)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
})
record, err := client.AddRecord(context.Background(), 123, Record{
Name: "example.org",
Type: "TXT",
TTL: 60,
Email: "email@example.org",
Content: "txttxttxttxt",
})
require.NoError(t, err)
expected := &Record{
ID: 456,
Name: "example.org",
Type: "TXT",
TTL: 60,
Email: "email@example.org",
Content: "txttxttxttxt",
}
assert.Equal(t, expected, record)
}
func TestClient_DeleteRecord(t *testing.T) {
client, mux := setupTest(t)
mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodDelete {
http.Error(rw, fmt.Sprintf("unsupported method: %s", req.Method), http.StatusMethodNotAllowed)
return
}
})
err := client.DeleteRecord(context.Background(), 123, 456)
require.NoError(t, err)
}
func writeResponse(rw io.Writer, filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer func() { _ = file.Close() }()
_, err = io.Copy(rw, file)
return err
}