-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathgindump_test.go
115 lines (97 loc) · 2.41 KB
/
gindump_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
package ginhelper_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/synapsecns/sanguine/core/ginhelper"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/gin-gonic/gin"
)
func init() {
gin.SetMode(gin.TestMode)
}
// nolint: unparam
func performRequest(ctx context.Context, r http.Handler, method, contentType string, path string, body io.Reader) *httptest.ResponseRecorder {
req, _ := http.NewRequestWithContext(ctx, method, path, body)
req.Header.Set("Content-Type", contentType)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
func TestMIMEJSON(t *testing.T) {
router := gin.New()
router.Use(ginhelper.Dump())
router.POST("/dump", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"ok": true,
"data": "gin-dump",
})
})
type params struct {
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
}
var httpdata = params{
StartTime: "2019-03-03",
EndTime: "2019-03-03",
}
b, err := json.Marshal(httpdata)
if err != nil {
fmt.Println("json format error:", err)
return
}
body := bytes.NewBuffer(b)
_ = performRequest(context.Background(), router, "POST", gin.MIMEJSON, "/dump", body)
}
func TestMIMEJSONWithOption(t *testing.T) {
router := gin.New()
router.Use(ginhelper.DumpWithOptions(true, false, true, true, false, func(dumpStr string) {
fmt.Println(dumpStr)
}))
router.POST("/dump", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"ok": true,
"data": "gin-dump",
})
})
type params struct {
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
}
var httpdata = params{
StartTime: "2019-03-03",
EndTime: "2019-03-03",
}
b, err := json.Marshal(httpdata)
if err != nil {
fmt.Println("json format error:", err)
return
}
body := bytes.NewBuffer(b)
_ = performRequest(context.Background(), router, "POST", gin.MIMEJSON, "/dump", body)
}
func TestMIMEPOSTFORM(t *testing.T) {
router := gin.New()
router.Use(ginhelper.Dump())
router.POST("/dump", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"ok": true,
"data": map[string]interface{}{
"name": "jfise",
"addr": "[email protected]",
},
})
})
form := make(url.Values)
form.Set("foo", "bar")
form.Add("foo", "bar2")
form.Set("bar", "baz")
body := strings.NewReader(form.Encode())
_ = performRequest(context.Background(), router, "POST", gin.MIMEPOSTForm, "/dump", body)
}