-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlogger_test.go
137 lines (104 loc) · 2.77 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
package logger
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
type LoggerSuite struct {
suite.Suite
req *http.Request
rl *responseLogger
w *testWriter
}
func (s *LoggerSuite) SetupTest() {
s.req = httptest.NewRequest(http.MethodGet, "/", nil)
s.req.RemoteAddr = "192.0.2.1:1234"
s.rl = &responseLogger{rw: testResponseWriter{}, start: time.Now()}
s.w = &testWriter{}
s.rl.Write([]byte("test-logger"))
}
func (s *LoggerSuite) TestRW() {
s.Equal(s.rl.Header(), s.rl.rw.Header())
s.rl.WriteHeader(http.StatusAccepted)
s.Equal(s.rl.status, http.StatusAccepted)
}
func (s *LoggerSuite) TestDefaultHanlder() {
dh := DefaultHandler(http.NotFoundHandler())
dh.ServeHTTP(s.rl, s.req)
}
func (s *LoggerSuite) TestHanlder() {
tw := testWriter{}
dh := Handler(http.NotFoundHandler(), &tw, TinyLoggerType)
dh.ServeHTTP(s.rl, s.req)
s.Equal("GET / 404 19 - 0.000 ms\n", string(tw.Bytes))
}
func (s *LoggerSuite) TestTiny() {
lh := loggerHanlder{
h: http.NotFoundHandler(),
formatType: TinyLoggerType,
writer: s.w,
}
lh.write(s.rl, s.req)
s.Equal("GET / 200 11 - 0.000 ms\n", string(s.w.Bytes))
}
func (s *LoggerSuite) TestShort() {
lh := loggerHanlder{
h: http.NotFoundHandler(),
formatType: ShortLoggerType,
writer: s.w,
}
lh.write(s.rl, s.req)
s.Equal("192.0.2.1:1234 - GET / HTTP/1.1 200 11 - 0.000 ms\n", string(s.w.Bytes))
}
func (s *LoggerSuite) TestDev() {
lh := loggerHanlder{
h: http.NotFoundHandler(),
formatType: DevLoggerType,
writer: s.w,
}
lh.write(s.rl, s.req)
s.Equal("GET / 200 0.000 ms - 11\n", string(s.w.Bytes))
}
func (s *LoggerSuite) TestCommon() {
lh := loggerHanlder{
h: http.NotFoundHandler(),
formatType: CommonLoggerType,
writer: s.w,
}
lh.write(s.rl, s.req)
s.Equal(`192.0.2.1:1234 - - [`+s.rl.start.Format(timeFormat)+`] "GET / HTTP/1.1" 200 11`+"\n", string(s.w.Bytes))
}
func (s *LoggerSuite) TestCombined() {
lh := loggerHanlder{
h: http.NotFoundHandler(),
formatType: CombineLoggerType,
writer: s.w,
}
lh.write(s.rl, s.req)
s.Equal(`192.0.2.1:1234 - - [`+s.rl.start.Format(timeFormat)+`] "GET / HTTP/1.1" 200 11 "" ""`+"\n", string(s.w.Bytes))
}
func TestLogger(t *testing.T) {
suite.Run(t, new(LoggerSuite))
}
type testResponseWriter struct {
header http.Header
}
func (trw testResponseWriter) Header() http.Header {
if trw.header != nil {
return trw.header
}
return make(http.Header)
}
func (trw testResponseWriter) Write(bytes []byte) (int, error) {
return len(bytes), nil
}
func (trw testResponseWriter) WriteHeader(status int) {}
type testWriter struct {
Bytes []byte
}
func (tw *testWriter) Write(b []byte) (n int, err error) {
tw.Bytes = append(tw.Bytes, b...)
return len(b), nil
}