-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess_logger_test.go
77 lines (72 loc) · 1.68 KB
/
access_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
package midfabs
import (
"bytes"
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
const errorString = "\nGot:\t%v\nWant:\t%v\n"
func TestWriteHeader(t *testing.T) {
cases := map[string]struct {
status int
want int
}{
"ok": {status: http.StatusOK, want: http.StatusOK},
"bad": {status: http.StatusBadRequest, want: http.StatusBadRequest},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
w := responseWriter{ResponseWriter: httptest.NewRecorder()}
w.WriteHeader(c.status)
if got := w.status; got != c.want {
t.Fatalf(errorString, got, c.want)
}
})
}
}
func TestAccessLogger(t *testing.T) {
defaultSubstrings := []string{
"status=200",
"duration=",
"method=GET",
"proto=HTTP/1.1",
"dest=/",
"src=",
"agent=",
}
base := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
var buf bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buf, nil))
cases := map[string]struct {
handler http.Handler
want string
}{
"no prefix": {
handler: AccessLogger(logger, "")(base),
want: "msg=",
},
"with prefix": {
handler: AccessLogger(logger, "foo")(base),
want: "msg=foo",
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
w := httptest.NewRecorder()
c.handler.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/", nil))
got := buf.String()
for i, sub := range defaultSubstrings {
if !strings.Contains(got, sub) {
t.Fatalf("Did not find wanted substring %s", defaultSubstrings[i])
}
if !strings.Contains(got, c.want) {
t.Fatalf("\nGot\t'%s'\n\tand could not find '%s'\n", got, c.want)
}
}
})
}
}