forked from momaek/easybind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind_test.go
66 lines (56 loc) · 1.51 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
package easybind
import (
"fmt"
"net/http"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type Status string
type queryUsersArgs struct {
IDs []Status `pos:"query:ids"`
Status *Status `pos:"query:status"`
Age int `json:"age"`
OK bool `json:"ok"`
}
type testCase struct {
Query map[string][]string
Path map[string]string
JSONBody map[string]interface{}
FormBody map[string][]string
}
func TestBind(t *testing.T) {
queries := url.Values{}
queries.Add("ids", "1")
queries.Add("ids", "3")
queries.Add("ids", "10086")
queries.Set("status", "active")
body := ` {
"time": "2021-22",
"age": 20,
"ok": true}`
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("https://hello.world/users?%s", queries.Encode()), strings.NewReader(body))
args := queryUsersArgs{}
err := Bind(req, &args)
assert.Nil(t, err)
assert.Equal(t, 3, len(args.IDs))
assert.Equal(t, Status("active"), *args.Status)
assert.Equal(t, 20, args.Age)
assert.Equal(t, true, args.OK)
fmt.Printf("===== %#v \n", args)
}
func TestBindGet(t *testing.T) {
queries := url.Values{}
queries.Add("ids", "1")
queries.Add("ids", "3")
queries.Add("ids", "10086")
queries.Set("status", "active")
req, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("https://hello.world/users?%s", queries.Encode()), nil)
args := queryUsersArgs{}
err := Bind(req, &args)
assert.Nil(t, err)
assert.Equal(t, 3, len(args.IDs))
assert.Equal(t, Status("active"), *args.Status)
fmt.Printf("===== %#v \n", args)
}