forked from orijtech/otils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotils_test.go
358 lines (315 loc) · 7.4 KB
/
otils_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
package otils_test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
"github.com/orijtech/otils"
)
func TestToURLValues(t *testing.T) {
tests := [...]struct {
v interface{}
want string
mustErr bool
}{
0: {
v: &Request{
Source: "https://orijtech.com",
Logo: &Logo{
URL: "https://orijtech.com/favicon.ico",
Dimensions: &Dimension{
Width: 100, Height: 120,
Extra: map[string]interface{}{
"zoom": false, "shade": "45%",
},
},
},
},
want: "logo.dimension.extra.shade=45%25&logo.dimension.height=120&logo.dimension.width=100&logo.url=https%3A%2F%2Forijtech.com%2Ffavicon.ico&source=https%3A%2F%2Forijtech.com",
},
1: {
v: nil,
mustErr: true,
},
2: {
v: "thisway",
mustErr: true,
},
3: {
v: Request{
Logo: &Logo{
URL: "https://orijtech.com/favicon.ico",
Dimensions: &Dimension{
Width: 100, Height: 120,
BasicName: "flux",
Inner: &Dimension{Width: 120, Height: 240},
Extra: map[string]interface{}{
"zoom": false, "shade": "0%",
},
},
},
},
want: "logo.dimension.extra.shade=0%25&logo.dimension.height=120&logo.dimension.width=100&logo.url=https%3A%2F%2Forijtech.com%2Ffavicon.ico",
},
4: {
v: []*Request{
{Logo: &Logo{URL: "https://orijtech.com/favicon.ico"}},
nil,
},
want: "0=logo.url%3Dhttps%253A%252F%252Forijtech.com%252Ffavicon.ico",
},
5: {
v: map[string]int{"uno": 1, "zero": 0, "satu": 3, "saba": 7},
want: "saba=7&satu=3&uno=1&zero=0",
},
6: {
v: func() int { return 2 },
mustErr: true,
},
7: {
v: &Query{Nested: true, Page: 0},
want: "nested=true",
},
8: {
v: &Query{Nested: false, Page: 0},
want: "",
},
9: {
v: &Query{Nested: false, Page: 2},
want: "page=2",
},
}
for i, tt := range tests {
values, err := otils.ToURLValues(tt.v)
if tt.mustErr {
continue
}
if err != nil {
t.Errorf("#%d: err: %v", i, err)
continue
}
got, want := values.Encode(), tt.want
if got != want {
t.Errorf("#%d:\ngot: %q\nwant: %q", i, got, want)
}
}
}
type Dimension struct {
Width int `json:"width"`
Height int `json:"height"`
BasicName string `json:"-"`
Inner *Dimension `json:"-"`
Extra map[string]interface{} `json:"extra,omitempty"`
}
type Query struct {
Nested bool `json:"nested"`
Page int64 `json:"page"`
}
type Logo struct {
URL string `json:"url"`
Dimensions *Dimension `json:"dimension"`
}
type Request struct {
Logo *Logo `json:"logo"`
Source string `json:"source"`
}
func TestFirstNonEmptyString(t *testing.T) {
tests := [...]struct {
args []string
want string
}{
0: {args: []string{" ", "", "a", "b"}, want: "a"},
1: {args: []string{""}, want: ""},
2: {args: []string{"", " "}, want: ""},
3: {args: []string{"ABC", "DEF", " "}, want: "ABC"},
4: {args: []string{"", " DEF ", " "}, want: " DEF "},
}
for i, tt := range tests {
got := otils.FirstNonEmptyString(tt.args...)
want := tt.want
if got != want {
t.Errorf("#%d got=%q want=%q", i, got, want)
}
}
}
func TestCodedError(t *testing.T) {
// No panics expected
defer func() {
if r := recover(); r != nil {
t.Errorf("unexpected panic: %#v", r)
}
}()
tests := [...]struct {
err *otils.CodedError
msg string
code int
}{
0: {otils.MakeCodedError("200 OK", 200), "200 OK", 200},
1: {otils.MakeCodedError("failed to find it", 404), "failed to find it", 404},
// nil CodedError should not panic and should return 200 values.
2: {nil, "", 200},
}
for i, tt := range tests {
gotCode, wantCode := tt.err.Code(), tt.code
if gotCode != wantCode {
t.Errorf("#%d gotCode=%v wantCode=%v", i, gotCode, wantCode)
}
gotMsg, wantMsg := tt.err.Error(), tt.msg
if gotMsg != wantMsg {
t.Errorf("#%d gotMsg=%v wantMsg=%v", i, gotMsg, wantMsg)
}
}
}
func TestNumericBool(t *testing.T) {
tests := [...]struct {
str string
want otils.NumericBool
wantErr bool
}{
0: {str: "1", want: otils.NumericBool(true)},
1: {str: "0", want: otils.NumericBool(false)},
2: {str: "true", want: otils.NumericBool(true)},
3: {str: "false", want: otils.NumericBool(false)},
4: {str: "ping", wantErr: true},
}
for i, tt := range tests {
var nb otils.NumericBool
err := json.Unmarshal([]byte(tt.str), &nb)
if tt.wantErr {
if err == nil {
t.Errorf("#%d: expecting non-nil error", i)
}
continue
}
if err != nil {
t.Errorf("#%d: err: %v", i, err)
continue
}
got, want := nb, tt.want
if got != want {
t.Errorf("#%d got=%v want=%v", i, got, want)
}
}
}
func TestNullableTime(t *testing.T) {
tests := [...]struct {
str string
want otils.NullableTime
wantErr bool
}{
0: {str: "", wantErr: true},
1: {str: "0", wantErr: true},
2: {
str: `"2006-01-02T15:04:05Z"`,
want: otils.NullableTime(time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC)),
},
3: {str: "ping", wantErr: true},
}
for i, tt := range tests {
var nt otils.NullableTime
err := json.Unmarshal([]byte(tt.str), &nt)
if tt.wantErr {
if err == nil {
t.Errorf("#%d: expecting non-nil error", i)
}
continue
}
if err != nil {
t.Errorf("#%d: err: %v", i, err)
continue
}
tGot, tWant := time.Time(nt), time.Time(tt.want)
if !tGot.Equal(tWant) {
t.Errorf("#%d got=%v want=%v", i, tGot, tWant)
}
}
}
func TestNullableFloat64(t *testing.T) {
tests := [...]struct {
str string
wantErr bool
want otils.NullableFloat64
}{
0: {
str: "", wantErr: true,
},
1: {
str: `"-5.7"`, want: -5.7,
},
2: {
str: `"00010.9"`, want: 10.9,
},
3: {
str: `null`, want: 0,
},
}
for i, tt := range tests {
var recv otils.NullableFloat64
err := json.Unmarshal([]byte(tt.str), &recv)
if tt.wantErr {
if err == nil {
t.Errorf("#%d: expected a non-nil error", i)
}
continue
}
if err != nil {
t.Errorf("#%d: gotErr=%v", i, err)
continue
}
// Compare by JSON repr
gotBytes, wantBytes := jsonify(recv), jsonify(tt.want)
if !bytes.Equal(gotBytes, wantBytes) {
t.Errorf("#%d:\ngot: %s\nwant:%s", i, gotBytes, wantBytes)
}
}
}
func jsonify(v interface{}) []byte {
blob, _ := json.MarshalIndent(v, "", " ")
return blob
}
func TestCORSMiddleware(t *testing.T) {
tests := [...]struct {
cors *otils.CORS
want http.Header
}{
0: {want: nil},
1: {
cors: &otils.CORS{
Headers: []string{"X-Preflight"},
Methods: []string{"POST", "GET"},
}, want: http.Header{
"Access-Control-Allow-Methods": []string{"POST", "GET"},
"Access-Control-Allow-Headers": []string{"X-Preflight"},
},
},
2: {
cors: &otils.CORS{
Methods: []string{"POST", "DELETE"},
}, want: http.Header{
"Access-Control-Allow-Methods": []string{"POST", "DELETE"},
},
},
}
for i, tt := range tests {
handler := otils.CORSMiddleware(tt.cors, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Hello!"))
}))
tst := httptest.NewServer(handler)
res, err := tst.Client().Get(tst.URL)
tst.Close()
if err != nil {
t.Errorf("#%d unexpected err: %v", i, err)
continue
}
// Match headers
for wantKey, wantValues := range tt.want {
gotValues := res.Header[wantKey]
if !reflect.DeepEqual(gotValues, wantValues) {
t.Errorf("#%d: key: %v\ngotHeader: %#v\nwantHeader:%#v", i, wantKey, gotValues, wantValues)
}
}
}
}