-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinterceptordeep_test.go
106 lines (91 loc) · 2.3 KB
/
interceptordeep_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
package golax
import (
"testing"
)
func Test_InterceptorDeep_OK(t *testing.T) {
world := NewWorld()
defer world.Destroy()
Print := func(c *Context) {
value, exist := c.Get("list")
if exist {
list := value.(string)
c.Response.Write([]byte(list))
}
}
failS := ""
Append := func(s string) *Interceptor {
return &Interceptor{
Before: func(c *Context) {
if s == failS {
c.Error(999, "Fail cause: "+failS)
Print(c)
return
}
list := ""
if value, exist := c.Get("list"); exist {
list = value.(string)
}
c.Set("list", list+s)
},
}
}
root := world.Api.Root
root.Interceptor(Append("[root"))
root.InterceptorDeep(Append("]"))
a := root.Node("a")
a.Interceptor(Append(",a_i1"))
a.Interceptor(Append(",a_i2"))
a.InterceptorDeep(Append(",a_d1"))
a.InterceptorDeep(Append(",a_d2"))
{
b := a.Node("b")
b.Interceptor(Append(",b_i1"))
b.Interceptor(Append(",b_i2"))
b.InterceptorDeep(Append(",b_d1"))
b.InterceptorDeep(Append(",b_d2"))
b.Method("GET", Print)
}
grandma := root.Node("grandma")
grandma.Interceptor(Append(",grandma"))
{
mary := grandma.Node("mary")
mary.Interceptor(Append(",Mary"))
mary.Method("GET", Print)
}
// Test1
res1 := world.Request("GET", "/grandma/mary").Do()
body1 := res1.BodyString()
//fmt.Println(body1)
if "[root,grandma,Mary]" != body1 {
t.Error("DeepInterceptor should be executed at the end")
}
// Test2
res2 := world.Request("GET", "/a/b").Do()
body2 := res2.BodyString()
//fmt.Println(body2)
if "[root,a_i1,a_i2,b_i1,b_i2,b_d2,b_d1,a_d2,a_d1]" != body2 {
t.Error("DeepInterceptor are executed in reverse order")
}
// Test3
cases := map[string]string{
"[root": "",
",a_i1": "[root",
",a_i2": "[root,a_i1",
",b_i1": "[root,a_i1,a_i2",
",b_i2": "[root,a_i1,a_i2,b_i1",
",b_d2": "[root,a_i1,a_i2,b_i1,b_i2",
",b_d1": "[root,a_i1,a_i2,b_i1,b_i2,b_d2",
",a_d2": "[root,a_i1,a_i2,b_i1,b_i2,b_d2,b_d1",
",a_d1": "[root,a_i1,a_i2,b_i1,b_i2,b_d2,b_d1,a_d2",
"]": "[root,a_i1,a_i2,b_i1,b_i2,b_d2,b_d1,a_d2,a_d1",
"": "[root,a_i1,a_i2,b_i1,b_i2,b_d2,b_d1,a_d2,a_d1]",
}
for s, expected := range cases {
failS = s
res := world.Request("GET", "/a/b").Do()
body := res.BodyString()
if expected != body {
t.Error("s:", s, "Expected:", expected, "Obtained:", body)
}
}
}