-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathmock_test.go
More file actions
340 lines (312 loc) · 7.75 KB
/
mock_test.go
File metadata and controls
340 lines (312 loc) · 7.75 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package gotwi
import (
"context"
"errors"
"io"
"net/http"
"strings"
"testing"
"github.com/michimani/gotwi/internal/util"
"github.com/michimani/gotwi/resources"
"github.com/stretchr/testify/assert"
)
func Test_RoundTripFunc(t *testing.T) {
cases := []struct {
name string
roundTripFunc RoundTripFunc
expectedStatus int
}{
{
name: "success: returns status code 200",
roundTripFunc: func(req *http.Request) *http.Response {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader("")),
}
},
expectedStatus: http.StatusOK,
},
}
for _, c := range cases {
t.Run(c.name, func(tt *testing.T) {
asst := assert.New(tt)
client := newMockClient(c.roundTripFunc)
req, _ := http.NewRequest("GET", "http://example.com", nil)
resp, err := client.Do(req)
asst.NoError(err)
asst.Equal(c.expectedStatus, resp.StatusCode)
})
}
}
func Test_NewMockHTTPClient(t *testing.T) {
cases := []struct {
name string
input *MockInput
expectedStatus int
}{
{
name: "success: returns status code 200",
input: &MockInput{
ResponseStatusCode: http.StatusOK,
ResponseBody: io.NopCloser(strings.NewReader("")),
},
expectedStatus: http.StatusOK,
},
{
name: "error: input is nil",
input: nil,
expectedStatus: 0,
},
}
for _, c := range cases {
t.Run(c.name, func(tt *testing.T) {
asst := assert.New(tt)
client := NewMockHTTPClient(c.input)
if c.input == nil {
asst.Nil(client)
return
}
req, _ := http.NewRequest("GET", "http://example.com", nil)
resp, err := client.Do(req)
asst.NoError(err)
asst.Equal(c.expectedStatus, resp.StatusCode)
})
}
}
func Test_MockGotwiClient_Exec(t *testing.T) {
cases := []struct {
name string
returnedToken string
execHasError bool
hasNot200Error bool
expectedError bool
}{
{
name: "success: returns token",
returnedToken: "test-token",
execHasError: false,
hasNot200Error: false,
expectedError: false,
},
{
name: "error: returns error",
returnedToken: "",
execHasError: true,
hasNot200Error: false,
expectedError: true,
},
{
name: "error: returns non-200 error",
returnedToken: "",
execHasError: false,
hasNot200Error: true,
expectedError: false,
},
}
for _, c := range cases {
t.Run(c.name, func(tt *testing.T) {
asst := assert.New(tt)
client := NewMockGotwiClient(c.returnedToken, c.execHasError, c.hasNot200Error)
req, _ := http.NewRequest("GET", "http://example.com", nil)
var response util.Response = &MockAPIResponse{}
non2xxError, err := client.Exec(req, response)
if c.expectedError {
asst.Error(err)
} else {
asst.NoError(err)
}
if c.hasNot200Error {
asst.NotNil(non2xxError)
} else {
asst.Nil(non2xxError)
}
})
}
}
func Test_MockGotwiClient_IsReady(t *testing.T) {
cases := []struct {
name string
client *MockGotwiClient
expect bool
}{
{
name: "success: returns true with AuthenMethodOAuth1UserContext",
client: NewMockGotwiClientWithFunc(MockFuncInput{
MockAuthenticationMethod: func() AuthenticationMethod {
return AuthenMethodOAuth1UserContext
},
MockAccessToken: func() string {
return "test-token"
},
MockSigningKey: func() string {
return "test-signing-key"
},
}),
expect: true,
},
{
name: "error: returns false with AuthenMethodOAuth1UserContext",
client: NewMockGotwiClientWithFunc(MockFuncInput{
MockAuthenticationMethod: func() AuthenticationMethod {
return AuthenMethodOAuth1UserContext
},
MockSigningKey: func() string {
return "test-signing-key"
},
}),
expect: false,
},
{
name: "success: returns true with AuthenMethodOAuth2BearerToken",
client: NewMockGotwiClientWithFunc(MockFuncInput{
MockAuthenticationMethod: func() AuthenticationMethod {
return AuthenMethodOAuth2BearerToken
},
MockAccessToken: func() string {
return "test-token"
},
}),
expect: true,
},
{
name: "error: returns false with AuthenMethodOAuth2BearerToken",
client: NewMockGotwiClientWithFunc(MockFuncInput{
MockAuthenticationMethod: func() AuthenticationMethod {
return AuthenMethodOAuth2BearerToken
},
}),
expect: false,
},
{
name: "error: return false with invalid AuthenticationMethod",
client: NewMockGotwiClientWithFunc(MockFuncInput{
MockAuthenticationMethod: func() AuthenticationMethod {
return AuthenticationMethod("invalid method")
},
}),
expect: false,
},
{
name: "error: returns false with nil client",
client: nil,
expect: false,
},
}
for _, c := range cases {
t.Run(c.name, func(tt *testing.T) {
asst := assert.New(tt)
asst.Equal(c.expect, c.client.IsReady())
})
}
}
func Test_MockGotwiClientWithFunc(t *testing.T) {
cases := []struct {
name string
input MockFuncInput
}{
{
name: "success: uses default functions",
input: MockFuncInput{},
},
{
name: "success: uses custom functions",
input: MockFuncInput{
MockExec: func(req *http.Request, i util.Response) (*resources.Non2XXError, error) {
return nil, nil
},
MockIsReady: func() bool { return true },
MockAccessToken: func() string { return "custom-token" },
MockAuthenticationMethod: func() AuthenticationMethod {
return AuthenMethodOAuth2BearerToken
},
MockOAuthToken: func() string {
return "custom-oauth-token"
},
MockOAuthConsumerKey: func() string {
return "custom-oauth-consumer-key"
},
MockSigningKey: func() string {
return "custom-signing-key"
},
MockCallAPI: func(ctx context.Context, endpoint, method string, p util.Parameters, i util.Response) error {
return nil
},
},
},
}
for _, c := range cases {
t.Run(c.name, func(tt *testing.T) {
asst := assert.New(tt)
client := NewMockGotwiClientWithFunc(c.input)
// can call all methods
asst.NotPanics(func() {
client.Exec(nil, nil)
client.IsReady()
client.AccessToken()
client.AuthenticationMethod()
client.OAuthToken()
client.OAuthConsumerKey()
client.SigningKey()
client.CallAPI(context.Background(), "endpoint", "method", nil, nil)
})
})
}
}
func Test_MockAPIParameter(t *testing.T) {
param := MockAPIParameter{}
t.Run("test MockAPIParameter methods", func(tt *testing.T) {
asst := assert.New(tt)
param.SetAccessToken("test-token")
asst.Equal("", param.AccessToken())
asst.Equal("", param.ResolveEndpoint("base"))
body, err := param.Body()
asst.Nil(body)
asst.NoError(err)
params := param.ParameterMap()
asst.Empty(params)
})
}
func Test_MockAPIResponse(t *testing.T) {
response := MockAPIResponse{}
t.Run("test MockAPIResponse methods", func(tt *testing.T) {
asst := assert.New(tt)
asst.False(response.HasPartialError())
})
}
func Test_MockGotwiClient_CallAPI(t *testing.T) {
cases := []struct {
name string
client *MockGotwiClient
expectError bool
}{
{
name: "success: returns true",
client: &MockGotwiClient{
MockCallAPI: func(ctx context.Context, endpoint, method string, p util.Parameters, i util.Response) error {
return nil
},
},
expectError: false,
},
{
name: "error: returns error",
client: &MockGotwiClient{
MockCallAPI: func(ctx context.Context, endpoint, method string, p util.Parameters, i util.Response) error {
return errors.New("error")
},
},
expectError: true,
},
}
for _, c := range cases {
t.Run(c.name, func(tt *testing.T) {
asst := assert.New(tt)
res := c.client.CallAPI(context.Background(), "endpoint", "method", nil, nil)
if c.expectError {
asst.Error(res)
} else {
asst.NoError(res)
}
})
}
}