forked from dustin/go-coap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servmux_test.go
73 lines (65 loc) · 1.54 KB
/
servmux_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
package coap
import (
"net"
"testing"
)
func TestPathMatching(t *testing.T) {
m := NewServeMux()
msgs := map[string]int{}
m.HandleFunc("/a", func(l *net.UDPConn, a *net.UDPAddr, m *Message) *Message {
msgs["a"]++
return nil
})
m.HandleFunc("/b", func(l *net.UDPConn, a *net.UDPAddr, m *Message) *Message {
msgs["b"]++
return nil
})
m.HandleFunc("/", func(l *net.UDPConn, a *net.UDPAddr, m *Message) *Message {
msgs[""]++
return nil
})
msg := &Message{}
msg.SetPathString("/a")
m.ServeCOAP(nil, nil, msg)
msg.SetPathString("/a")
m.ServeCOAP(nil, nil, msg)
msg.SetPathString("/b")
m.ServeCOAP(nil, nil, msg)
msg.SetPathString("/c")
m.ServeCOAP(nil, nil, msg)
msg.Type = NonConfirmable
msg.SetPathString("/c")
m.ServeCOAP(nil, nil, msg)
msg.SetPathString("/")
m.ServeCOAP(nil, nil, msg)
m.ServeCOAP(nil, nil, msg)
if msgs["a"] != 2 {
t.Errorf("Expected 2 messages for /a, got %v", msgs["a"])
}
if msgs["b"] != 1 {
t.Errorf("Expected 1 message for /b, got %v", msgs["b"])
}
if msgs[""] != 2 {
t.Errorf("Expected 2 message for /, got %v", msgs[""])
}
}
func TestPathMatch(t *testing.T) {
tests := []struct {
pattern, path string
exp bool
}{
{"", "", true},
{"/", "/", true},
{"/a/b/c", "/a/b/c", true},
{"/a/b/c", "/a/b/c/d", false},
{"/a/b/c/", "/a/b/c/d", true},
{"/a/b/c", "/", false},
{"/a/", "/", false},
}
for _, test := range tests {
if pathMatch(test.pattern, test.path) != test.exp {
t.Errorf("Failed on pathMatch(%q, %q), wanted %v",
test.pattern, test.path, test.exp)
}
}
}