forked from rcrowley/go-tigertonic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_test.go
135 lines (129 loc) · 3.6 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
package tigertonic
import (
"bytes"
"encoding/base64"
"fmt"
"log"
"net/http"
"net/url"
"regexp"
"strings"
"testing"
)
func TestApacheLogger(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
r.Header.Set("Authorization", fmt.Sprintf(
"Basic %s",
base64.StdEncoding.EncodeToString([]byte("rcrowley:password")),
))
r.Header.Set("Referer", "http://example.com/")
r.Header.Set("User-Agent", "Tiger Tonic tests")
r.RemoteAddr = "127.0.0.1:48879"
r.RequestURI = "/foo"
logger := ApacheLogged(Marshaled(func(u *url.URL, h http.Header, _ interface{}) (int, http.Header, *testResponse, error) {
return http.StatusOK, nil, &testResponse{"bar"}, nil
}))
b := &bytes.Buffer{}
logger.Logger = log.New(b, "", 0)
logger.ServeHTTP(w, r)
s := b.String()
if ok, _ := regexp.MatchString(`^127\.0\.0\.1 - rcrowley \[\d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2} [+-]\d{4}\] "GET /foo HTTP/1.1" 200 14 "http://example.com/" "Tiger Tonic tests"\n$`, s); !ok {
t.Fatal(s)
}
}
// Test that ApacheLogger always calls WriteHeader.
func TestApacheLoggerWriteHeader(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
logger := ApacheLogged(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Hi", "hi")
w.Write([]byte("hi\n"))
}))
logger.Logger = log.New(&bytes.Buffer{}, "", 0)
logger.ServeHTTP(w, r)
if !w.WroteHeader {
t.Fatal("didn't call WriteHeader")
}
if "hi\n" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func TestLogger(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest(
"POST",
"http://example.com/foo?bar=baz",
bytes.NewBufferString(`{"foo":"bar"}`),
)
r.Header.Set("Accept", "application/json")
r.Header.Set("Content-Type", "application/json")
logger := Logged(Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusOK, nil, &testResponse{"bar"}, nil
}), nil)
b := &bytes.Buffer{}
logger.Logger = log.New(b, "", 0)
logger.ServeHTTP(w, r)
s := b.String()
requestID := s[:16]
if fmt.Sprintf(
`%s > POST /foo?bar=baz HTTP/1.1
%s > Accept: application/json
%s > Content-Type: application/json
%s >
%s > {"foo":"bar"}
%s < HTTP/1.1 200 OK
%s < Content-Type: application/json
%s <
%s < {"foo":"bar"}
`,
requestID,
requestID,
requestID,
requestID,
requestID,
requestID,
requestID,
requestID,
requestID,
) != s {
t.Fatal(s)
}
}
// Test that Logger always calls WriteHeader.
func TestLoggerWriteHeader(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
logger := Logged(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Hi", "hi")
w.Write([]byte("hi\n"))
}), nil)
logger.Logger = log.New(&bytes.Buffer{}, "", 0)
logger.ServeHTTP(w, r)
if !w.WroteHeader {
t.Fatal("didn't call WriteHeader")
}
if "hi\n" != w.Body.String() {
t.Fatal(w.Body.String())
}
}
func TestRedactor(t *testing.T) {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
r.Header.Set("Accept", "application/json")
logger := Logged(Marshaled(func(u *url.URL, h http.Header, rq *testRequest) (int, http.Header, *testResponse, error) {
return http.StatusOK, nil, &testResponse{"SECRET"}, nil
}), func(s string) string {
return strings.Replace(s, "SECRET", "REDACTED", -1)
})
b := &bytes.Buffer{}
logger.Logger = log.New(b, "", 0)
logger.ServeHTTP(w, r)
s := b.String()
if strings.Contains(s, "SECRET") {
t.Fatal(s)
}
if !strings.Contains(s, "REDACTED") {
t.Fatal(s)
}
}