-
Notifications
You must be signed in to change notification settings - Fork 4
/
log_test.go
110 lines (86 loc) · 2.41 KB
/
log_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
/*
* Copyright (c) Fabio da Silva Ribeiro <[email protected]>
* SPDX-License-Identifier: MIT
*/
package middleware
import (
"errors"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/labstack/echo/v4"
)
var testFields = map[string]string{
"remote_ip": logRemoteIP,
"uri": logURI,
"host": logHost,
"method": logMethod,
"status": logStatus,
"latency": logLatency,
"error": logError,
"empty": "",
"id": logID,
"path": logPath,
"route": logRoute,
"protocol": logProtocol,
"referer": logReferer,
"user_agent": logUserAgent,
"store": logHeaderPrefix + "store",
"filter_name": logQueryPrefix + "name",
"username": logFormPrefix + "username",
"session": logCookiePrefix + "session",
"latency_human": logLatencyHuman,
"bytes_in": logBytesIn,
"bytes_out": logBytesOut,
"user": logHeaderPrefix + "user",
}
func testHandler(ec echo.Context) error {
if v := ec.QueryParam("err"); v != "" {
return errors.New("error")
}
if v := ec.QueryParam("panic"); v != "" {
panic("unable to call")
}
return ec.String(http.StatusOK, "test")
}
func testCtx(t *testing.T, target string) echo.Context {
t.Helper()
req := httptest.NewRequest(echo.GET, target, nil)
rec := httptest.NewRecorder()
e := echo.New()
return e.NewContext(req, rec)
}
func reqCtx(t *testing.T) echo.Context {
return testCtx(t, "/some")
}
func errCtx(t *testing.T) echo.Context {
return testCtx(t, "/some?err=1")
}
func postCtx(t *testing.T) echo.Context {
t.Helper()
form := url.Values{}
form.Add("username", "doejohn")
u, _ := url.Parse("http://some/foo/456?name=john")
req := httptest.NewRequest(echo.POST, u.String(), strings.NewReader(form.Encode()))
req.Header.Add(echo.HeaderContentType, echo.MIMEApplicationForm)
req.Header.Add("Referer", "http://foo.bar")
req.Header.Add("User-Agent", "cli-agent")
req.Header.Add(echo.HeaderXForwardedFor, "http://foo.bar")
req.Header.Add("user", "admin")
req.AddCookie(&http.Cookie{
Name: "session",
Value: "A1B2C3",
})
rec := httptest.NewRecorder()
rec.Header().Add(echo.HeaderXRequestID, "123")
e := echo.New()
e.Add(echo.GET, "/foo/:id", testHandler)
ec := e.NewContext(req, rec)
e.Router().Find(echo.GET, u.RequestURI(), ec)
return ec
}
func panicCtx(t *testing.T) echo.Context {
return testCtx(t, "/some?panic=1")
}