-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathquery_test.go
103 lines (83 loc) · 2.16 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
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
package gout
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/guonaihong/gout/debug"
"github.com/stretchr/testify/assert"
)
type queryWithSlice struct {
A []string `query:"a" form:"a"`
B string `query:"b" form:"b"`
}
func testQueryWithSliceServer(t *testing.T) *httptest.Server {
r := gin.New()
need := queryWithSlice{A: []string{"1", "2", "3"}, B: "b"}
r.GET("/query", func(c *gin.Context) {
got := queryWithSlice{}
err := c.ShouldBindQuery(&got)
assert.NoError(t, err)
assert.Equal(t, need, got)
})
return httptest.NewServer(http.HandlerFunc(r.ServeHTTP))
}
// 测试query接口,带slice的情况
func TestQuery_slice(t *testing.T) {
ts := testQueryWithSliceServer(t)
for _, v := range []interface{}{
queryWithSlice{A: []string{"1", "2", "3"}, B: "b"},
H{"a": []string{"1", "2", "3"}, "b": "b"},
A{"a", []string{"1", "2", "3"}, "b", "b"},
} {
err := GET(ts.URL + "/query").Debug(true).SetQuery(v).Do()
assert.NoError(t, err)
}
}
func TestQuery_NotIgnoreEmpty(t *testing.T) {
total := int32(0)
router := setupMethod(&total)
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
defer ts.Close()
query := H{
"t": 1296,
"callback": "searchresult",
"q": "美食",
"stype": 1,
"pagesize": 100,
"pagenum": 1,
"imageType": 2,
"imageColor": "",
"brand": "",
"imageSType": "",
"fr": 1,
"sortFlag": 1,
"imageUType": "",
"btype": "",
"authid": "",
"_": int64(1611822443760),
}
var out bytes.Buffer
SaveDebug := func() debug.Apply {
return DebugFunc(func(o *DebugOption) {
o.Write = &out
o.Debug = true
})
}
// 默认不忽略空值
err := GET(ts.URL).Debug(SaveDebug()).SetQuery(query).Do()
assert.NoError(t, err)
// 有authid字段
assert.NotEqual(t, bytes.Index(out.Bytes(), []byte("authid")), -1)
// 重置bytes.Buffer
out.Reset()
// 忽略空值
IgnoreEmpty()
// 默认不忽略空值
err = GET(ts.URL).Debug(SaveDebug()).SetQuery(query).Do()
assert.NoError(t, err)
// 没有authid字段
assert.Equal(t, bytes.Index(out.Bytes(), []byte("authid")), -1)
NotIgnoreEmpty()
}