-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmux_test.go
95 lines (82 loc) · 2.92 KB
/
mux_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
// Copyright (c) 2022, R.I. Pienaar and the Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package asyncjobs
import (
"context"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Router", func() {
Describe("ExternalProcess", func() {
var (
task *Task
err error
router *Mux
)
BeforeEach(func() {
task, err = NewTask("email:new", nil)
Expect(err).ToNot(HaveOccurred())
router = NewTaskRouter()
})
It("Should handle missing commands", func() {
Expect(router.ExternalProcess("email:new", "testdata/missing.sh")).ToNot(HaveOccurred())
handler := router.Handler(task)
_, err = handler(context.Background(), &defaultLogger{}, task)
Expect(err).To(MatchError(ErrExternalCommandNotFound))
})
It("Should handle command failures", func() {
Expect(router.ExternalProcess("email:new", "testdata/failing-handler.sh")).ToNot(HaveOccurred())
handler := router.Handler(task)
_, err = handler(context.Background(), &defaultLogger{}, task)
Expect(err).To(MatchError(ErrExternalCommandFailed))
})
It("Should handle success", func() {
Expect(router.ExternalProcess("email:new", "testdata/passing-handler.sh")).ToNot(HaveOccurred())
handler := router.Handler(task)
payload, err := handler(context.Background(), &defaultLogger{}, task)
Expect(err).ToNot(HaveOccurred())
Expect(payload).To(Equal("success\n"))
})
})
Describe("Handler", func() {
It("Should support default handler", func() {
router := NewTaskRouter()
router.HandleFunc("x", func(_ context.Context, _ Logger, _ *Task) (any, error) {
return "x", nil
})
task, err := NewTask("y", nil)
Expect(err).ToNot(HaveOccurred())
handler := router.Handler(task)
_, err = handler(nil, &defaultLogger{}, task)
Expect(err).To(MatchError(ErrNoHandlerForTaskType))
})
It("Should find the correct handler", func() {
router := NewTaskRouter()
router.HandleFunc("", func(_ context.Context, _ Logger, _ *Task) (any, error) {
return "custom default", nil
})
router.HandleFunc("things:", func(_ context.Context, _ Logger, _ *Task) (any, error) {
return "things:", nil
})
router.HandleFunc("things:very:specific", func(_ context.Context, _ Logger, _ *Task) (any, error) {
return "things:very:specific", nil
})
router.HandleFunc("things:specific", func(_ context.Context, _ Logger, _ *Task) (any, error) {
return "things:specific", nil
})
check := func(ttype string, expected string) {
task := &Task{Type: ttype}
res, err := router.Handler(task)(context.Background(), &defaultLogger{}, task)
Expect(err).ToNot(HaveOccurred())
Expect(res).To(Equal(expected))
}
check("things:other", "things:")
check("things:very:other", "things:")
check("things:very:specific", "things:very:specific")
check("things:specific", "things:specific")
check("things:specific:other", "things:specific")
check("x", "custom default")
})
})
})