-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
202 lines (170 loc) · 5.08 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
package meraki
import (
"errors"
"io"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
func testClient() Client {
client, _ := NewClient("abc123", MaxRetries(0))
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("abc123", RequestTimeout(120))
assert.Equal(t, client.ApiToken, "abc123")
assert.Equal(t, client.HttpClient.Timeout, 120*time.Second)
}
// TestClientGet tests the Client::Get method.
func TestClientGet(t *testing.T) {
defer gock.Off()
client := testClient()
var err error
// Success
gock.New(client.BaseUrl).Get("/url").Reply(200)
_, err = client.Get("/url")
assert.NoError(t, err)
// HTTP error
gock.New(client.BaseUrl).Get("/url").ReplyError(errors.New("fail"))
_, err = client.Get("/url")
assert.Error(t, err)
// Invalid HTTP status code
gock.New(client.BaseUrl).Get("/url").Reply(405)
_, err = client.Get("/url")
assert.Error(t, err)
// Error decoding response body
gock.New(client.BaseUrl).
Get("/url").
Reply(200).
Map(func(res *http.Response) *http.Response {
res.Body = io.NopCloser(ErrReader{})
return res
})
_, err = client.Get("/url")
assert.Error(t, err)
}
// TestClientGetPages is like TestClientGet, but with basic pagination.
func TestClientGetPages(t *testing.T) {
defer gock.Off()
client := testClient()
var err error
gock.New(client.BaseUrl).Get("/url").
Reply(200).
BodyString(`["1","2","3"]`).
Header.Set("Link", `<`+client.BaseUrl+`/url?offset=4>; rel="next"`)
gock.New(client.BaseUrl).Get("/url").MatchParam("offset", "4").
Reply(200).
BodyString(`["4","5","6"]`).
Header.Set("Link", `<`+client.BaseUrl+`/url?offset=7>; rel="next"`)
gock.New(client.BaseUrl).Get("/url").MatchParam("offset", "7").
Reply(200).
BodyString(`["7","8"]`).
Header.Set("Link", `<`+client.BaseUrl+`/url?offset=1>; rel="first"`)
res, err := client.Get("/url")
assert.NoError(t, err)
assert.Equal(t, `["1","2","3","4","5","6","7","8"]`, res.Raw)
}
// TestClientGetPages is like TestClientGet, but with basic pagination and "items" response.
func TestClientGetPagesItems(t *testing.T) {
defer gock.Off()
client := testClient()
var err error
gock.New(client.BaseUrl).Get("/url").
Reply(200).
BodyString(`{"items": ["1","2","3"]}`).
Header.Set("Link", `<`+client.BaseUrl+`/url?offset=4>; rel="next"`)
gock.New(client.BaseUrl).Get("/url").MatchParam("offset", "4").
Reply(200).
BodyString(`{"items": ["4","5","6"]}`).
Header.Set("Link", `<`+client.BaseUrl+`/url?offset=7>; rel="next"`)
gock.New(client.BaseUrl).Get("/url").MatchParam("offset", "7").
Reply(200).
BodyString(`{"items": ["7","8"]}`).
Header.Set("Link", `<`+client.BaseUrl+`/url?offset=1>; rel="first"`)
res, err := client.Get("/url")
assert.NoError(t, err)
assert.Equal(t, `{"items":["1","2","3","4","5","6","7","8"]}`, res.Raw)
}
// TestClientDelete tests the Client::Delete method.
func TestClientDelete(t *testing.T) {
defer gock.Off()
client := testClient()
// Success
gock.New(client.BaseUrl).
Delete("/url").
Reply(200)
_, err := client.Delete("/url")
assert.NoError(t, err)
// HTTP error
gock.New(client.BaseUrl).
Delete("/url").
ReplyError(errors.New("fail"))
_, err = client.Delete("/url")
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(client.BaseUrl).Post("/url").Reply(200)
_, err = client.Post("/url", "{}")
assert.NoError(t, err)
// HTTP error
gock.New(client.BaseUrl).Post("/url").ReplyError(errors.New("fail"))
_, err = client.Post("/url", "{}")
assert.Error(t, err)
// Invalid HTTP status code
gock.New(client.BaseUrl).Post("/url").Reply(405)
_, err = client.Post("/url", "{}")
assert.Error(t, err)
// Error decoding response body
gock.New(client.BaseUrl).
Post("/url").
Reply(200).
Map(func(res *http.Response) *http.Response {
res.Body = io.NopCloser(ErrReader{})
return res
})
_, err = client.Post("/url", "{}")
assert.Error(t, err)
}
// TestClientPost tests the Client::Post method.
func TestClientPut(t *testing.T) {
defer gock.Off()
client := testClient()
var err error
// Success
gock.New(client.BaseUrl).Put("/url").Reply(200)
_, err = client.Put("/url", "{}")
assert.NoError(t, err)
// HTTP error
gock.New(client.BaseUrl).Put("/url").ReplyError(errors.New("fail"))
_, err = client.Put("/url", "{}")
assert.Error(t, err)
// Invalid HTTP status code
gock.New(client.BaseUrl).Put("/url").Reply(405)
_, err = client.Put("/url", "{}")
assert.Error(t, err)
// Error decoding response body
gock.New(client.BaseUrl).
Put("/url").
Reply(200).
Map(func(res *http.Response) *http.Response {
res.Body = io.NopCloser(ErrReader{})
return res
})
_, err = client.Put("/url", "{}")
assert.Error(t, err)
}