-
Notifications
You must be signed in to change notification settings - Fork 13
/
gin_test.go
58 lines (51 loc) · 1.29 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
package logging
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"testing"
"time"
"github.com/gin-gonic/gin"
)
func hello(c *gin.Context) {
c.Error(errors.New("test1"))
c.Error(errors.New("test2"))
c.JSON(200, "world")
}
func TestGinLogger(t *testing.T) {
gin.SetMode(gin.ReleaseMode)
app := gin.New()
app.Use(GinLogger())
app.GET("/hello", hello)
go app.Run()
time.Sleep(100 * time.Millisecond)
_, err := http.Get("http://localhost:8080/hello?k=v")
if err != nil {
t.Fatal(err)
}
}
func TestGinLoggerWithConfig(t *testing.T) {
gin.SetMode(gin.ReleaseMode)
app := gin.New()
conf := GinLoggerConfig{
EnableDetails: true,
EnableContextKeys: true,
EnableRequestBody: true,
EnableRequestForm: true,
EnableResponseBody: true,
EnableRequestHeader: true,
Formatter: func(c context.Context, m GinLogDetails) string { return fmt.Sprintln(m.StatusCode, m.RequestURI) },
TraceIDFunc: func(context.Context) string { return "xx-xx-xx-xx" },
SkipPaths: []string{},
}
app.Use(GinLoggerWithConfig(conf))
app.POST("/hello", hello)
go app.Run(":8888")
time.Sleep(100 * time.Millisecond)
_, err := http.Post("http://localhost:8888/hello?k=v", "application/json", bytes.NewReader([]byte(`{"k": "v"}`)))
if err != nil {
t.Fatal(err)
}
}