-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
142 lines (133 loc) · 3.6 KB
/
middleware_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package eglm
import (
"bytes"
"fmt"
"github.com/GameWith/gwlog/formatter"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/GameWith/gwlog"
"github.com/labstack/echo/v4"
)
func TestMiddleware(t *testing.T) {
e := echo.New()
buf := new(bytes.Buffer)
logger := gwlog.GetLogger()
logger.SetOutput(buf)
e.Logger = logger
e.Use(Middleware(&Config{}))
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "OK")
})
status, _ := request(http.MethodGet, "/", e)
if status != http.StatusOK {
t.Errorf("got = %v, want = %v", status, http.StatusOK)
}
if strings.Contains(buf.String(), "type=ACCESS") == false {
t.Errorf("invalid buf: %v", buf.String())
}
}
func TestMiddleware_skipper(t *testing.T) {
e := echo.New()
buf := new(bytes.Buffer)
logger := gwlog.GetLogger()
logger.SetOutput(buf)
e.Logger = logger
e.Use(Middleware(&Config{
Skipper: func(c echo.Context) bool {
return c.Path() == "/"
},
}))
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "OK")
})
status, _ := request(http.MethodGet, "/", e)
if status != http.StatusOK {
t.Errorf("got = %v, want = %v", status, http.StatusOK)
}
if 0 != buf.Len() {
t.Errorf("invalid buf: %v", buf.String())
}
}
func TestMiddleware_loggingFunc(t *testing.T) {
e := echo.New()
buf := new(bytes.Buffer)
logger := gwlog.GetLogger()
logger.SetOutput(buf)
e.Logger = logger
e.Use(Middleware(&Config{
LoggingFunc: func(logger gwlog.Logger, _ *Parameter, _ echo.Context) error {
logger.Print("a")
return nil
},
}))
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "OK")
})
status, _ := request(http.MethodGet, "/", e)
if status != http.StatusOK {
t.Errorf("got = %v, want = %v", status, http.StatusOK)
}
if strings.Contains(buf.String(), "msg=a") == false {
t.Errorf("invalid buf: %v", buf.String())
}
}
func TestMiddleware_error(t *testing.T) {
e := echo.New()
buf := new(bytes.Buffer)
logger := gwlog.GetLogger()
logger.SetOutput(buf)
e.Logger = logger
e.Use(Middleware(&Config{}))
e.GET("/", func(c echo.Context) error {
return fmt.Errorf("ERROR")
})
status, _ := request(http.MethodGet, "/", e)
if status != http.StatusInternalServerError {
t.Errorf("got = %v, want = %v", status, http.StatusInternalServerError)
}
if strings.Contains(buf.String(), "error=ERROR") == false {
t.Errorf("NotFound Text: ERROR")
}
}
func TestMiddleware_json(t *testing.T) {
e := echo.New()
buf := new(bytes.Buffer)
logger := gwlog.GetLogger()
logger.SetFormatter(&formatter.JSONFormatter{})
logger.SetOutput(buf)
e.Logger = logger
e.Use(Middleware(&Config{}))
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "OK")
})
status, _ := request(http.MethodGet, "/", e)
if status != http.StatusOK {
t.Errorf("got = %v, want = %v", status, http.StatusOK)
}
if strings.Contains(buf.String(), `"message":"GET /"`) == false {
t.Errorf("NotFound Text: ERROR")
}
}
func TestMiddleware_defaultLogger(t *testing.T) {
e := echo.New()
e.Use(Middleware(&Config{}))
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "OK")
})
status, body := request(http.MethodGet, "/", e)
if status != http.StatusInternalServerError {
t.Errorf("got = %v, want = %v", status, http.StatusInternalServerError)
}
bw := `{"message":"invalid instance type. only gwlog.Logger"}` + "\n"
if body != bw {
t.Errorf("got = %v, want = %v", body, bw)
}
}
func request(method, path string, e *echo.Echo) (int, string) {
req := httptest.NewRequest(method, path, nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
return rec.Code, rec.Body.String()
}