-
Notifications
You must be signed in to change notification settings - Fork 0
/
gin_test.go
124 lines (96 loc) · 2.97 KB
/
gin_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
116
117
118
119
120
121
122
123
124
package UBT
import (
"bytes"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
type FormData struct {
A int `json:"a" form:"a"`
B int `json:"b" form:"a"`
}
func setupRouter() *gin.Engine {
r := gin.Default()
// 注入自动捕获网络数据的中间件
ubt := InitUBT()
r.Use(GinEsLog(ubt))
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
r.POST("/ping", func(c *gin.Context) {
formData := &FormData{}
//body, err := ioutil.ReadAll(c.Request.Body)
err := c.Bind(formData)
if err != nil {
c.JSON(400, gin.H{
"err": err,
})
return
}
c.JSON(200, gin.H{
"msg": "post pong",
"formData": formData,
})
})
return r
}
func TestPingRoute(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ping", nil)
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "pong", w.Body.String())
}
func TestHTTPTrafficMiddleWareByGet(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/ping", nil)
router.ServeHTTP(w, req)
ubtReqMessageText := ubt.messageTextStacks[0]
ubtResMessageText := ubt.messageTextStacks[1]
assert.Contains(t, ubtReqMessageText, `"logType":"request"`)
assert.Contains(t, ubtReqMessageText, `"method":"GET"`)
assert.Contains(t, ubtReqMessageText, `"url":"`)
assert.Contains(t, ubtResMessageText, `"data":"pong"`)
assert.Contains(t, ubtResMessageText, `"responseTime":"`)
ubt.clear()
}
func TestHTTPTrafficMiddleWareByPostFormData(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
params := url.Values{}
params.Set("a", "1")
params.Set("b", "2")
req, _ := http.NewRequest("POST", "/ping", strings.NewReader(params.Encode()))
router.ServeHTTP(w, req)
ubtReqMessageText := ubt.messageTextStacks[0]
ubtResMessageText := ubt.messageTextStacks[1]
assert.Contains(t, ubtReqMessageText, `"method":"POST"`)
assert.Contains(t, ubtReqMessageText, `"path":"/ping"`)
assert.Contains(t, ubtReqMessageText, `"postData":"a=1\u0026b=2"`)
assert.Contains(t, ubtResMessageText, `post pong`)
ubt.clear()
}
func TestHTTPTrafficMiddleWareByPostJSON(t *testing.T) {
router := setupRouter()
w := httptest.NewRecorder()
var jsonStr = []byte(`{"a": 1, "b": 2}`)
req, _ := http.NewRequest("POST", "/ping", bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
ubtReqMessageText := ubt.messageTextStacks[0]
ubtResMessageText := ubt.messageTextStacks[1]
assert.Contains(t, ubtReqMessageText, `"method":"POST"`)
assert.Contains(t, ubtReqMessageText, `"path":"/ping"`)
// ubt应该捕捉到了请求的数据
assert.Contains(t, ubtReqMessageText, `"postData":"{\"a\": 1, \"b\": 2}"`)
// ubt应该读取到了响应的数据
assert.Contains(t, ubtResMessageText, `\"formData\":{\"a\":1,\"b\":2}`)
assert.Contains(t, ubtResMessageText, `post pong`)
ubt.clear()
}