forked from gorilla/handlers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_test.go
385 lines (329 loc) · 10.2 KB
/
logging_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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright 2013 The Gorilla Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package handlers
import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
"math/rand"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestMakeLogger(t *testing.T) {
rec := httptest.NewRecorder()
logger, w := makeLogger(rec)
// initial status
if logger.Status() != http.StatusOK {
t.Fatalf("wrong status, got %d want %d", logger.Status(), http.StatusOK)
}
// WriteHeader
w.WriteHeader(http.StatusInternalServerError)
if logger.Status() != http.StatusInternalServerError {
t.Fatalf("wrong status, got %d want %d", logger.Status(), http.StatusInternalServerError)
}
// Write
w.Write([]byte(ok))
if logger.Size() != len(ok) {
t.Fatalf("wrong size, got %d want %d", logger.Size(), len(ok))
}
// Header
w.Header().Set("key", "value")
if val := w.Header().Get("key"); val != "value" {
t.Fatalf("wrong header, got %s want %s", val, "value")
}
}
func TestLoggerCleanup(t *testing.T) {
rand.Seed(time.Now().UnixNano())
rbuf := make([]byte, 128)
if _, err := rand.Read(rbuf); err != nil {
t.Fatalf("Failed to generate random content: %v", err)
}
contents := base64.StdEncoding.EncodeToString(rbuf)
var body bytes.Buffer
body.WriteString(fmt.Sprintf(`
--boundary
Content-Disposition: form-data; name="buzz"; filename="example.txt"
%s
--boundary--
`, contents))
r := multipart.NewReader(&body, "boundary")
form, err := r.ReadForm(0) // small max memory to force flush to disk
if err != nil {
t.Fatalf("Failed to read multipart form: %v", err)
}
tmpFiles, err := ioutil.ReadDir(os.TempDir())
if err != nil {
t.Fatalf("Failed to list %s: %v", os.TempDir(), err)
}
var tmpFile string
for _, f := range tmpFiles {
if !strings.HasPrefix(f.Name(), "multipart-") {
continue
}
path := filepath.Join(os.TempDir(), f.Name())
switch b, err := ioutil.ReadFile(path); {
case err != nil:
t.Fatalf("Failed to read %s: %v", path, err)
case string(b) != contents:
continue
default:
tmpFile = path
break
}
}
if tmpFile == "" {
t.Fatal("Could not find multipart form tmp file")
}
req := newRequest("GET", "/subdir/asdf")
req.MultipartForm = form
var buf bytes.Buffer
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL.Path = "/" // simulate http.StripPrefix and friends
w.WriteHeader(200)
})
logger := LoggingHandler(&buf, handler)
logger.ServeHTTP(httptest.NewRecorder(), req)
if _, err := os.Stat(tmpFile); err == nil || !os.IsNotExist(err) {
t.Fatalf("Expected %s to not exist, got %v", tmpFile, err)
}
}
func TestLogPathRewrites(t *testing.T) {
var buf bytes.Buffer
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.URL.Path = "/" // simulate http.StripPrefix and friends
w.WriteHeader(200)
})
logger := LoggingHandler(&buf, handler)
logger.ServeHTTP(httptest.NewRecorder(), newRequest("GET", "/subdir/asdf"))
if !strings.Contains(buf.String(), "GET /subdir/asdf HTTP") {
t.Fatalf("Got log %#v, wanted substring %#v", buf.String(), "GET /subdir/asdf HTTP")
}
}
func BenchmarkWriteLog(b *testing.B) {
loc, err := time.LoadLocation("Europe/Warsaw")
if err != nil {
b.Fatalf(err.Error())
}
ts := time.Date(1983, 05, 26, 3, 30, 45, 0, loc)
req := newRequest("GET", "http://example.com")
req.RemoteAddr = "192.168.100.5"
b.ResetTimer()
params := LogFormatterParams{
Request: req,
URL: *req.URL,
TimeStamp: ts,
StatusCode: http.StatusUnauthorized,
Size: 500,
}
buf := &bytes.Buffer{}
for i := 0; i < b.N; i++ {
buf.Reset()
writeLog(buf, params)
}
}
func TestLogFormatterWriteLog_Scenario1(t *testing.T) {
formatter := writeLog
expected := "192.168.100.5 - - [26/May/1983:03:30:45 +0200] \"GET / HTTP/1.1\" 200 100\n"
LoggingScenario1(t, formatter, expected)
}
func TestLogFormatterCombinedLog_Scenario1(t *testing.T) {
formatter := writeCombinedLog
expected := "192.168.100.5 - - [26/May/1983:03:30:45 +0200] \"GET / HTTP/1.1\" 200 100 \"http://example.com\" " +
"\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) " +
"AppleWebKit/537.33 (KHTML, like Gecko) Chrome/27.0.1430.0 Safari/537.33\"\n"
LoggingScenario1(t, formatter, expected)
}
func TestLogFormatterWriteLog_Scenario2(t *testing.T) {
formatter := writeLog
expected := "192.168.100.5 - - [26/May/1983:03:30:45 +0200] \"CONNECT www.example.com:443 HTTP/2.0\" 200 100\n"
LoggingScenario2(t, formatter, expected)
}
func TestLogFormatterCombinedLog_Scenario2(t *testing.T) {
formatter := writeCombinedLog
expected := "192.168.100.5 - - [26/May/1983:03:30:45 +0200] \"CONNECT www.example.com:443 HTTP/2.0\" 200 100 \"http://example.com\" " +
"\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) " +
"AppleWebKit/537.33 (KHTML, like Gecko) Chrome/27.0.1430.0 Safari/537.33\"\n"
LoggingScenario2(t, formatter, expected)
}
func TestLogFormatterWriteLog_Scenario3(t *testing.T) {
formatter := writeLog
expected := "192.168.100.5 - kamil [26/May/1983:03:30:45 +0200] \"GET / HTTP/1.1\" 401 500\n"
LoggingScenario3(t, formatter, expected)
}
func TestLogFormatterCombinedLog_Scenario3(t *testing.T) {
formatter := writeCombinedLog
expected := "192.168.100.5 - kamil [26/May/1983:03:30:45 +0200] \"GET / HTTP/1.1\" 401 500 \"http://example.com\" " +
"\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) " +
"AppleWebKit/537.33 (KHTML, like Gecko) Chrome/27.0.1430.0 Safari/537.33\"\n"
LoggingScenario3(t, formatter, expected)
}
func TestLogFormatterWriteLog_Scenario4(t *testing.T) {
formatter := writeLog
expected := "192.168.100.5 - - [26/May/1983:03:30:45 +0200] \"GET /test?abc=hello%20world&a=b%3F HTTP/1.1\" 200 100\n"
LoggingScenario4(t, formatter, expected)
}
func TestLogFormatterCombinedLog_Scenario5(t *testing.T) {
formatter := writeCombinedLog
expected := "::1 - kamil [26/May/1983:03:30:45 +0200] \"GET / HTTP/1.1\" 200 100 \"http://example.com\" " +
"\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) " +
"AppleWebKit/537.33 (KHTML, like Gecko) Chrome/27.0.1430.0 Safari/537.33\"\n"
LoggingScenario5(t, formatter, expected)
}
func LoggingScenario1(t *testing.T, formatter LogFormatter, expected string) {
loc, err := time.LoadLocation("Europe/Warsaw")
if err != nil {
panic(err)
}
ts := time.Date(1983, 05, 26, 3, 30, 45, 0, loc)
// A typical request with an OK response
req := constructTypicalRequestOk()
buf := new(bytes.Buffer)
params := LogFormatterParams{
Request: req,
URL: *req.URL,
TimeStamp: ts,
StatusCode: http.StatusOK,
Size: 100,
}
formatter(buf, params)
log := buf.String()
if log != expected {
t.Fatalf("wrong log, got %q want %q", log, expected)
}
}
func LoggingScenario2(t *testing.T, formatter LogFormatter, expected string) {
loc, err := time.LoadLocation("Europe/Warsaw")
if err != nil {
panic(err)
}
ts := time.Date(1983, 05, 26, 3, 30, 45, 0, loc)
// CONNECT request over http/2.0
req := constructConnectRequest()
buf := new(bytes.Buffer)
params := LogFormatterParams{
Request: req,
URL: *req.URL,
TimeStamp: ts,
StatusCode: http.StatusOK,
Size: 100,
}
formatter(buf, params)
log := buf.String()
if log != expected {
t.Fatalf("wrong log, got %q want %q", log, expected)
}
}
func LoggingScenario3(t *testing.T, formatter LogFormatter, expected string) {
loc, err := time.LoadLocation("Europe/Warsaw")
if err != nil {
panic(err)
}
ts := time.Date(1983, 05, 26, 3, 30, 45, 0, loc)
// Request with an unauthorized user
req := constructTypicalRequestOk()
req.URL.User = url.User("kamil")
buf := new(bytes.Buffer)
params := LogFormatterParams{
Request: req,
URL: *req.URL,
TimeStamp: ts,
StatusCode: http.StatusUnauthorized,
Size: 500,
}
formatter(buf, params)
log := buf.String()
if log != expected {
t.Fatalf("wrong log, got %q want %q", log, expected)
}
}
func LoggingScenario4(t *testing.T, formatter LogFormatter, expected string) {
loc, err := time.LoadLocation("Europe/Warsaw")
if err != nil {
panic(err)
}
ts := time.Date(1983, 05, 26, 3, 30, 45, 0, loc)
// Request with url encoded parameters
req := constructEncodedRequest()
buf := new(bytes.Buffer)
params := LogFormatterParams{
Request: req,
URL: *req.URL,
TimeStamp: ts,
StatusCode: http.StatusOK,
Size: 100,
}
formatter(buf, params)
log := buf.String()
if log != expected {
t.Fatalf("wrong log, got %q want %q", log, expected)
}
}
func LoggingScenario5(t *testing.T, formatter LogFormatter, expected string) {
loc, err := time.LoadLocation("Europe/Warsaw")
if err != nil {
panic(err)
}
ts := time.Date(1983, 05, 26, 3, 30, 45, 0, loc)
req := constructTypicalRequestOk()
req.URL.User = url.User("kamil")
req.RemoteAddr = "::1"
buf := new(bytes.Buffer)
params := LogFormatterParams{
Request: req,
URL: *req.URL,
TimeStamp: ts,
StatusCode: http.StatusOK,
Size: 100,
}
formatter(buf, params)
log := buf.String()
if log != expected {
t.Fatalf("wrong log, got %q want %q", log, expected)
}
}
// A typical request with an OK response
func constructTypicalRequestOk() *http.Request {
req := newRequest("GET", "http://example.com")
req.RemoteAddr = "192.168.100.5"
req.Header.Set("Referer", "http://example.com")
req.Header.Set(
"User-Agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.33 "+
"(KHTML, like Gecko) Chrome/27.0.1430.0 Safari/537.33",
)
return req
}
// CONNECT request over http/2.0
func constructConnectRequest() *http.Request {
req := &http.Request{
Method: "CONNECT",
Host: "www.example.com:443",
Proto: "HTTP/2.0",
ProtoMajor: 2,
ProtoMinor: 0,
RemoteAddr: "192.168.100.5",
Header: http.Header{},
URL: &url.URL{Host: "www.example.com:443"},
}
req.Header.Set("Referer", "http://example.com")
req.Header.Set(
"User-Agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.33 "+
"(KHTML, like Gecko) Chrome/27.0.1430.0 Safari/537.33",
)
return req
}
func constructEncodedRequest() *http.Request {
req := constructTypicalRequestOk()
req.URL, _ = url.Parse("http://example.com/test?abc=hello%20world&a=b%3F")
return req
}