This repository has been archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_test.go
158 lines (147 loc) · 4.34 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
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
149
150
151
152
153
154
155
156
157
158
package logger
import (
"bytes"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/vicanso/elton"
)
func TestGetHumanReadableSize(t *testing.T) {
if getHumanReadableSize(1024*1024) != "1MB" {
t.Fatalf("1024 * 1024 should be 1MB")
}
if getHumanReadableSize(1024*1024+500*1024) != "1.49MB" {
t.Fatalf("1024*1024+500*1024 should be 1.49MB")
}
if getHumanReadableSize(1024) != "1KB" {
t.Fatalf("1024 should be 1KB")
}
if getHumanReadableSize(1024+500) != "1.49KB" {
t.Fatalf("1024+500 should be 1.49KB")
}
if getHumanReadableSize(500) != "500B" {
t.Fatalf("500 should be 500B")
}
}
func TestLogger(t *testing.T) {
t.Run("normal", func(t *testing.T) {
config := Config{
Format: "{host} {remote} {real-ip} {method} {path} {proto} {query} {scheme} {uri} {referer} {userAgent} {size} {size-human} {status} {payload-size} {payload-size-human}",
OnLog: func(log string, _ *elton.Context) {
if log != "aslant.site 192.0.2.1:1234 192.0.2.1 GET / HTTP/1.1 a=1&b=2 HTTPS https://aslant.site/?a=1&b=2 https://aslant.site/ test-agent 13 13B 200 12 12B" {
t.Fatalf("log format fail")
}
},
}
m := New(config)
req := httptest.NewRequest("GET", "https://aslant.site/?a=1&b=2", nil)
req.Header.Set("Referer", "https://aslant.site/")
req.Header.Set("User-Agent", "test-agent")
resp := httptest.NewRecorder()
c := elton.NewContext(resp, req)
c.BodyBuffer = bytes.NewBufferString("response-body")
c.RequestBody = []byte("request-body")
c.StatusCode = 200
c.Next = func() error {
return nil
}
m(c)
})
t.Run("latency", func(t *testing.T) {
config := Config{
Format: "{latency} {latency-ms}",
OnLog: func(log string, _ *elton.Context) {
if len(strings.Split(log, " ")) != 2 {
t.Fatalf("get latency fail")
}
},
}
m := New(config)
req := httptest.NewRequest("GET", "https://aslant.iste/?a=1&b=2", nil)
resp := httptest.NewRecorder()
c := elton.NewContext(resp, req)
c.Next = func() error {
time.Sleep(time.Second)
return nil
}
m(c)
})
t.Run("when", func(t *testing.T) {
config := Config{
Format: "{when} {when-iso} {when-utc-iso} {when-unix} {when-iso-ms} {when-utc-iso-ms}",
OnLog: func(log string, _ *elton.Context) {
if len(strings.Split(log, " ")) != 6 {
t.Fatalf("get when fail")
}
},
}
m := New(config)
req := httptest.NewRequest("GET", "https://aslant.iste/?a=1&b=2", nil)
resp := httptest.NewRecorder()
c := elton.NewContext(resp, req)
c.Next = func() error {
return nil
}
m(c)
})
t.Run("cookie", func(t *testing.T) {
config := Config{
Format: "{~jt}",
OnLog: func(log string, _ *elton.Context) {
if log != "abc" {
t.Fatalf("get cookie value fail")
}
},
}
m := New(config)
req := httptest.NewRequest("GET", "https://aslant.iste/?a=1&b=2", nil)
req.AddCookie(&http.Cookie{
Name: "jt",
Value: "abc",
})
resp := httptest.NewRecorder()
c := elton.NewContext(resp, req)
c.Next = func() error {
return nil
}
m(c)
})
t.Run("header", func(t *testing.T) {
config := Config{
Format: "{>X-Token} {<X-Response-Id} place-holder",
OnLog: func(log string, _ *elton.Context) {
if log != "abc def place-holder" {
t.Fatalf("get header value fail")
}
},
}
m := New(config)
req := httptest.NewRequest("GET", "https://aslant.iste/?a=1&b=2", nil)
req.Header.Set("X-Token", "abc")
resp := httptest.NewRecorder()
c := elton.NewContext(resp, req)
c.SetHeader("X-Response-Id", "def")
c.Next = func() error {
return nil
}
m(c)
})
t.Run("get log function", func(t *testing.T) {
layout := "{host} {remote} {real-ip} {method} {path} {proto} {query} {scheme} {uri} {referer} {userAgent} {size} {size-human} {status} {payload-size} {payload-size-human}"
fn := GenerateLog(layout)
req := httptest.NewRequest("GET", "https://aslant.site/?a=1&b=2", nil)
req.Header.Set("Referer", "https://aslant.site/")
req.Header.Set("User-Agent", "test-agent")
resp := httptest.NewRecorder()
c := elton.NewContext(resp, req)
c.BodyBuffer = bytes.NewBufferString("response-body")
c.RequestBody = []byte("request-body")
c.StatusCode = 200
startedAt := time.Now()
if fn(c, startedAt) != "aslant.site 192.0.2.1:1234 192.0.2.1 GET / HTTP/1.1 a=1&b=2 HTTPS https://aslant.site/?a=1&b=2 https://aslant.site/ test-agent 13 13B 200 12 12B" {
t.Fatalf("log function fail")
}
})
}