-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcore_test.go
116 lines (85 loc) · 2.27 KB
/
core_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
package ucon
import "testing"
func TestMiddleware(t *testing.T) {
DefaultMux = NewServeMux()
if v := len(DefaultMux.middlewares); v != 0 {
t.Fatalf("unexpected: %v", v)
}
Middleware(func(b *Bubble) error {
return nil
})
if v := len(DefaultMux.middlewares); v != 1 {
t.Fatalf("unexpected: %v", v)
}
}
type TargetOfHandlersScannerPlugin struct {
}
func (obj *TargetOfHandlersScannerPlugin) HandlersScannerProcess(m *ServeMux, rds []*RouteDefinition) error {
m.HandleFunc("GET", "/api/test/{id}", func() {})
return nil
}
func TestPluginWithPluginContainer(t *testing.T) {
DefaultMux = NewServeMux()
if v := len(DefaultMux.plugins); v != 0 {
t.Fatalf("unexpected: %v", v)
}
Plugin(&pluginContainer{
base: &TargetOfHandlersScannerPlugin{},
})
if v := len(DefaultMux.plugins); v != 1 {
t.Fatalf("unexpected: %v", v)
}
}
func TestPluginWithoutPluginContainer(t *testing.T) {
DefaultMux = NewServeMux()
if v := len(DefaultMux.plugins); v != 0 {
t.Fatalf("unexpected: %v", v)
}
Plugin(&TargetOfHandlersScannerPlugin{})
if v := len(DefaultMux.plugins); v != 1 {
t.Fatalf("unexpected: %v", v)
}
}
func TestPrepare(t *testing.T) {
DefaultMux = NewServeMux()
Plugin(&TargetOfHandlersScannerPlugin{})
if v := len(DefaultMux.router.handlers); v != 0 {
t.Fatalf("unexpected: %v", v)
}
DefaultMux.Prepare()
if v := len(DefaultMux.router.handlers); v != 1 {
t.Fatalf("unexpected: %v", v)
}
}
func TestHandle(t *testing.T) {
DefaultMux = NewServeMux()
if v := len(DefaultMux.router.handlers); v != 0 {
t.Fatalf("unexpected: %v", v)
}
Handle("GET", "/api/test", &handlerContainerImpl{
handler: func() {},
Context: background,
})
HandleFunc("GET", "/api/test/{id}", func() {})
HandleFunc("PUT", "/api/test/{id}", func() {})
if v := len(DefaultMux.router.handlers); v != 3 {
t.Fatalf("unexpected: %v", v)
}
}
func TestUconContextWithValue(t *testing.T) {
var ctx Context = background
if v := ctx.Value("a"); v != nil {
t.Fatalf("unexpected: %v", v)
}
ctx = WithValue(ctx, "a", "b")
if v := ctx.Value("a"); v.(string) != "b" {
t.Fatalf("unexpected: %v", v)
}
ctx = WithValue(ctx, 1, 2)
if v := ctx.Value("a"); v.(string) != "b" {
t.Fatalf("unexpected: %v", v)
}
if v := ctx.Value(1); v.(int) != 2 {
t.Fatalf("unexpected: %v", v)
}
}