-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxaccess_test.go
108 lines (94 loc) · 2.88 KB
/
xaccess_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
// +build go1.7
package xaccess
import (
"bytes"
"net/http"
"net/http/httptest"
"runtime"
"testing"
"time"
"github.com/rs/xlog"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
)
type fakeContext struct {
err error
}
func (c fakeContext) Err() error {
return c.err
}
func (c fakeContext) Deadline() (deadline time.Time, ok bool) {
return time.Now(), true
}
func (c fakeContext) Done() <-chan struct{} {
return make(chan struct{})
}
func (c fakeContext) Value(key interface{}) interface{} {
return nil
}
type recorderOutput struct {
last map[string]interface{}
}
func (o *recorderOutput) Write(fields map[string]interface{}) (err error) {
o.last = map[string]interface{}{}
for k, v := range fields {
o.last[k] = v
}
return nil
}
func TestResponseStatus(t *testing.T) {
assert.Equal(t, "ok", responseStatus(fakeContext{err: nil}, http.StatusOK))
assert.Equal(t, "canceled", responseStatus(fakeContext{err: context.Canceled}, http.StatusOK))
assert.Equal(t, "timeout", responseStatus(fakeContext{err: context.DeadlineExceeded}, http.StatusOK))
assert.Equal(t, "error", responseStatus(fakeContext{err: nil}, http.StatusFound))
}
func TestNewHandler(t *testing.T) {
h := NewHandler()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusAccepted)
w.Write([]byte{'1', '2', '3'})
}))
o := &recorderOutput{}
l := xlog.NewHandler(xlog.Config{
Output: o,
})
r, _ := http.NewRequest("GET", "/path", nil)
w := httptest.NewRecorder()
l(h).ServeHTTP(w, r)
runtime.Gosched()
for i := 0; len(o.last) == 0 && i < 100; i++ {
time.Sleep(10 * time.Millisecond)
}
if assert.NotNil(t, o.last) {
assert.Equal(t, 3, o.last["size"])
assert.Equal(t, "info", o.last["level"])
assert.Equal(t, "access", o.last["type"])
assert.Equal(t, "ok", o.last["status"])
assert.Equal(t, 202, o.last["status_code"])
assert.Equal(t, "GET /path 202", o.last["message"])
assert.NotEmpty(t, o.last["duration"])
assert.NotEmpty(t, o.last["time"])
}
xx := string(bytes.Repeat([]byte{'x'}, 150))
r, _ = http.NewRequest("GET", "/"+xx, nil)
w = httptest.NewRecorder()
l(h).ServeHTTP(w, r)
runtime.Gosched()
for i := 0; len(o.last) == 0 && i < 100; i++ {
time.Sleep(10 * time.Millisecond)
}
if assert.NotNil(t, o.last) {
assert.Equal(t, "GET /"+xx[:48]+"..."+xx[:49]+" 202", o.last["message"])
}
}
func TestEllipsize(t *testing.T) {
assert.Equal(t, "", ellipsize("", 10))
assert.Equal(t, "s...g", ellipsize("somestring", 5))
assert.Equal(t, "somestring", ellipsize("somestring", 10))
assert.Equal(t, "som...ing", ellipsize("somestring", 9))
assert.Equal(t, "", ellipsize("somestring", 0))
assert.Equal(t, ".", ellipsize("somestring", 1))
assert.Equal(t, "..", ellipsize("somestring", 2))
assert.Equal(t, "...", ellipsize("somestring", 3))
assert.Equal(t, "s...g", ellipsize("somestring", 4))
assert.Equal(t, "s...g", ellipsize("somestring", 5))
}