-
Notifications
You must be signed in to change notification settings - Fork 43
/
ctx_test.go
88 lines (68 loc) · 1.59 KB
/
ctx_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
package neo
import (
"errors"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMakeCtx(t *testing.T) {
req := makeTestHttpRequest(nil)
w := httptest.NewRecorder()
ctx := makeCtx(req, w)
assert.NotNil(t, ctx)
}
func TestAddError(t *testing.T) {
req := makeTestHttpRequest(nil)
w := httptest.NewRecorder()
ctx := makeCtx(req, w)
ctx.Error(errors.New("my error 1"))
ctx.Error(errors.New("my error 2"))
assert.Equal(t, 2, len(ctx.Errors))
assert.True(t, ctx.HasErrors())
messages := []string{"my error 1", "my error 2"}
for i, m := range ctx.Errors {
assert.Equal(t, messages[i], m.Error())
}
}
func TestCtxReqRes(t *testing.T) {
req := makeTestHttpRequest(nil)
w := httptest.NewRecorder()
ctx := makeCtx(req, w)
assert.NotNil(t, ctx.Req)
assert.NotNil(t, ctx.Req.URL)
assert.NotNil(t, ctx.Res)
assert.Equal(t, 404, ctx.Res.Status)
}
func TestCtxData(t *testing.T) {
req := makeTestHttpRequest(nil)
w := httptest.NewRecorder()
ctx := makeCtx(req, w)
assert.Nil(t, ctx.Data.Get("data"))
ctx.Data.Set("data", "value")
assert.Equal(t, "value", ctx.Data.Get("data"))
ctx.Data.Del("data")
assert.Nil(t, ctx.Data.Get("data"))
}
func TestCtxOnEmit(t *testing.T) {
req := makeTestHttpRequest(nil)
w := httptest.NewRecorder()
ctx := makeCtx(req, w)
channel := make(chan int)
ok := false
ctx.On("event", func(data interface{}) {
counter := data.(int)
if counter == 10 {
ok = true
}
channel <- 1
})
ctx.Emit("event", 5)
// wait
<-channel
assert.False(t, ok)
ok = false
ctx.Emit("event", 10)
// wait
<-channel
assert.True(t, ok)
}