-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttplog_test.go
151 lines (134 loc) · 2.9 KB
/
httplog_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
package httplog_test
import (
"bytes"
"net/http/httptest"
"strings"
"testing"
"github.com/gkats/httplog"
)
type testWriter struct {
Stream string
}
func (w *testWriter) Write(p []byte) (n int, err error) {
n = len(p)
buf := bytes.NewBuffer([]byte(w.Stream))
buf.Write(p)
w.Stream = buf.String()
return
}
func (w *testWriter) Flush() {
w.Stream = ""
}
func NewTestWriter() *testWriter {
return &testWriter{Stream: ""}
}
func TestLog(t *testing.T) {
w := NewTestWriter()
l := httplog.New(w)
l.Log()
tests := []string{
"level=I",
"time=",
"ip=",
"method=",
"path=",
"ua=",
"status=",
"params=",
"\n",
}
count := 0
for i, want := range tests {
count = strings.Count(w.Stream, want)
if count != 1 {
t.Errorf("(%v) Expected %v to contain '%v' exactly once, got %v", i, w.Stream, want, count)
}
count = 0
}
// Test that it appends to the stream
l.Log()
for i, want := range tests {
count = strings.Count(w.Stream, want)
if count != 2 {
t.Errorf("(%v) Expected %v to contain %v exactly twice, got %v", i, w.Stream, want, count)
}
count = 0
}
}
func TestSetStatus(t *testing.T) {
w := NewTestWriter()
l := httplog.New(w)
l.SetStatus(200)
l.Log()
want := "status=200"
if !strings.Contains(w.Stream, want) {
t.Errorf("Expected %v to include %v", w.Stream, want)
}
}
func TestSetRequestInfo(t *testing.T) {
w := NewTestWriter()
l := httplog.New(w)
r := httptest.NewRequest("POST", "https://example.com/resources", strings.NewReader("{\"foo\": \"bar\"}"))
r.Header.Set("User-Agent", "request-ua")
l.SetRequestInfo(r)
l.Log()
tests := []string{
"ip=" + strings.Split(r.RemoteAddr, ":")[0],
"method=POST",
"path=https://example.com/resources",
"ua=request-ua",
"params={\"foo\": \"bar\"}",
}
for i, want := range tests {
if !strings.Contains(w.Stream, want) {
t.Errorf("(%v) Expected %v to contain '%v'", i, w.Stream, want)
}
}
w.Flush()
forwardedIP := "127.0.0.1"
r = httptest.NewRequest("GET", "https://example.com/resources?foo=bar", nil)
r.Header.Set("User-Agent", "request-ua")
r.Header.Set("X-Forwarded-For", forwardedIP)
l.SetRequestInfo(r)
l.Log()
tests = []string{
"ip=" + forwardedIP,
"method=GET",
"path=https://example.com/resources",
"ua=request-ua",
"params={\"foo\": \"bar\"}",
}
for i, want := range tests {
if !strings.Contains(w.Stream, want) {
t.Errorf("(%v) Expected %v to contain '%v'", i, w.Stream, want)
}
}
}
func TestLogExtras(t *testing.T) {
w := NewTestWriter()
l := httplog.New(w)
l.Add("uid", 1234)
l.Add("secret", "shhh!")
l.Log()
tests := []string{
"level=I",
"time=",
"ip=",
"method=",
"path=",
"ua=",
"status=",
"params=",
"uid=1234",
"secret=shhh!",
"\n",
}
count := 0
for i, want := range tests {
count = strings.Count(w.Stream, want)
if count != 1 {
t.Errorf("(%v) Expected %v to contain '%v' exactly once, got %v", i, w.Stream, want, count)
}
count = 0
}
}