forked from fatih/kite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethod_test.go
200 lines (154 loc) · 5.16 KB
/
method_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package kite
import (
"errors"
"testing"
"time"
)
func TestMethod_Latest(t *testing.T) {
k := New("testkite", "0.0.1")
k.Config.DisableAuthentication = true
k.Config.Port = 9997
k.MethodHandling = ReturnLatest
k.PreHandleFunc(func(r *Request) (interface{}, error) { return nil, nil })
k.PreHandleFunc(func(r *Request) (interface{}, error) { return "hello", nil })
// the following shouldn't do anything because the previous error breaks the chain
k.HandleFunc("foo", func(r *Request) (interface{}, error) {
return "handle", nil
})
k.PostHandleFunc(func(r *Request) (interface{}, error) { return "post1", nil })
k.PostHandleFunc(func(r *Request) (interface{}, error) { return "post2", nil })
go k.Run()
defer k.Close()
<-k.ServerReadyNotify()
c := New("exp", "0.0.1").NewClient("http://127.0.0.1:9997/kite")
if err := c.Dial(); err != nil {
t.Fatal(err)
}
result, err := c.TellWithTimeout("foo", 4*time.Second)
if err != nil {
t.Fatal(err)
}
if result.MustString() != "post2" {
t.Errorf("Latest repsonse should be post2, got %s", result.MustString())
}
}
func TestMethod_First(t *testing.T) {
k := New("testkite", "0.0.1")
k.SetLogLevel(DEBUG)
k.Config.DisableAuthentication = true
k.Config.Port = 9998
k.MethodHandling = ReturnFirst
k.PreHandleFunc(func(r *Request) (interface{}, error) { return nil, nil })
k.PreHandleFunc(func(r *Request) (interface{}, error) { return "hello", nil })
// the following shouldn't do anything because the previous error breaks the chain
k.HandleFunc("foo", func(r *Request) (interface{}, error) {
return "handle", nil
})
k.PostHandleFunc(func(r *Request) (interface{}, error) { return "post1", nil })
k.PostHandleFunc(func(r *Request) (interface{}, error) { return "post2", nil })
go k.Run()
defer k.Close()
<-k.ServerReadyNotify()
c := New("exp", "0.0.1").NewClient("http://127.0.0.1:9998/kite")
if err := c.Dial(); err != nil {
t.Fatal(err)
}
result, err := c.TellWithTimeout("foo", 4*time.Second)
if err != nil {
t.Fatal(err)
}
if result.MustString() != "hello" {
t.Errorf("Latest repsonse should be hello, got %s", result.MustString())
}
}
func TestMethod_Error(t *testing.T) {
k := New("testkite", "0.0.1")
k.Config.DisableAuthentication = true
k.Config.Port = 9999
var testError = errors.New("an error")
k.PreHandleFunc(func(r *Request) (interface{}, error) { return nil, testError })
// the following shouldn't do anything because the previous error breaks the chain
k.HandleFunc("foo", func(r *Request) (interface{}, error) {
return "handle", nil
})
k.PostHandleFunc(func(r *Request) (interface{}, error) { return "post1", nil })
k.PostHandleFunc(func(r *Request) (interface{}, error) { return "post2", nil })
go k.Run()
defer k.Close()
<-k.ServerReadyNotify()
c := New("exp", "0.0.1").NewClient("http://127.0.0.1:9999/kite")
if err := c.Dial(); err != nil {
t.Fatal(err)
}
_, err := c.TellWithTimeout("foo", 4*time.Second)
if err == nil {
t.Fatal("PreHandle returns an error, however error is non-nil.")
}
if err.Error() != testError.Error() {
t.Errorf("Error should be '%v', got '%v'", testError, err)
}
}
func TestMethod_Base(t *testing.T) {
k := New("testkite", "0.0.1")
k.Config.DisableAuthentication = true
k.Config.Port = 10000
k.PreHandleFunc(func(r *Request) (interface{}, error) {
r.Context.Set("pre1", "pre1")
return nil, nil
})
k.PreHandleFunc(func(r *Request) (interface{}, error) {
res, _ := r.Context.Get("pre1")
if res != "pre1" {
t.Errorf("Context response from previous pre handler should be pre1, got: %v", res)
}
r.Context.Set("pre2", "pre2")
return nil, nil
})
k.HandleFunc("foo", func(r *Request) (interface{}, error) {
res, _ := r.Context.Get("funcPre1")
if res != "funcPre1" {
t.Errorf("Context response from previous pre handler should be funcPre1, got: %v", res)
}
r.Context.Set("handle", "handle")
return "main-response", nil
}).PreHandleFunc(func(r *Request) (interface{}, error) {
r.Context.Set("funcPre1", "funcPre1")
return "funcPre1", nil
}).PostHandleFunc(func(r *Request) (interface{}, error) {
res, _ := r.Context.Get("handle")
if res != "handle" {
t.Errorf("Context response from previous pre handler should be handle, got: %v", res)
}
r.Context.Set("funcPost1", "funcPost1")
return "funcPost1", nil
})
k.PostHandleFunc(func(r *Request) (interface{}, error) {
res, _ := r.Context.Get("funcPost1")
if res != "funcPost1" {
t.Errorf("Context response from previous pre handler should be funcPost1, got: %v", res)
}
r.Context.Set("post1", "post1")
return "post1", nil
})
k.PostHandleFunc(func(r *Request) (interface{}, error) {
res, _ := r.Context.Get("post1")
if res != "post1" {
t.Errorf("Context response from previous pre handler should be post1, got: %v", res)
}
return "post2", nil
})
go k.Run()
defer k.Close()
<-k.ServerReadyNotify()
c := New("exp", "0.0.1").NewClient("http://127.0.0.1:10000/kite")
if err := c.Dial(); err != nil {
t.Fatal(err)
}
result, err := c.TellWithTimeout("foo", 4*time.Second)
if err != nil {
t.Fatal(err)
}
if result.MustString() != "main-response" {
t.Errorf("Latest repsonse should be main-response, got %s", result.MustString())
}
}