-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
54 lines (47 loc) · 1.42 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 looli
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLogger(t *testing.T) {
buffer := new(bytes.Buffer)
router := New()
router.Use(LoggerWithWriter(buffer))
router.Get("/a", func(c *Context) {})
router.Post("/a", func(c *Context) {})
router.Put("/a", func(c *Context) {})
router.Delete("/a", func(c *Context) {})
router.Patch("/a", func(c *Context) {})
router.Head("/a", func(c *Context) {})
router.Options("/a", func(c *Context) {})
methods := []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodPatch, http.MethodHead, http.MethodOptions}
for _, method := range methods {
buffer.Reset()
issueRequest(t, router, method, "/a")
assert.Contains(t, buffer.String(), method)
assert.Contains(t, buffer.String(), "/a")
assert.Contains(t, buffer.String(), "200")
buffer.Reset()
issueRequest(t, router, method, "/a/b")
assert.Contains(t, buffer.String(), method)
assert.Contains(t, buffer.String(), "/a/b")
assert.Contains(t, buffer.String(), "404")
}
}
func issueRequest(t *testing.T, router *Engine, method, path string) {
server := httptest.NewServer(router)
defer server.Close()
serverURL := server.URL
getReq, err := http.NewRequest(method, serverURL+path, nil)
if err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(getReq)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
}