-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog_test.go
320 lines (244 loc) Β· 10 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package gohm_test
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
"github.com/karrick/gohm"
)
func TestLogAllWithoutError(t *testing.T) {
logOutput := new(bytes.Buffer)
response := "something interesting"
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(response))
}), gohm.Config{LogWriter: logOutput})
handler.ServeHTTP(recorder, request)
if got, want := logOutput.String(), fmt.Sprintf(" %d ", http.StatusOK); !strings.Contains(got, want) {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
func TestLogAllWithError(t *testing.T) {
logOutput := new(bytes.Buffer)
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gohm.Error(w, "some error", http.StatusConflict)
}), gohm.Config{LogWriter: logOutput})
handler.ServeHTTP(recorder, request)
if got, want := logOutput.String(), fmt.Sprintf(" %d ", http.StatusConflict); !strings.Contains(got, want) {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
// then set log errors, and try both error and not error
func TestLogErrorsWhenWriteHeaderErrorStatus(t *testing.T) {
logBitmask := gohm.LogStatusErrors
logOutput := new(bytes.Buffer)
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
}), gohm.Config{LogBitmask: &logBitmask, LogWriter: logOutput})
handler.ServeHTTP(recorder, request)
if got, want := logOutput.String(), fmt.Sprintf(" %d ", http.StatusForbidden); !strings.Contains(got, want) {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
func TestLogErrorsWithError(t *testing.T) {
logBitmask := gohm.LogStatusErrors
logOutput := new(bytes.Buffer)
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gohm.Error(w, "some error", http.StatusForbidden)
}), gohm.Config{LogBitmask: &logBitmask, LogWriter: logOutput})
handler.ServeHTTP(recorder, request)
if got, want := logOutput.String(), fmt.Sprintf(" %d ", http.StatusForbidden); !strings.Contains(got, want) {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
func TestLogErrorsWithoutError(t *testing.T) {
logBitmask := gohm.LogStatusErrors
logOutput := new(bytes.Buffer)
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// no error
}), gohm.Config{LogBitmask: &logBitmask, LogWriter: logOutput})
handler.ServeHTTP(recorder, request)
if got, want := logOutput.String(), ""; got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
const apacheTimeFormat = "02/Jan/2006:15:04:05 -0700"
func TestLogWithFormatStatusEscapedCharacters(t *testing.T) {
format := "\\{client-ip\\}"
logBitmask := gohm.LogStatusAll
logOutput := new(bytes.Buffer)
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gohm.Error(w, "some error", http.StatusForbidden)
}), gohm.Config{LogBitmask: &logBitmask, LogWriter: logOutput, LogFormat: format})
handler.ServeHTTP(recorder, request)
if got, want := logOutput.String(), "{client-ip}\n"; got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
func TestLogWithFormatStatic(t *testing.T) {
format := "{client} {client-ip} {client-port} - \"{method} {uri} {proto}\" {status} {bytes}"
t.Run("ipv4", func(t *testing.T) {
logOutput := new(bytes.Buffer)
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
request.RemoteAddr = "1.2.3.4:1234"
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gohm.Error(w, "some error", http.StatusForbidden)
}), gohm.Config{LogFormat: format, LogWriter: logOutput})
handler.ServeHTTP(recorder, request)
want := fmt.Sprintf("1.2.3.4:1234 1.2.3.4 1234 - \"GET /some/url HTTP/1.1\" %d 26\n", http.StatusForbidden)
if got := logOutput.String(); got != want {
t.Fatalf("\nGOT:\n\t%#v\nWant:\n\t%#v", got, want)
}
})
t.Run("ipv6", func(t *testing.T) {
logOutput := new(bytes.Buffer)
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
request.RemoteAddr = "[::1]:1234"
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gohm.Error(w, "some error", http.StatusForbidden)
}), gohm.Config{LogFormat: format, LogWriter: logOutput})
handler.ServeHTTP(recorder, request)
want := fmt.Sprintf("[::1]:1234 ::1 1234 - \"GET /some/url HTTP/1.1\" %d 26\n", http.StatusForbidden)
if got := logOutput.String(); got != want {
t.Fatalf("\nGOT:\n\t%#v\nWant:\n\t%#v", got, want)
}
})
}
func TestLogWithFormatIgnoresInvalidTokens(t *testing.T) {
format := "This is an {invalid-token} with a {status} after it"
logBitmask := gohm.LogStatusAll
logOutput := new(bytes.Buffer)
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gohm.Error(w, "some error", http.StatusForbidden)
}), gohm.Config{LogBitmask: &logBitmask, LogFormat: format, LogWriter: logOutput})
handler.ServeHTTP(recorder, request)
want := fmt.Sprintf("This is an {invalid-token} with a %d after it\n", http.StatusForbidden)
if got := logOutput.String(); got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
func timeFromEpochString(t *testing.T, value string) time.Time {
epoch, err := strconv.Atoi(value)
if err != nil {
t.Fatal(err)
}
return time.Unix(int64(epoch), 0)
}
func TestLogWithFormatDynamic(t *testing.T) {
format := "{begin-epoch} {end-epoch} {begin} {begin-iso8601} {end} {end-iso8601} {duration}"
logBitmask := gohm.LogStatusAll
logOutput := new(bytes.Buffer)
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gohm.Error(w, "some error", http.StatusForbidden)
}), gohm.Config{LogBitmask: &logBitmask, LogFormat: format, LogWriter: logOutput})
beforeTime := time.Now()
handler.ServeHTTP(recorder, request)
afterTime := time.Now()
// first, grab the begin-epoch, and compute the other begin values
got := logOutput.String()
indexFirstSpace := strings.IndexByte(got, ' ')
beginString := got[:indexFirstSpace]
beginTime := timeFromEpochString(t, beginString)
if beginTime.Before(beforeTime.Truncate(time.Second)) {
t.Errorf("Begin: %v; Before: %v", beginTime, beforeTime)
}
if beginTime.After(afterTime) {
t.Errorf("Begin: %v; After: %v", beginTime, afterTime)
}
// first, grab the end-epoch, and compute the other end values
indexSecondSpace := indexFirstSpace + strings.IndexByte(got[indexFirstSpace+1:], ' ')
endString := got[indexFirstSpace+1 : indexSecondSpace+1]
endTime := timeFromEpochString(t, endString)
if endTime.Before(beforeTime.Truncate(time.Second)) {
t.Errorf("End: %v; Before: %v", endTime, beforeTime)
}
if endTime.After(afterTime) {
t.Errorf("End: %v; After: %v", endTime, afterTime)
}
if got, want := got[len(got)-1:], "\n"; got != want {
t.Errorf("Got: %#v; #Want: %#v", got, want)
}
indexFinalSpace := strings.LastIndexByte(got, ' ')
durationString := got[indexFinalSpace+1 : len(got)-1]
// to check duration, let's just ensure we can parse it as a float, and it's less than the span duration
durationFloat, err := strconv.ParseFloat(durationString, 64)
if err != nil {
t.Errorf("GOT: %v; WANT: %v", err, nil)
}
durationMilliseconds := afterTime.Sub(beforeTime).Nanoseconds() / 1000
if int64(durationFloat*1000000) > durationMilliseconds {
t.Errorf("durationFloat: %v; durationMilliseconds: %v", durationFloat, durationMilliseconds)
}
want := fmt.Sprintf("%s %s %s %s %s %s %s\n", beginString, endString,
beginTime.UTC().Format(apacheTimeFormat),
beginTime.UTC().Format(time.RFC3339),
endTime.UTC().Format(apacheTimeFormat),
endTime.UTC().Format(time.RFC3339),
durationString)
if got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
func TestLogWithFormatEmoji(t *testing.T) {
format := "π {status} π" // log format starts and ends with a multi-byte rune
logOutput := new(bytes.Buffer)
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gohm.Error(w, "some error", http.StatusForbidden)
}), gohm.Config{LogFormat: format, LogWriter: logOutput})
handler.ServeHTTP(recorder, request)
got := logOutput.String()
want := fmt.Sprintf("π %d π\n", http.StatusForbidden)
if got != want {
t.Fatalf("GOT: %v; WANT: %v", got, want)
}
}
func BenchmarkWithLogsElided(b *testing.B) {
logBitmask := gohm.LogStatusErrors
logOutput := new(bytes.Buffer)
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// this does not error, so nothing ought to be logged
}), gohm.Config{LogBitmask: &logBitmask, LogWriter: logOutput})
b.ResetTimer()
for i := 0; i < b.N; i++ {
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
handler.ServeHTTP(recorder, request)
logOutput.Reset()
}
}
func BenchmarkWithLogs(b *testing.B) {
logOutput := new(bytes.Buffer)
handler := gohm.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden) // error class forces log entry
}), gohm.Config{LogWriter: logOutput})
b.ResetTimer()
for i := 0; i < b.N; i++ {
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/some/url", nil)
handler.ServeHTTP(recorder, request)
logOutput.Reset()
}
}