-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctx_testkit.go
207 lines (162 loc) · 3.61 KB
/
ctx_testkit.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
201
202
203
204
205
206
207
package kit
import (
"mime/multipart"
"sync"
"github.com/clubpay/ronykit/kit/utils"
)
// TestContext is useful for writing end-to-end tests for your Contract handlers.
type TestContext struct {
ls localStore
handlers HandlerFuncChain
inMsg Message
inHdr EnvelopeHdr
clientIP string
expectFunc func(...*Envelope) error
}
func NewTestContext() *TestContext {
return &TestContext{
ls: localStore{
kv: map[string]any{},
},
}
}
func (testCtx *TestContext) SetHandler(h ...HandlerFunc) *TestContext {
testCtx.handlers = h
return testCtx
}
func (testCtx *TestContext) SetClientIP(ip string) *TestContext {
testCtx.clientIP = ip
return testCtx
}
func (testCtx *TestContext) Input(m Message, hdr EnvelopeHdr) *TestContext {
testCtx.inMsg = m
testCtx.inHdr = hdr
return testCtx
}
func (testCtx *TestContext) Receiver(f func(out ...*Envelope) error) *TestContext {
testCtx.expectFunc = f
return testCtx
}
func (testCtx *TestContext) Run(stream bool) error {
ctx := newContext(&testCtx.ls)
conn := newTestConn()
conn.clientIP = testCtx.clientIP
conn.stream = stream
ctx.conn = conn
ctx.in = newEnvelope(ctx, conn, false)
ctx.in.
SetMsg(testCtx.inMsg).
SetHdrMap(testCtx.inHdr)
ctx.handlers = append(ctx.handlers, testCtx.handlers...)
ctx.Next()
return testCtx.expectFunc(conn.out...)
}
func (testCtx *TestContext) RunREST() error {
ctx := newContext(&testCtx.ls)
conn := newTestRESTConn()
conn.clientIP = testCtx.clientIP
conn.stream = false
ctx.conn = conn
ctx.in = newEnvelope(ctx, conn, false)
ctx.in.
SetMsg(testCtx.inMsg).
SetHdrMap(testCtx.inHdr)
ctx.handlers = append(ctx.handlers, testCtx.handlers...)
ctx.Next()
return testCtx.expectFunc(conn.out...)
}
type testConn struct {
sync.Mutex
id uint64
clientIP string
stream bool
kv map[string]string
out []*Envelope
}
var _ Conn = (*testConn)(nil)
func newTestConn() *testConn {
return &testConn{
id: utils.RandomUint64(0),
}
}
func (t *testConn) ConnID() uint64 {
return t.id
}
func (t *testConn) ClientIP() string {
return t.clientIP
}
func (t *testConn) Write(_ []byte) (int, error) {
return 0, nil
}
func (t *testConn) WriteEnvelope(e *Envelope) error {
e.dontReuse()
t.Lock()
t.out = append(t.out, e)
t.Unlock()
return nil
}
func (t *testConn) Stream() bool {
return t.stream
}
func (t *testConn) Walk(f func(key string, val string) bool) {
t.Lock()
defer t.Unlock()
for k, v := range t.kv {
if !f(k, v) {
return
}
}
}
func (t *testConn) Get(key string) string {
t.Lock()
defer t.Unlock()
return t.kv[key]
}
func (t *testConn) Set(key string, val string) {
t.Lock()
t.kv[key] = val
t.Unlock()
}
func (t *testConn) Keys() []string {
keys := make([]string, 0, len(t.kv))
for k := range t.kv {
keys = append(keys, k)
}
return keys
}
type testRESTConn struct {
testConn
method string
path string
host string
requestURI string
statusCode int
}
var _ RESTConn = (*testRESTConn)(nil)
func newTestRESTConn() *testRESTConn {
return &testRESTConn{
testConn: testConn{
id: utils.RandomUint64(0),
},
}
}
func (t *testRESTConn) WalkQueryParams(f func(key string, val string) bool) {}
func (t *testRESTConn) GetHost() string {
return t.host
}
func (t *testRESTConn) GetRequestURI() string {
return t.requestURI
}
func (t *testRESTConn) GetMethod() string {
return t.method
}
func (t *testRESTConn) GetPath() string {
return t.path
}
func (t *testRESTConn) Form() (*multipart.Form, error) {
panic("not implemented")
}
func (t *testRESTConn) SetStatusCode(code int) {
t.statusCode = code
}
func (t *testRESTConn) Redirect(_ int, _ string) {}