-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogHandler_test.go
147 lines (136 loc) · 4.01 KB
/
logHandler_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
package main
import (
"bytes"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type loggerTest struct {
name string
method string
URL string
tests func (t *testing.T, logs *bytes.Buffer, recorder *httptest.ResponseRecorder)
}
func runLoggerTests(t *testing.T, handler http.Handler, tests []loggerTest) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r, err := http.NewRequest(test.method, test.URL, nil)
if err != nil {
t.Errorf("expected no error got %v", err)
}
w := httptest.NewRecorder()
l := log.Writer()
buf := &bytes.Buffer{}
log.SetOutput(buf)
defer log.SetOutput(l)
handler.ServeHTTP(w, r)
test.tests(t, buf, w)
})
}
}
func TestDeactivatedLog(t *testing.T) {
tempDir := setupFS()
defer cleanTempDir(tempDir)
h := LogAccess(false, "", http.FileServer(justFilesFilesystem{http.Dir(tempDir)}))
tests := []loggerTest{
{
name: "Index html redirect",
method: "GET",
URL: "/index.html",
tests: func (t *testing.T, logs *bytes.Buffer, rec *httptest.ResponseRecorder) {
if rec.Code != http.StatusMovedPermanently {
t.Fatalf("Expected %v but got %v", http.StatusMovedPermanently, rec.Code)
}
location := rec.Header().Get("Location")
if location != "./" {
t.Fatalf("Expected redirect %v but got %v", "./", location)
}
expectedLog := ""
logStr := logs.String()
if logStr != expectedLog {
t.Fatalf("Expected %v but got %v", expectedLog, logStr)
}
},
},
{
name: "File serving should still work as expected",
method: "GET",
URL: "/test.txt",
tests: func (t *testing.T, logs *bytes.Buffer, rec *httptest.ResponseRecorder) {
if rec.Code != http.StatusOK {
t.Fatalf("Expected %v but got %v", http.StatusOK, rec.Code)
}
shouldContain := "hello go"
body := string(rec.Body.Bytes())
if !strings.Contains(body, shouldContain) {
t.Fatalf("%v should contain %v", body, shouldContain)
}
mimeText := "text/plain; charset=utf-8"
location := rec.Header().Get("Content-type")
if location != mimeText {
t.Fatalf("Expected mime type %v but got %v", mimeText, location)
}
expectedLog := ""
logStr := logs.String()
if logStr != expectedLog {
t.Fatalf("Expected %v but got %v", expectedLog, logStr)
}
},
},
}
runLoggerTests(t, h, tests)
}
func TestWithLogging(t *testing.T) {
tempDir := setupFS()
defer cleanTempDir(tempDir)
h := LogAccess(true, "", http.FileServer(justFilesFilesystem{http.Dir(tempDir)}))
tests := []loggerTest{
{
name: "Index html redirect",
method: "GET",
URL: "/index.html",
tests: func (t *testing.T, logs *bytes.Buffer, rec *httptest.ResponseRecorder) {
if rec.Code != http.StatusMovedPermanently {
t.Fatalf("Expected %v but got %v", http.StatusMovedPermanently, rec.Code)
}
location := rec.Header().Get("Location")
if location != "./" {
t.Fatalf("Expected redirect %v but got %v", "./", location)
}
shouldContain := "301 0 /index.html"
logStr := logs.String()
if !strings.Contains(logStr, shouldContain) {
t.Fatalf("%v should contain %v", logStr, shouldContain)
}
},
},
{
name: "File serving should still work as expected",
method: "GET",
URL: "/test.txt",
tests: func (t *testing.T, logs *bytes.Buffer, rec *httptest.ResponseRecorder) {
if rec.Code != http.StatusOK {
t.Fatalf("Expected %v but got %v", http.StatusOK, rec.Code)
}
shouldContain := "hello go"
body := string(rec.Body.Bytes())
if !strings.Contains(body, shouldContain) {
t.Fatalf("%v should contain %v", body, shouldContain)
}
mimeText := "text/plain; charset=utf-8"
location := rec.Header().Get("Content-type")
if location != mimeText {
t.Fatalf("Expected mime type %v but got %v", mimeText, location)
}
shouldContain = "200 8 /test.txt"
logStr := logs.String()
if !strings.Contains(logStr, shouldContain) {
t.Fatalf("%v should contain %v", logStr, shouldContain)
}
},
},
}
runLoggerTests(t, h, tests)
}