-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind_test.go
79 lines (74 loc) · 2.14 KB
/
bind_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
package querybinder_test
import (
"net/url"
"testing"
"github.com/stretchr/testify/require"
querybinder "github.com/wgarunap/url-query-binder"
)
func TestBinder_Bind(t *testing.T) {
type Obj struct {
Query string `bind:"query,required"`
StringParam string `bind:"string_param"`
SliceParam []string `bind:"slice_param"`
IntParam int `bind:"int_param"`
}
tests := map[string]struct {
obj interface{}
url string
expect interface{}
expectedErr error
}{
"url has multiple parameters": {
obj: &Obj{},
url: "/get?query=something&string_param=testing&slice_param=param1&slice_param=param2&int_param=12",
expect: &Obj{
Query: "something",
StringParam: "testing",
SliceParam: []string{"param1", "param2"},
IntParam: 12,
},
expectedErr: nil,
},
"url has multiple parameters with comma separation, (should not treated as separate params)": {
obj: &Obj{},
url: "/get?query=something&string_param=testing&slice_param=param1,param2&int_param=12",
expect: &Obj{
Query: "something",
StringParam: "testing",
SliceParam: []string{"param1,param2"},
IntParam: 12,
},
expectedErr: nil,
},
"url has only few params": {
obj: &Obj{},
url: "/get?query=something&string_param=testing",
expect: &Obj{
Query: "something",
StringParam: "testing",
},
expectedErr: nil,
},
"url is missing required query parameter": {
obj: &Obj{},
url: "/get?string_param=testing&slice_param=param1&slice_param=param2&int_param=12",
expect: &Obj{},
expectedErr: querybinder.ErrMissingQueryParam,
},
"invalid object value passed": {
obj: "",
url: "/get?query=something&string_param=testing&slice_param=param1&slice_param=param2&int_param=12",
expect: "",
expectedErr: querybinder.ErrInvalidObjectType,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
qb := querybinder.NewQueryBinder()
u, _ := url.Parse(test.url)
err := qb.Bind(test.obj, u)
require.ErrorIs(t, err, test.expectedErr)
require.Equal(t, test.expect, test.obj)
})
}
}