forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_test.go
101 lines (91 loc) · 2.45 KB
/
path_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
package router
import (
"testing"
)
func TestCleanPath(t *testing.T) {
tests := []struct {
path string
expected string
}{
{"/",
"/"},
{"noslashPrefix",
"/noslashPrefix"},
{"slashSuffix/",
"/slashSuffix"},
{"noSlashPrefixAndslashSuffix/",
"/noSlashPrefixAndslashSuffix"},
// don't do any clean up inside {},
// fixes #927.
{"/total/{year:string regexp(\\d{4})}",
"/total/{year:string regexp(\\d{4})}"},
{"/total/{year:string regexp(\\d{4})}/more",
"/total/{year:string regexp(\\d{4})}/more"},
{"/total/{year:string regexp(\\d{4})}/more/{s:string regexp(\\d{7})}",
"/total/{year:string regexp(\\d{4})}/more/{s:string regexp(\\d{7})}"},
{"/single_no_params",
"/single_no_params"},
{"/single/{id:int}",
"/single/{id:int}"},
}
for i, tt := range tests {
if expected, got := tt.expected, cleanPath(tt.path); expected != got {
t.Fatalf("[%d] - expected path '%s' but got '%s'", i, expected, got)
}
}
}
func TestSplitPath(t *testing.T) {
tests := []struct {
path string
expected []string
}{
{"/v2/stores/{id:string format(uuid)} /v3",
[]string{"/v2/stores/{id:string format(uuid)}", "/v3"}},
{"/user/{id:int} /admin/{id:int}",
[]string{"/user/{id:int}", "/admin/{id:int}"}},
{"/user /admin",
[]string{"/user", "/admin"}},
{"/single_no_params",
[]string{"/single_no_params"}},
{"/single/{id:int}",
[]string{"/single/{id:int}"}},
}
equalSlice := func(s1 []string, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
for i := range s1 {
if s2[i] != s1[i] {
return false
}
}
return true
}
for i, tt := range tests {
paths := splitPath(tt.path)
if expected, got := tt.expected, paths; !equalSlice(expected, got) {
t.Fatalf("[%d] - expected paths '%#v' but got '%#v'", i, expected, got)
}
}
}
func TestSplitSubdomainAndPath(t *testing.T) {
tests := []struct {
original string
subdomain string
path string
}{
{"admin./users/42", "admin.", "/users/42"},
{"//api/users\\42", "", "/api/users/42"},
{"admin./users//42", "admin.", "/users/42"},
{"*./users/42/", "*.", "/users/42"},
}
for i, tt := range tests {
subdomain, path := splitSubdomainAndPath(tt.original)
if expected, got := tt.subdomain, subdomain; expected != got {
t.Fatalf("[%d] - expected subdomain '%s' but got '%s'", i, expected, got)
}
if expected, got := tt.path, path; expected != got {
t.Fatalf("[%d] - expected path '%s' but got '%s'", i, expected, got)
}
}
}