-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger_test.go
54 lines (46 loc) · 1.32 KB
/
logger_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
package ginzerolog
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func init() {
gin.SetMode(gin.TestMode)
}
func TestLogger(t *testing.T) {
assert.NotNil(t, Logger())
}
func TestLoggerWithWriter(t *testing.T) {
buffer := new(bytes.Buffer)
router := gin.New()
router.Use(LoggerWithWriter(buffer))
router.GET("/example", func(c *gin.Context) {})
router.POST("/error", func(c *gin.Context) {
c.Error(fmt.Errorf("myerror"))
})
performRequest(router, "GET", "/example?a=100")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "GET")
assert.Contains(t, buffer.String(), "/example")
assert.Contains(t, buffer.String(), "a=100")
buffer.Reset()
performRequest(router, "POST", "/error")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "POST")
assert.Contains(t, buffer.String(), "myerror")
buffer.Reset()
performRequest(router, "GET", "/notfound")
assert.Contains(t, buffer.String(), "404")
assert.Contains(t, buffer.String(), "GET")
assert.Contains(t, buffer.String(), "/notfound")
}
func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}