-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontextlogger_test.go
68 lines (59 loc) · 1.89 KB
/
contextlogger_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
package accesslog_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"sort"
"strings"
"testing"
"github.com/mash/go-accesslog"
)
type contextLogger struct {
buf string
}
func (l *contextLogger) Log(record accesslog.LogRecord) {
fields := make([]string, 0)
fields = append(fields, "method:"+record.Method)
fields = append(fields, "uri:"+record.Uri)
fields = append(fields, "protocol:"+record.Protocol)
fields = append(fields, "username:"+record.Username)
fields = append(fields, "host:"+record.Host)
fields = append(fields, "status:"+fmt.Sprintf("%d", record.Status))
// Sort the custom records to get a deterministic test result
keys := []string{}
for k, _ := range record.CustomRecords {
keys = append(keys, k)
}
sort.Strings(keys)
customRecords := []string{}
for _, key := range keys {
customRecords = append(customRecords, fmt.Sprintf("%s:%s", key, record.CustomRecords[key]))
}
fields = append(fields, "customRecords:"+fmt.Sprintf("map%v", customRecords))
l.buf += strings.Join(fields, ",")
l.buf += "\n"
}
func (l *contextLogger) LogContext(ctx context.Context, r accesslog.LogRecord) {
if r.CustomRecords == nil {
r.CustomRecords = map[string]string{}
}
r.CustomRecords["x-user-id"] = ctx.Value("x-user-id").(string)
l.Log(r)
}
func TestContextLogger(t *testing.T) {
logger := &contextLogger{}
loggingHandler := accesslog.NewLoggingHandler(http.HandlerFunc(
func(w http.ResponseWriter, req *http.Request) {
ctx := context.WithValue(req.Context(), "x-user-id", "1")
*req = *req.WithContext(ctx)
w.Write([]byte(`ok`))
}), logger)
writer := httptest.NewRecorder()
loggingHandler.ServeHTTP(writer, httptest.NewRequest("GET", "/", nil))
expected := "method:GET,uri:/,protocol:HTTP/1.1,username:-,host:example.com,status:200,customRecords:map[x-user-id:1]\n"
output := logger.buf
if output != expected {
t.Errorf("expected %s but got %s", expected, output)
}
}