-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_test.go
210 lines (172 loc) · 5.27 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
205
206
207
208
209
210
package cc
import (
"errors"
"io"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)
const (
testURL = "https://10.0.0.1"
)
func testClient() Client {
client, _ := NewClient(testURL, "usr", "pwd", MaxRetries(0))
gock.InterceptClient(client.HttpClient)
return client
}
func authenticatedTestClient() Client {
client := testClient()
client.Token = "ABC"
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("/dna/system/api/v1/auth/token").Reply(200).BodyString(`{"Token": "ABC"}`)
assert.NoError(t, client.Login())
// Unsuccessful token retrieval
gock.New(testURL).Post("/dna/system/api/v1/auth/token").Reply(200).BodyString(``)
assert.Error(t, client.Login())
// Invalid HTTP status code
gock.New(testURL).Post("/dna/system/api/v1/auth/token").Reply(405)
assert.Error(t, client.Login())
}
// TestClientGet tests the Client.Get method.
func TestClientGet(t *testing.T) {
defer gock.Off()
client := authenticatedTestClient()
var err error
// Success
gock.New(testURL).Get("/url").Reply(200).BodyString(`{"response":"a string"}`)
res, err := client.Get("/url")
assert.NoError(t, err)
assert.Equal(t, "a string", res.Get("response").String())
// HTTP error
gock.New(testURL).Get("/url").ReplyError(errors.New("fail"))
_, err = client.Get("/url")
assert.Error(t, err)
// Invalid HTTP status code
gock.New(testURL).Get("/url").Reply(405)
_, err = client.Get("/url")
assert.Error(t, err)
// Error decoding response body
gock.New(testURL).
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)
}
// TestClientDelete tests the Client.Delete method.
func TestClientDelete(t *testing.T) {
defer gock.Off()
client := authenticatedTestClient()
// Success
gock.New(testURL).
Delete("/url").
Reply(200)
_, err := client.Delete("/url")
assert.NoError(t, err)
// HTTP error
gock.New(testURL).
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 := authenticatedTestClient()
var err error
// Success
gock.New(testURL).Post("/url").Reply(200)
_, err = client.Post("/url", "{}")
assert.NoError(t, err)
// HTTP error
gock.New(testURL).Post("/url").ReplyError(errors.New("fail"))
_, err = client.Post("/url", "{}")
assert.Error(t, err)
// Invalid HTTP status code
gock.New(testURL).Post("/url").Reply(405)
_, err = client.Post("/url", "{}")
assert.Error(t, err)
// Error decoding response body
gock.New(testURL).
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)
}
// TestClientPut tests the Client.Put method.
func TestClientPut(t *testing.T) {
defer gock.Off()
client := authenticatedTestClient()
var err error
// Success
gock.New(testURL).Put("/url").Reply(200)
_, err = client.Put("/url", "{}")
assert.NoError(t, err)
// HTTP error
gock.New(testURL).Put("/url").ReplyError(errors.New("fail"))
_, err = client.Put("/url", "{}")
assert.Error(t, err)
// Invalid HTTP status code
gock.New(testURL).Put("/url").Reply(405)
_, err = client.Put("/url", "{}")
assert.Error(t, err)
// Error decoding response body
gock.New(testURL).
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)
}
// TestClientWaitTask tests the Client.WaitTask method.
func TestClientWaitTask(t *testing.T) {
defer gock.Off()
client := authenticatedTestClient()
var err error
// Task
gock.New(testURL).Get("/api/v1/task/123").Reply(200).BodyString(`{"response": {"endTime": "1", "isError": false}}`)
_, err = client.WaitTask(&Req{}, &Res{Raw: `{"response": {"taskId": "123"}}`})
assert.NoError(t, err)
// Task error
gock.New(testURL).Get("/api/v1/task/123").Reply(200).BodyString(`{"response": {"endTime": "1", "isError": true}}`)
_, err = client.WaitTask(&Req{}, &Res{Raw: `{"response": {"taskId": "123"}}`})
assert.Error(t, err)
// Execution
gock.New(testURL).Get("/dna/platform/management/business-api/v1/execution-status/123").Reply(200).BodyString(`{"status": "SUCCESS"}`)
_, err = client.WaitTask(&Req{}, &Res{Raw: `{"executionId": "123"}`})
assert.NoError(t, err)
// Execution error
gock.New(testURL).Get("/dna/platform/management/business-api/v1/execution-status/123").Reply(200).BodyString(`{"status": "FAILURE"}`)
_, err = client.WaitTask(&Req{}, &Res{Raw: `{"executionId": "123"}`})
assert.Error(t, err)
}