forked from emicklei/go-restful
-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_processor_test.go
55 lines (47 loc) · 1.45 KB
/
path_processor_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
package restful
import "testing"
func TestMatchesPath_OneParam(t *testing.T) {
params := doExtractParams("/from/{source}", 2, "/from/here", t)
if params["source"] != "here" {
t.Errorf("parameter mismatch here")
}
}
func TestMatchesPath_Slash(t *testing.T) {
params := doExtractParams("/", 0, "/", t)
if len(params) != 0 {
t.Errorf("expected empty parameters")
}
}
func TestMatchesPath_SlashNonVar(t *testing.T) {
params := doExtractParams("/any", 1, "/any", t)
if len(params) != 0 {
t.Errorf("expected empty parameters")
}
}
func TestMatchesPath_TwoVars(t *testing.T) {
params := doExtractParams("/from/{source}/to/{destination}", 4, "/from/AMS/to/NY", t)
if params["source"] != "AMS" {
t.Errorf("parameter mismatch AMS")
}
}
func TestMatchesPath_VarOnFront(t *testing.T) {
params := doExtractParams("{what}/from/{source}/", 3, "who/from/SOS/", t)
if params["source"] != "SOS" {
t.Errorf("parameter mismatch SOS")
}
}
func TestExtractParameters_EmptyValue(t *testing.T) {
params := doExtractParams("/fixed/{var}", 2, "/fixed/", t)
if params["var"] != "" {
t.Errorf("parameter mismatch var")
}
}
func doExtractParams(routePath string, size int, urlPath string, t *testing.T) map[string]string {
r := Route{Path: routePath}
r.postBuild()
if len(r.pathParts) != size {
t.Fatalf("len not %v %v, but %v", size, r.pathParts, len(r.pathParts))
}
pathProcessor := defaultPathProcessor{}
return pathProcessor.ExtractParameters(&r, nil, urlPath)
}