-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_test.go
78 lines (73 loc) · 1.3 KB
/
query_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
package patch
import (
"net/url"
"testing"
"github.com/stretchr/testify/require"
)
func TestToQueryString(t *testing.T) {
tests := []struct {
name string
v interface{}
want string
wantErr bool
}{
{
name: "nil",
v: nil,
want: "",
},
{
name: "url.Values{}",
v: url.Values{"foo": {"bar", "bat"}},
want: "foo=bar&foo=bat",
},
{
name: "map[string][]string",
v: map[string][]string{
"foo": {"bar", "bat"},
},
want: "foo=bar&foo=bat",
},
{
name: "map[string]string",
v: map[string]string{
"foo": "bar",
"bat": "baz",
},
want: "bat=baz&foo=bar",
},
{
name: "empty struct",
v: struct{}{},
want: "",
},
{
name: "struct",
v: struct {
Foo string `form:"foo"`
Bar int `form:"bar"`
Bat bool `form:"bat"`
}{"foo", 5, true},
want: "bar=5&bat=true&foo=foo",
},
{
name: "struct with slice",
v: struct {
Foo []string `form:"foo"`
}{
Foo: []string{"bar", "bat"},
},
want: "foo=bar&foo=bat",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := toQueryString(tt.v, "form")
if (err != nil) != tt.wantErr {
t.Errorf("ToURLValues() error = %v, wantErr %v", err, tt.wantErr)
return
}
require.Equal(t, tt.want, got)
})
}
}