-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoperation_test.go
165 lines (135 loc) · 3.55 KB
/
operation_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package golax
import (
"fmt"
"testing"
)
func Test_Operation_Combo1(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Root.
Node("users").
Node("{user_id}").
Interceptor(&Interceptor{
Before: func(c *Context) {
fmt.Fprintln(c.Response, "Interceptor users/{user_id}", c.Parameters)
},
}).
Method("GET", func(c *Context) {
fmt.Fprintln(c.Response, "Method users/{user_id} ", c.Parameters["user_id"])
}).
Operation("list").
Interceptor(&Interceptor{
Before: func(c *Context) {
fmt.Fprintln(c.Response, "Interceptor users/{user_id}:list", c.Parameters)
},
}).
Method("GET", func(c *Context) {
fmt.Fprintln(c.Response, "Operation 'list' operation over ", c.Parameters["user_id"])
})
// Case 1
r := world.Request("GET", "/users/2").Do()
expected := `Interceptor users/{user_id} map[user_id:2]
Method users/{user_id} 2
`
if r.BodyString() != expected {
t.Error("Expected interceptor + method users")
}
// Case 2
r = world.Request("GET", "/users/2:list").Do()
expected = `Interceptor users/{user_id} map[user_id:2]
Interceptor users/{user_id}:list map[user_id:2]
Operation 'list' operation over 2
`
if r.BodyString() != expected {
t.Error("Expected interceptor (node) + interceptor (operation) + operation")
}
}
func Test_Operation_Combo2(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Root.
Node("users:good").
Method("GET", func(c *Context) {
fmt.Fprint(c.Response, "I am /users:good")
}).
Operation("list").
Method("GET", func(c *Context) {
fmt.Fprint(c.Response, "I am /users:good:list")
})
// Case 1
r := world.Request("GET", "/users:good").Do()
expected := `I am /users:good`
body := r.BodyString()
if body != expected {
t.Error("Expected interceptor + method users")
}
// Case 2
r = world.Request("GET", "/users:good:list").Do()
expected = `I am /users:good:list`
body = r.BodyString()
if body != expected {
t.Error("Expected interceptor (node) + interceptor (operation) + operation")
}
}
func Test_Operation_Combo3(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Root.
Interceptor(InterceptorError).
Node("users").
Interceptor(&Interceptor{
Before: func(c *Context) {
c.Error(999, "Unexpected invented error")
},
}).
Node("{user_id}").
Operation("list").
Method("GET", func(c *Context) {
fmt.Fprint(c.Response, "I am /users:good:list")
})
// Case 1
r := world.Request("GET", "/users/23:list").Do()
if 999 != r.StatusCode {
t.Error("Expected status code 999")
}
}
func Test_Operation_PathHandlers(t *testing.T) {
world := NewWorld()
defer world.Destroy()
PrintHandlers := func(c *Context) {
fmt.Fprint(c.Response, c.PathHandlers)
}
world.Api.Root.
Node("a").
Node("{b}").
Node("c:isNode").
Method("GET", PrintHandlers).
Node("{d}").
Method("GET", PrintHandlers).
Operation("myOperation").
Method("GET", PrintHandlers)
{ // Case 1
r := world.Request("GET", "/a/b:3/c:isNode/d:myOperation").Do()
body := r.BodyString()
expected := "/a/{b}/c:isNode/{d}:myOperation"
if body != expected {
t.Error("Expected:", expected, "Obtained:", body)
}
}
{ // Case 2
r := world.Request("GET", "/a/b:3/c:isNode").Do()
body := r.BodyString()
expected := "/a/{b}/c:isNode"
if body != expected {
t.Error("Expected:", expected, "Obtained:", body)
}
}
{ // Case 3
r := world.Request("GET", "/a/b/c:isNode/d:notOperation").Do()
body := r.BodyString()
expected := "/a/{b}/c:isNode/{d}"
if body != expected {
t.Error("Expected:", expected, "Obtained:", body)
}
}
}