-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
400 lines (362 loc) · 8.18 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package tinysrc
import (
"context"
"fmt"
"github.com/dmitrypro77/tinysrc-api-sdk/models"
"io"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"time"
)
func TestClient_do(t *testing.T) {
type fields struct {
Client *Client
}
type args struct {
req *http.Request
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, "Test Passed")
}))
tsNotFound := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
_, _ = fmt.Fprint(w, "test 404")
}))
tsTimeout := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(10 * time.Millisecond)
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
defer tsNotFound.Close()
defer tsTimeout.Close()
req, _ := http.NewRequest(http.MethodGet, ts.URL+"/", nil)
reqNotFound, _ := http.NewRequest(http.MethodGet, tsNotFound.URL+"/", nil)
reqTimeout, _ := http.NewRequest(http.MethodGet, tsTimeout.URL+"/", nil)
testClient, _ := NewClient(context.Background(), "test", nil)
cont, cancel := context.WithTimeout(context.Background(), time.Duration(1)*time.Microsecond)
testClientTimeout, _ := NewClient(cont, "test", nil)
cancel()
tests := []struct {
name string
fields fields
args args
want *http.Response
wantErr bool
}{
{
name: "test_success",
fields: fields{
Client: testClient,
},
args: args{req: req},
want: &http.Response{
StatusCode: 200,
},
wantErr: false,
},
{
name: "not_found_error",
fields: fields{
Client: testClient,
},
args: args{req: reqNotFound},
want: &http.Response{
StatusCode: 404,
},
wantErr: false,
},
{
name: "timeout_error",
fields: fields{
Client: testClientTimeout,
},
args: args{req: reqTimeout},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.fields.Client.do(tt.args.req)
if (err != nil) != tt.wantErr {
t.Errorf("do() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if !reflect.DeepEqual(got.StatusCode, tt.want.StatusCode) {
t.Errorf("do() got = %v, want %v", got, tt.want)
}
} else {
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("do() got = %v, want %v", got, tt.want)
}
}
})
}
}
func TestClient_isSuccess(t *testing.T) {
type fields struct {
Client *Client
}
type args struct {
statusCode int
}
testClient, _ := NewClient(context.Background(), "test", nil)
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "test_success",
fields: fields{
Client: testClient,
},
args: args{statusCode: 200},
want: true,
},
{
name: "test_fail",
fields: fields{
Client: testClient,
},
args: args{statusCode: 200},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.fields.Client.isSuccess(tt.args.statusCode); got != tt.want {
t.Errorf("isSuccess() = %v, want %v", got, tt.want)
}
})
}
}
func TestClient_isUnauthorized(t *testing.T) {
type fields struct {
Client *Client
}
type args struct {
statusCode int
}
testClient, _ := NewClient(context.Background(), "test", nil)
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "test_success",
fields: fields{
Client: testClient,
},
args: args{statusCode: 401},
want: true,
},
{
name: "test_fail",
fields: fields{
Client: testClient,
},
args: args{statusCode: 200},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.fields.Client.isUnauthorized(tt.args.statusCode); got != tt.want {
t.Errorf("isUnauthorized() = %v, want %v", got, tt.want)
}
})
}
}
func TestClient_parseErrorResponse(t *testing.T) {
type fields struct {
Client *Client
}
type args struct {
resp *http.Response
}
testClient, _ := NewClient(context.Background(), "test", nil)
handler := func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, "{\n\"validations\": {\n\"url\": [\n\" URL is not valid\"\n ]\n},\n\"errors\": [\n\"Validation Error Happened\"\n]\n}")
}
handlerNotError := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(401)
_, _ = io.WriteString(w, "{)")
}
req := httptest.NewRequest("GET", "http://example.com:8080", nil)
w := httptest.NewRecorder()
handler(w, req)
handlerNotError(w, req)
resp := w.Result()
tests := []struct {
name string
fields fields
args args
want *models.ErrorResponse
}{
{
name: "test_validation_error",
fields: fields{
Client: testClient,
},
args: args{resp: resp},
want: &models.ErrorResponse{
Validations: map[string][]string{
"url": {" URL is not valid"},
},
Errors: []string{"Validation Error Happened"},
Status: 200,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.fields.Client.parseErrorResponse(tt.args.resp); !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseErrorResponse() = %v, want %v", got, tt.want)
}
})
}
}
func TestClient_sendRequest(t *testing.T) {
type fields struct {
Client *Client
}
type args struct {
method string
pathURL string
body io.Reader
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, "Test Passed")
}))
defer ts.Close()
testClient, _ := NewClient(context.Background(), "test", nil)
testClient.baseURL = &url.URL{
RawPath: ts.URL,
}
tests := []struct {
name string
fields fields
args args
want int
wantErr bool
}{
{
name: "test_success",
fields: fields{Client: testClient},
args: args{
method: http.MethodGet,
pathURL: ts.URL,
body: nil,
},
want: 200,
wantErr: false,
},
{
name: "test_fail",
fields: fields{Client: testClient},
args: args{
method: http.MethodGet,
pathURL: "*7/\\asdsad",
body: nil,
},
want: 500,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.fields.Client.sendRequest(tt.args.method, tt.args.pathURL, tt.args.body)
if (err != nil) != tt.wantErr {
t.Errorf("sendRequest() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
if !reflect.DeepEqual(got.StatusCode, tt.want) {
t.Errorf("sendRequest() got = %v, want %v", got, tt.want)
}
}
})
}
}
func TestClient_setRequestHeaders(t *testing.T) {
type fields struct {
httpClient *http.Client
ApiKey string
baseURL *url.URL
ctx context.Context
}
req := httptest.NewRequest("GET", "http://example.com:8080", nil)
type args struct {
req *http.Request
}
tests := []struct {
name string
fields fields
args args
}{
{
name: "test_success",
args: args{req: req},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := &Client{
httpClient: tt.fields.httpClient,
ApiKey: tt.fields.ApiKey,
baseURL: tt.fields.baseURL,
ctx: tt.fields.ctx,
}
req, _ := http.NewRequest(http.MethodGet, "test", nil)
client.setRequestHeaders(req)
req.Header.Get("Accept")
req.Header.Get("Content-Type")
req.Header.Get("x-api-key")
})
}
}
func TestNewClient(t *testing.T) {
type args struct {
ctx context.Context
apiKey string
httpClient *http.Client
}
testClient, _ := NewClient(context.Background(), "test", nil)
tests := []struct {
name string
args args
want *Client
wantErr bool
}{
{
name: "test_success",
args: args{
ctx: context.Background(),
apiKey: "test",
httpClient: http.DefaultClient,
},
want: testClient,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewClient(tt.args.ctx, tt.args.apiKey, tt.args.httpClient)
if (err != nil) != tt.wantErr {
t.Errorf("NewClient() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewClient() got = %v, want %v", got, tt.want)
}
})
}
}