-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
56 lines (52 loc) · 1.7 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
package kivikd
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"regexp"
"testing"
"github.com/go-kivik/kivikd/v4/auth"
"github.com/go-kivik/kivikd/v4/authdb"
"github.com/go-kivik/kivikd/v4/logger"
)
func TestLogger(t *testing.T) {
buf := &bytes.Buffer{}
l := logger.New(buf)
mw := loggerMiddleware(l)
req := httptest.NewRequest("GET", "/foo", nil)
session := &auth.Session{User: &authdb.UserContext{Name: "bob"}}
ctx := context.WithValue(req.Context(), SessionKey, &session)
req = req.WithContext(ctx)
w := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte("Go away!"))
})
mw(handler).ServeHTTP(w, req)
expectedRE := regexp.MustCompile(`^192\.0\.2\.1 bob \[\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d.+] ` +
`\([\d.]+[µn]s\) "GET /foo HTTP/1.1" 401 8 "" ""`)
if !expectedRE.Match(buf.Bytes()) {
t.Errorf("Log does not match. Got:\n%s\n", buf.String())
}
}
func TestLoggerNoAuth(t *testing.T) {
buf := &bytes.Buffer{}
l := logger.New(buf)
mw := loggerMiddleware(l)
req := httptest.NewRequest("GET", "/foo", nil)
session := &auth.Session{}
ctx := context.WithValue(req.Context(), SessionKey, &session)
req = req.WithContext(ctx)
w := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte("Go away!"))
})
mw(handler).ServeHTTP(w, req)
expectedRE := regexp.MustCompile(`^192\.0\.2\.1 \[\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d.+] ` +
`\([\d.]+[µn]s\) "GET /foo HTTP/1.1" 401 8 "" ""`)
if !expectedRE.Match(buf.Bytes()) {
t.Errorf("Log does not match. Got:\n%s\n", buf.String())
}
}