-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrestclient_test.go
270 lines (247 loc) · 6.02 KB
/
restclient_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
package restclient
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"reflect"
"testing"
)
var (
server *httptest.Server
client *Client
th *testHandler
u *url.URL
)
type testHandler struct {
url *url.URL
reqBody []byte
header http.Header
reqmethod string
response []byte
t *testing.T
}
func (th *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var err error
defer r.Body.Close()
th.header = make(http.Header)
for key, headers := range r.Header {
th.header[key] = make([]string, len(headers))
copy(th.header[key], headers)
}
th.url = r.URL
th.reqmethod = r.Method
th.reqBody, err = ioutil.ReadAll(r.Body)
th.t.Log(r.URL)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(200)
w.Write(th.response)
}
func TestMain(m *testing.M) {
th = &testHandler{}
server = httptest.NewServer(th)
var err error
u, err = url.Parse(server.URL)
if err != nil {
panic(fmt.Sprintf("Failed on url.Parse call: %s, %s\n", server.URL, err))
}
client, err = NewClient(&ClientConfig{}, nil)
if err != nil {
panic(fmt.Sprintf("Failed on NewClient call: %s\n", err))
}
rc := m.Run()
os.Exit(rc)
}
func TestGet(t *testing.T) {
th.t = t
var err error
tr := &testResponse{
Foo: "foo",
Bar: "bar",
Baz: 59,
}
th.response, err = json.Marshal(tr)
if err != nil {
t.Fatal("Error marshaling: ", err)
}
reqb := &testValidatorRequest{
UID: "testuid",
KeyType: "s3",
}
tr2 := &testResponse{}
err = client.Get(context.Background(), u, "/laterpath", reqb, tr2)
if err != nil {
t.Log("Failed client.Get: ", err)
t.Fail()
}
if !reflect.DeepEqual(tr, tr2) {
t.Log("data not preserved on round trip")
t.Fail()
}
}
func TestDelete(t *testing.T) {
th.t = t
var err error
tr := &testResponse{
Foo: "foo",
Bar: "bar",
Baz: 59,
}
th.response, err = json.Marshal(tr)
if err != nil {
t.Fatal("Error marshaling: ", err)
}
tr2 := &testResponse{}
err = client.Delete(context.Background(), u, "/laterpath", nil, tr2)
if err != nil {
t.Log("Failed client.Delete: ", err)
t.Fail()
}
if !reflect.DeepEqual(tr, tr2) {
t.Log("data not preserved on round trip")
t.Fail()
}
}
func TestPost(t *testing.T) {
th.t = t
var err error
tr := &testResponse{
Foo: "foo",
Bar: "bar",
Baz: 59,
}
th.response, err = json.Marshal(tr)
if err != nil {
t.Fatal("Error marshaling: ", err)
}
tr2 := &testResponse{}
reqb := &testValidatorRequest{
UID: "testuid",
KeyType: "s3",
}
err = client.Post(context.Background(), u, "/whatever", nil, reqb, tr2)
if err != nil {
t.Log("Failed client.Post: ", err)
t.Fail()
}
if !reflect.DeepEqual(tr, tr2) {
t.Log("data not preserved on round trip")
t.Fail()
}
// Not try with something we know will fail.
reqb.UID = ""
reqb.KeyType = "nots3"
err = client.Post(context.Background(), u, "/whatever", nil, reqb, tr2)
if err == nil {
t.Log("error expected on failed validation, no error returned")
} else {
t.Log("expected validation error: ", err)
}
}
func TestPut(t *testing.T) {
th.t = t
var err error
tr := &testResponse{
Foo: "foo",
Bar: "bar",
Baz: 59,
}
th.response, err = json.Marshal(tr)
if err != nil {
t.Fatal("Error marshaling: ", err)
}
tr2 := &testResponse{}
reqb := &testValidatorRequest{
UID: "testuid",
KeyType: "s3",
}
err = client.Put(context.Background(), u, "/whatever", nil, reqb, tr2)
if err != nil {
t.Log("Failed client.Put: ", err)
t.Fail()
}
if !reflect.DeepEqual(tr, tr2) {
t.Log("data not preserved on round trip")
t.Fail()
}
}
func TestPostSliceValidation(t *testing.T) {
th.t = t
var err error
ttl := 100
dreq := DNSRecords{
DNSRecord{
Type: "A",
Name: "catpics.org",
Data: "1",
Priority: nil,
TTL: &ttl,
Service: nil,
Protocol: nil,
Port: nil,
Weight: nil,
}, DNSRecord{
Type: "A",
Name: "catpics.org",
Data: "1",
Priority: nil,
TTL: &ttl,
Service: nil,
Protocol: nil,
Port: nil,
Weight: nil,
},
}
err = client.Post(context.Background(), u, "/whatever", nil, dreq, nil)
if err != nil {
t.Log("error returned: " + err.Error())
t.Fail()
}
// now make it fail
dreq[1].Type = "ASDF"
err = client.Post(context.Background(), u, "/whatever", nil, dreq, nil)
if err == nil {
t.Log("error was nil, should have failed validation")
t.Fail()
}
if err.Error() != `Validation error: Field 'DNSRecord.Type' invalid value: 'ASDF', valid values are: "A","AAAA","CNAME","MX","NS","SOA","SRV","TXT"` {
t.Log("unexpected error message, got ", err)
}
}
type testValidatorRequest struct {
UID string `url:"uid" validate:"required"`
SubUser string `url:"subuser,omitempty"`
AccessKey string `url:"access-key,omitempty"`
SecretKey string `url:"secret-key,omitempty"`
KeyType string `url:"key-type,omitempty" validate:"omitempty,eq=s3|eq=swift"`
GenerateKey *bool `url:"generate-key,omitempty"` // defaults to true
}
type testResponse struct {
Foo string
Bar string
Baz int
}
// DNSRecord represents an element of the GoDaddy model
// https://developer.godaddy.com/doc#!/_v1_domains/recordReplace/ArrayOfDNSRecord
type DNSRecord struct {
Type string `json:"type" validate:"required,eq=A|eq=AAAA|eq=CNAME|eq=MX|eq=NS|eq=SOA|eq=SRV|eq=TXT"`
Name string `json:"name" validate:"required,min=1,max=255"`
Data string `json:"data" validate:"required,min=1,max=255"`
Priority *int `json:"priority,omitempty" validate:"omitempty,gte=1"`
TTL *int `json:"ttl,omitempty" validate:"omitempty,gte=1"`
Service *string `json:"service,omitempty" validate:"omitempty,min=1"`
Protocol *string `json:"protocol,omitempty" validate:"omitempty,min=1"`
Port *int `json:"port,omitempty" validate:"omitempty,min=1,max=65535"`
Weight *int `json:"weight,omitempty" validate:"omitempty,gte=1"`
}
// DNSRecords represents the GoDaddy model
// https://developer.godaddy.com/doc#!/_v1_domains/recordReplace/ArrayOfDNSRecord
type DNSRecords []DNSRecord