-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
200 lines (168 loc) · 4.86 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
package goaci
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
const (
testHost = "10.0.0.1"
testURL = "https://" + testHost
)
func testClient() Client {
client, _ := NewClient(testHost, "usr", "pwd")
client.LastRefresh = time.Now()
gock.InterceptClient(client.HttpClient)
return client
}
// ErrReader implements the io.Reader interface and fails on Read.
type ErrReader struct{}
// Read mocks failing io.Reader test cases.
func (r ErrReader) Read(buf []byte) (int, error) {
return 0, errors.New("fail")
}
// TestNewClient tests the NewClient function.
func TestNewClient(t *testing.T) {
client, _ := NewClient(testURL, "usr", "pwd", RequestTimeout(120))
assert.Equal(t, client.HttpClient.Timeout, 120*time.Second)
}
// TestClientLogin tests the Client::Login method.
func TestClientLogin(t *testing.T) {
defer gock.Off()
client := testClient()
// Successful login
gock.New(testURL).Post("/api/aaaLogin.json").Reply(200)
assert.NoError(t, client.Login())
// Invalid HTTP status code
gock.New(testURL).Post("/api/aaaLogin.json").Reply(405)
assert.Error(t, client.Login())
// JSON error from Client
gock.New(testURL).
Post("/api/aaaLogin.json").
Reply(200).
BodyString(Body{}.Set("imdata.0.error.attributes.text", "error").Str)
assert.Error(t, client.Login())
}
// TestClientRefresh tests the Client::Refresh method.
func TestClientRefresh(t *testing.T) {
defer gock.Off()
client := testClient()
gock.New(testURL).Get("/api/aaaRefresh.json").Reply(200)
assert.NoError(t, client.Refresh())
}
// TestClientGet tests the Client::Get method.
func TestClientGet(t *testing.T) {
defer gock.Off()
client := testClient()
var err error
// Success
gock.New(testURL).Get("/url.json").Reply(200)
_, err = client.Get("/url")
assert.NoError(t, err)
// HTTP error
gock.New(testURL).Get("/url.json").ReplyError(errors.New("fail"))
_, err = client.Get("/url")
assert.Error(t, err)
// Invalid HTTP status code
gock.New(testURL).Get("/url.json").Reply(405)
_, err = client.Get("/url")
assert.Error(t, err)
// Error decoding response body
gock.New(testURL).
Get("/url.json").
Reply(200).
Map(func(res *http.Response) *http.Response {
res.Body = ioutil.NopCloser(ErrReader{})
return res
})
_, err = client.Get("/url")
assert.Error(t, err)
// Force token refresh and throw an error
client.LastRefresh = time.Now().AddDate(0, 0, -1)
gock.New(testURL).
Get("/api/aaaRefresh.json").
ReplyError(errors.New("fail"))
_, err = client.Get("/url")
assert.Error(t, err)
}
// TestClientGetClass tests the Client::GetClass method.
func TestClientGetClass(t *testing.T) {
defer gock.Off()
client := testClient()
// Success
gock.New(testURL).
Get("/api/class/fvTenant.json").
Reply(200).
BodyString(Body{}.
Set("imdata.0.fvTenant.attributes.name", "zero").
Set("imdata.1.fvTenant.attributes.name", "one").
Str)
res, _ := client.GetClass("fvTenant")
if !assert.Len(t, res.Array(), 2) {
fmt.Println(res.Get("@pretty"))
}
if !assert.Equal(t, "one", res.Get("1.fvTenant.attributes.name").Str) {
fmt.Println(res.Get("@pretty"))
}
// HTTP error
gock.New(testURL).Get("/api/class/test.json").ReplyError(errors.New("fail"))
_, err := client.GetClass("test")
assert.Error(t, err)
}
// TestClientGetDn tests the Client::GetDn method.
func TestClientGetDn(t *testing.T) {
defer gock.Off()
client := testClient()
// Success
gock.New(testURL).
Get("/api/mo/uni/tn-test.json").
Reply(200).
BodyString(Body{}.Set("imdata.0.fvTenant.attributes.name", "test").Str)
res, _ := client.GetDn("uni/tn-test")
if !assert.Equal(t, "test", res.Get("fvTenant.attributes.name").Str) {
fmt.Println(res.Get("@pretty"))
}
// HTTP error
gock.New(testURL).
Get("/api/mo/uni/fail.json").
ReplyError(errors.New("fail"))
_, err := client.GetDn("uni/fail")
assert.Error(t, err)
}
// TestClientPost tests the Client::Post method.
func TestClientPost(t *testing.T) {
defer gock.Off()
client := testClient()
var err error
// Success
gock.New(testURL).Post("/url.json").Reply(200)
_, err = client.Post("/url", "{}")
assert.NoError(t, err)
// HTTP error
gock.New(testURL).Post("/url.json").ReplyError(errors.New("fail"))
_, err = client.Post("/url", "{}")
assert.Error(t, err)
// Invalid HTTP status code
gock.New(testURL).Post("/url.json").Reply(405)
_, err = client.Post("/url", "{}")
assert.Error(t, err)
// Error decoding response body
gock.New(testURL).
Post("/url.json").
Reply(200).
Map(func(res *http.Response) *http.Response {
res.Body = ioutil.NopCloser(ErrReader{})
return res
})
_, err = client.Post("/url", "{}")
assert.Error(t, err)
// Force token refresh and throw an error
client.LastRefresh = time.Now().AddDate(0, 0, -1)
gock.New(testURL).Get("/api/aaaRefresh.json").ReplyError(errors.New("fail"))
_, err = client.Post("/url", "{}")
assert.Error(t, err)
}