-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrouter_test.go
554 lines (473 loc) · 12.7 KB
/
router_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package cookoo
import (
"testing"
)
// Mock resolver
type FakeRequestResolver struct {
BasicRequestResolver
}
// Always returns FOO.
func (self *FakeRequestResolver) Resolve(name string, cxt Context) (string, error) {
return "FOO", nil
}
type FakeErrorRequestResolver struct {
BasicRequestResolver
}
func (self *FakeErrorRequestResolver) Resolve(name string, cxt Context) (string, error) {
if name == "test" {
return "test", nil
}
return "test2", &RouteError{"Route not resolved."}
}
type CmdDefStub struct {
Name string `coo:"name"`
Age int `coo:"age"`
}
func (c *CmdDefStub) Run(cxt Context) (interface{}, Interrupt) {
if len(c.Name) == 0 {
return false, &FatalError{"Expected a name."}
}
return true, nil
}
// Test the resolver.
func TestResolver(t *testing.T) {
fakeCxt := new(ExecutionContext)
fakeCxt.Init()
registry := new(Registry)
registry.Init()
r := new(Router)
r.Init(registry)
// Canary: Check that resolver is working.
if a, _ := r.ResolveRequest("test", fakeCxt); a != "test" {
t.Error("Expected path to be 'test'")
}
// Set and get a resolver.
resolver := new(FakeRequestResolver)
r.SetRequestResolver(resolver)
resolver, ok := r.RequestResolver().(*FakeRequestResolver)
if !ok {
t.Error("! Resolver is not a FakeRequestResolver.")
}
// Make sure the new resolver works.
path, _ := r.ResolveRequest("test", fakeCxt)
if path != "FOO" {
t.Error("Expected path to be 'test'")
}
// Test errors on a resolver.
resolver2 := new(FakeErrorRequestResolver)
r.SetRequestResolver(resolver2)
resolver2, ok2 := r.RequestResolver().(*FakeErrorRequestResolver)
if !ok2 {
t.Error("! Resolver is not a FakeErrorRequestResolver.")
}
_, e := r.ResolveRequest("test2", fakeCxt)
if e == nil {
t.Error("! Resolver did not error when it should.")
}
e = r.HandleRequest("test2", fakeCxt, true)
if e == nil {
t.Error("! HandleRequest did not error when it should.")
}
// Testing errors on a resolver via a reroute.
registry.
Route("test", "A test route").
Does(RerouteCommand, "fake").
Using("route").WithDefault("test2").
Route("test2", "a test").
Does(AddToContext, "fake2").
Using("bar").WithDefault("baz")
e = r.HandleRequest("test", fakeCxt, true)
if e == nil {
t.Error("! HandleRequest did not error on a reroute.")
}
if fakeCxt.Get("route.Name", "foo").(string) != "test" {
t.Error("! Expected route name to be test.")
}
if fakeCxt.Get("route.Description", "").(string) != "A test route" {
t.Error("! Unexpected route description")
}
if fakeCxt.Get("route.RequestName", "").(string) != "test" {
t.Errorf("Expected raw route name to be test.")
}
if n := fakeCxt.Get("command.Name", "").(string); n != "fake" {
t.Errorf("Expected fake to be the last command run. Got %s", n)
}
}
func MockCommand(cxt Context, params *Params) (interface{}, Interrupt) {
//println("Mock command")
return true, nil
}
func RerouteCommand(cxt Context, params *Params) (interface{}, Interrupt) {
route := params.Get("route", "default").(string)
return nil, &Reroute{route}
}
func FetchParams(cxt Context, params *Params) (interface{}, Interrupt) {
return params, nil
}
func RecoverableErrorCommand(cxt Context, params *Params) (interface{}, Interrupt) {
return nil, &RecoverableError{"Blarg"}
}
func FatalErrorCommand(cxt Context, params *Params) (interface{}, Interrupt) {
return nil, &FatalError{"Blarg"}
}
func StopCommand(cxt Context, params *Params) (interface{}, Interrupt) {
return nil, &Stop{}
}
type MockDatasource struct {
RetVal string
}
func (ds *MockDatasource) Value(key string) interface{} {
return ds.RetVal
}
func TestParseFromStatement(t *testing.T) {
str := "foo:bar foo:baz blarg:urg"
res := parseFromStatement(str)
if len(res) != 3 {
t.Error("! Expected length 3, got ", len(res))
}
exp := res[0]
if exp.source != "foo" {
t.Error("! Expected foo, got ", exp.source)
}
if exp.key != "bar" {
t.Error("! Expected bar, got ", exp.source)
}
exp = res[1]
if exp.source != "foo" {
t.Error("! Expected foo, got ", exp.source)
}
if exp.key != "baz" {
t.Error("! Expected baz, got ", exp.source)
}
exp = res[2]
if exp.source != "blarg" {
t.Error("! Expected blarg, got ", exp.source)
}
if exp.key != "urg" {
t.Error("! Expected urg, got ", exp.source)
}
}
func TestParseFromVal(t *testing.T) {
fr := "test:foo"
r := parseFromVal(fr)
name := r.source
val := r.key
if name != "test" {
t.Error("Expected 'test', got ", name)
}
if val != "foo" {
t.Error("Expected 'foo', got ", val)
}
fr = "test"
r = parseFromVal(fr)
name = r.source
val = r.key
if name != "test" {
t.Error("Expected 'test', got ", name)
}
if val != "" {
t.Error("Expected an empty string, got ", val)
}
fr = "test:"
r = parseFromVal(fr)
name = r.source
val = r.key
if name != "test" {
t.Error("Expected 'test', got ", name)
}
if val != "" {
t.Error("Expected an empty string, got ", val)
}
fr = "test:foo:bar:baz"
r = parseFromVal(fr)
name = r.source
val = r.key
if name != "test" {
t.Error("Expected 'test', got ", name)
}
if val != "foo:bar:baz" {
t.Error("Expected 'foo:bar:baz' string, got ", val)
}
fr = ""
r = parseFromVal(fr)
name = r.source
val = r.key
if name != "" {
t.Error("Expected empty string, got ", name)
}
if val != "" {
t.Error("Expected an empty string string, got ", val)
}
}
func TestRouterSetRegistry(t *testing.T) {
reg, router, cxt := Cookoo()
reg2 := NewRegistry()
reg.Route("mock", "a test").
Does(AddToContext, "fake").
Using("foo").WithDefault("bar")
reg2.Route("foo", "a test").
Does(AddToContext, "fake2").
Using("bar").WithDefault("baz")
router.SetRegistry(reg2)
e := router.HandleRequest("mock", cxt, true)
if e == nil {
t.Error("! Router should have been set to one that does not handle request.")
}
}
func TestFromValues(t *testing.T) {
reg, router, cxt := Cookoo()
cxt.Put("test1", 1234)
cxt.AddDatasource("test2", "foo")
ds := new(MockDatasource)
ds.RetVal = "1234"
cxt.AddDatasource("foo", ds)
reg.AddRoute(Route{
Name: "mock",
Help: "Test from.",
Does: Tasks{
Cmd{
Name: "first",
Fn: FetchParams,
Using: []Param{
{Name: "test1", From: "cxt:test1"},
{Name: "test2", From: "datasource:test2"},
{Name: "test3", From: "foo:test3"},
{Name: "test4", From: "NONE:none", DefaultValue: "test4"},
{Name: "test5", From: "NONE:none foo:test3 cxt:test1", DefaultValue: "Z"},
{Name: "test6", From: "None:none"},
},
},
},
})
e := router.HandleRequest("mock", cxt, true)
if e != nil {
t.Error("Unexpected: ", e.Error())
}
params, ok := cxt.Get("first", nil).(*Params)
if !ok {
t.Error("! Expected a Params object.")
}
test1, ok := params.Has("test1")
if !ok {
t.Error("! Expected a value in cxt:test1")
}
if test1.(int) != 1234 {
t.Error("! Expected test1 to return 1234. Got ", test1)
}
test2, ok := params.Has("test2")
if !ok {
t.Error("! Expected a value in cxt:test1")
}
if test2.(string) != "foo" {
t.Error("! Expected test2 to return 'foo'. Got ", test2)
}
test3, ok := params.Has("test3")
if !ok {
t.Error("! Expected default value")
}
if test3.(string) != "1234" {
t.Error("! Expected test4 to return '1234'. Got ", test3)
}
test4, ok := params.Has("test4")
if !ok {
t.Error("! Expected default value")
}
if test4.(string) != "test4" {
t.Error("! Expected test4 to return 'test4'. Got ", test4)
}
// We expect that in this case the first match in the From clause
// will be returned, which is the value of foo:test3.
test5, ok := params.Has("test3")
if !ok {
t.Error("! Expected default value")
}
if test5.(string) != "1234" {
t.Error("! Expected test5 to return '1234'. Got ", test5)
}
param, ok := params.Has("test6")
// This expectation has changed. A nil value must be treated
// as a non-existing param. Otherwise there is no way to
// trigger defaults when no default is set on the chain itself.
//if !ok {
// t.Error("! Expected a *Param with a nil value")
//}
if param != nil {
t.Error("! Expected nil value")
}
}
func TestHandleRequest(t *testing.T) {
reg, router, context := Cookoo()
reg.
Route("TEST", "A test route").Does(MockCommand, "fake").
Route("@tainted", "Tainted route").Does(MockCommand, "fake2").
Route("Several", "Test multiple.").
Does(MockCommand, "first").
Does(MockCommand, "second").
Does(MockCommand, "third")
e := router.HandleRequest("TEST", context, true)
if e != nil {
t.Error("Unexpected: ", e.Error())
}
e = router.HandleRequest("@tainted", context, true)
if e == nil {
t.Error("Expected tainted route to not run protected name.")
}
if e.Error() != "Route is tainted. Refusing to run." {
t.Error("Expected RouteError to be a tainted one.")
}
e = router.HandleRequest("@tainted", context, false)
if e != nil {
t.Error("Unexpected: ", e.Error())
}
router.HandleRequest("NO Such Route", context, false)
context = NewContext()
router.HandleRequest("Several", context, false)
if context.Len() != 7 {
t.Errorf("! Expected three items in the context, got %d", context.Len())
}
e = router.HandleRequest("", context, true)
if e == nil {
t.Error("Expected empty route to give error.")
}
if e.Error() != "Empty route name." {
t.Error("Expected RouteError to be a empty route error.")
}
}
func TestHandleRequestCmdDef(t *testing.T) {
reg, router, context := Cookoo()
reg.AddRoutes(Route{
Name: "TEST",
Help: "A test route",
Does: Tasks{
Cmd{
Name: "fake",
Fn: MockCommand,
},
},
},
Route{
Name: "@tainted",
Help: "Tainted route",
Does: Tasks{
Cmd{Name: "fake", Fn: MockCommand},
},
},
Route{
Name: "Several",
Help: "Test multiple",
Does: Tasks{
Cmd{Name: "first", Fn: MockCommand},
Cmd{Name: "second", Fn: MockCommand},
Cmd{Name: "third", Fn: MockCommand},
},
},
Route{
Name: "CmdDef",
Help: "Test command defs.",
Does: Tasks{
CmdDef{
Name: "test",
Def: &CmdDefStub{},
Using: []Param{
{Name: "name", DefaultValue: "test"},
{Name: "age", DefaultValue: 5},
},
},
},
})
e := router.HandleRequest("TEST", context, true)
if e != nil {
t.Error("Unexpected: ", e.Error())
}
e = router.HandleRequest("@tainted", context, true)
if e == nil {
t.Error("Expected tainted route to not run protected name.")
}
if e.Error() != "Route is tainted. Refusing to run." {
t.Error("Expected RouteError to be a tainted one.")
}
e = router.HandleRequest("@tainted", context, false)
if e != nil {
t.Error("Unexpected: ", e.Error())
}
router.HandleRequest("NO Such Route", context, false)
context = NewContext()
router.HandleRequest("Several", context, false)
if context.Len() != 7 {
t.Errorf("! Expected three items in the context, got %d", context.Len())
}
e = router.HandleRequest("", context, true)
if e == nil {
t.Error("Expected empty route to give error.")
}
if e.Error() != "Empty route name." {
t.Error("Expected RouteError to be a empty route error.")
}
// Test command def
if e = router.HandleRequest("CmdDef", context, true); e != nil {
t.Error(e)
}
}
func TestReroute(t *testing.T) {
reg, router, context := Cookoo()
reg.
Route("TEST", "A test route").Does(RerouteCommand, "fake").
Using("route").WithDefault("TEST2").
Route("TEST2", "Tainted route").Does(FetchParams, "fake2").Using("foo").WithDefault("bar")
e := router.HandleRequest("TEST", context, false)
if e != nil {
t.Error("! Unexpected error executing TEST")
}
p := context.Get("fake2", nil)
if p == nil {
t.Error("! Expected data in fake2.")
}
}
func TestRecoverableError(t *testing.T) {
reg, router, context := Cookoo()
reg.
Route("TEST", "A test route").
Does(RecoverableErrorCommand, "fake").
Does(FetchParams, "fake2").Using("foo").WithDefault("bar")
e := router.HandleRequest("TEST", context, false)
if e != nil {
t.Error("! Unexpected error executing TEST")
}
p := context.Get("fake2", nil)
if p == nil {
t.Error("! Expected data in fake2.")
}
}
func TestFatalError(t *testing.T) {
reg, router, context := Cookoo()
reg.
Route("TEST", "A test route").
Does(FatalErrorCommand, "fake").
Does(FetchParams, "fake2").Using("foo").WithDefault("bar")
e := router.HandleRequest("TEST", context, false)
if e == nil {
t.Error("! Expected error executing TEST")
}
if e.Error() != "Blarg" {
t.Error("! Message from FatalError is incorrect.")
}
p := context.Get("fake2", nil)
if p != nil {
t.Error("! Expected fake2 to not get executed.")
}
}
func TestStop(t *testing.T) {
reg, router, context := Cookoo()
reg.
Route("TEST", "A test route").
Does(StopCommand, "fake").
Does(FetchParams, "fake2").Using("foo").WithDefault("bar")
e := router.HandleRequest("TEST", context, false)
if e != nil {
t.Error("! Unexpected error executing TEST")
}
p := context.Get("fake2", nil)
if p != nil {
t.Error("! Expected fake2 to not get executed.")
}
}