-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho_test.go
148 lines (139 loc) · 4.09 KB
/
echo_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
143
144
145
146
147
148
package logger
import (
"bytes"
"encoding/json"
"errors"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestEchoMiddleware(t *testing.T) {
testErr := errors.New("test err")
tests := []struct {
name string
config ConfigEcho
route func(server *echo.Echo)
requestUri string
response string
err error
responseStatusCode int
logLevel string
}{
{
name: "Test nil config success",
config: ConfigEcho{},
route: func(server *echo.Echo) {
server.GET("/hello/:name", func(ctx echo.Context) error {
logger := GetLogger(ctx.Request().Context())
name := ctx.Param("name")
logger.AddLog("request name %v", name)
return ctx.String(200, "hello "+name)
})
},
requestUri: "/hello/world",
response: "hello world",
err: nil,
responseStatusCode: http.StatusOK,
logLevel: "info",
},
{
name: "Test default config success",
config: DefaultConfigEcho,
route: func(server *echo.Echo) {
server.GET("/hello/:name", func(ctx echo.Context) error {
logger := GetLogger(ctx.Request().Context())
name := ctx.Param("name")
logger.AddLog("request name %v", name)
return ctx.String(200, "hello "+name)
})
},
requestUri: "/hello/world",
response: "hello world",
err: nil,
responseStatusCode: http.StatusOK,
logLevel: "info",
},
{
name: "Test skipp config success",
config: ConfigEcho{
SkipperEcho: func(context echo.Context) bool {
if context.Request().RequestURI == "/metrics" {
return true
}
return false
},
},
route: func(server *echo.Echo) {
server.GET("/metrics", func(ctx echo.Context) error {
logger := GetLogger(ctx.Request().Context())
logger.AddLog("metrics")
return ctx.String(200, "success")
})
},
requestUri: "/metrics",
err: nil,
responseStatusCode: http.StatusOK,
logLevel: "info",
response: "success",
},
{
name: "Test error",
config: ConfigEcho{BeforeFuncEcho: func(ctx echo.Context) {
}},
route: func(server *echo.Echo) {
server.GET("/hello/:name", func(ctx echo.Context) error {
logger := GetLogger(ctx.Request().Context())
name := ctx.Param("name")
logger.AddLog("request name %v", name)
ctx.Error(testErr)
return testErr
})
},
requestUri: "/hello/world",
response: "{\"message\":\"Internal Server Error\"}\n",
err: nil,
responseStatusCode: http.StatusInternalServerError,
logLevel: "error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := &bytes.Buffer{}
New(WithFormatter(&JSONFormatter{}), WithOutput(buf))
server := echo.New()
server.Use(EchoMiddleware(tt.config))
tt.route(server)
w := performRequest(server, "GET", tt.requestUri)
out, e := ioutil.ReadAll(w.Result().Body)
t.Logf("Out %v, err %v", string(out), e)
t.Logf("Log output %v", buf.String())
var data map[string]interface{}
if len(buf.String()) > 0 {
if err := json.Unmarshal(buf.Bytes(), &data); err != nil {
t.Error("unexpected error", err)
}
_, ok := data["STEP_1"]
uri, uriOk := data[URIField]
level, levelOk := data[FieldKeyLevel]
// TEST
assert.True(t, ok, `cannot found expected "STEP_1" field: %v`, data)
assert.True(t, uriOk, `cannot found expected "%v" field: %v`, URIField, data)
assert.Equal(t, tt.requestUri, uri)
assert.True(t, levelOk, `cannot found expected "%v" field: %v`, FieldKeyLevel, data)
assert.Equal(t, tt.logLevel, level)
}
assert.Equal(t, tt.err, e)
assert.Equal(t, tt.response, string(out))
assert.Equal(t, tt.responseStatusCode, w.Code)
})
}
}
func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
req := httptest.NewRequest(method, path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}