forked from bitly/oauth2_proxy
-
Notifications
You must be signed in to change notification settings - Fork 9
/
logging_handler_test.go
47 lines (38 loc) · 1.01 KB
/
logging_handler_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
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"regexp"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestLoggingHandler_ServeHTTP(t *testing.T) {
datestr := time.Now().Format(`02/Jan/2006:15:04`)
tests := []struct {
Format,
Expected string
}{
{defaultRequestLoggingFormat,
`127.0.0.1 - - \[` + datestr + `:\d{2} [+-]\d{4}\] test-server GET - "/foo/bar" HTTP/1.1 "" 200 4 0.\d{3}` + "\n"},
{"{{.RequestMethod}}", "GET\n"},
}
for _, test := range tests {
buf := bytes.NewBuffer(nil)
handler := func(w http.ResponseWriter, req *http.Request) {
_, ok := w.(http.Hijacker)
if !ok {
t.Error("http.Hijacker is not available")
}
w.Write([]byte("test"))
}
h := LoggingHandler(buf, http.HandlerFunc(handler), "", test.Format)
r, _ := http.NewRequest("GET", "/foo/bar", nil)
r.RemoteAddr = "127.0.0.1"
r.Host = "test-server"
h.ServeHTTP(httptest.NewRecorder(), r)
re := regexp.MustCompile(test.Expected)
assert.Regexp(t, re, buf.String())
}
}