-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmux_test.go
78 lines (68 loc) · 1.78 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
package gosc
import (
"testing"
)
type testMessageHandler struct {
handled bool
}
func (h *testMessageHandler) HandleMessage(_ *ResponseWriter, _ *Message) {
h.handled = true
}
type testBundleHandler struct {
handled bool
}
func (h *testBundleHandler) HandleBundle(_ *ResponseWriter, _ *Bundle) {
h.handled = true
}
func TestNewMux(t *testing.T) {
mux := NewMux(nil)
if mux.messageHandlers == nil {
t.Error("expected Mux messageHandlers to be initialized")
}
}
func TestMux_HandlePackage(t *testing.T) {
t.Run("bundleWithHandler", func(t *testing.T) {
hand := &testBundleHandler{}
mux := NewMux(hand)
mux.HandlePackage(nil, &Bundle{})
if hand.handled != true {
t.Error("expected Mux with bundleHandler to handle the bundle")
}
})
t.Run("bundleWithoutHandler", func(t *testing.T) {
mux := NewMux(nil)
mux.HandlePackage(nil, &Bundle{})
})
t.Run("message", func(t *testing.T) {
handled := false
mux := NewMux(nil)
mux.HandleMessageFunc("/test", func(w *ResponseWriter, m *Message) {
handled = true
})
mux.HandlePackage(nil, &Message{
Address: "/test",
Arguments: []any{},
})
if !handled {
t.Error("expected Mux messageHandler for message to run")
}
})
}
func TestDefaultMux_HandleMessage(t *testing.T) {
mux := NewMux(nil)
han := &testMessageHandler{}
mux.HandleMessage("/test", han)
if len(mux.messageHandlers) != 1 {
t.Errorf("expected Mux messageHandlers to have 1 entry, got: %d", len(mux.messageHandlers))
}
}
func TestMux_HandleMessageFunc(t *testing.T) {
mux := NewMux(nil)
mux.HandleMessageFunc("/test", func(w *ResponseWriter, msg *Message) {})
if len(mux.messageHandlers) != 1 {
t.Errorf("expected Mux messageHandlers to have 1 entry, got: %d", len(mux.messageHandlers))
}
}
func TestMux_Send(t *testing.T) {
// TODO: Implement me
}