-
Notifications
You must be signed in to change notification settings - Fork 47
/
path_test.go
155 lines (131 loc) · 3.77 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package router
import (
"reflect"
"runtime"
"testing"
"github.com/savsgio/gotils/strings"
"github.com/valyala/fasthttp"
)
type cleanPathTest struct {
path, result string
}
var cleanTests = []cleanPathTest{
// Already clean
{"/", "/"},
{"/abc", "/abc"},
{"/a/b/c", "/a/b/c"},
{"/abc/", "/abc/"},
{"/a/b/c/", "/a/b/c/"},
// missing root
{"", "/"},
{"a/", "/a/"},
{"abc", "/abc"},
{"abc/def", "/abc/def"},
{"a/b/c", "/a/b/c"},
// Remove doubled slash
{"//", "/"},
{"/abc//", "/abc/"},
{"/abc/def//", "/abc/def/"},
{"/a/b/c//", "/a/b/c/"},
{"/abc//def//ghi", "/abc/def/ghi"},
{"//abc", "/abc"},
{"///abc", "/abc"},
{"//abc//", "/abc/"},
// Remove . elements
{".", "/"},
{"./", "/"},
{"/abc/./def", "/abc/def"},
{"/./abc/def", "/abc/def"},
{"/abc/.", "/abc/"},
// Remove .. elements
{"..", "/"},
{"../", "/"},
{"../../", "/"},
{"../..", "/"},
{"../../abc", "/abc"},
{"/abc/def/ghi/../jkl", "/abc/def/jkl"},
{"/abc/def/../ghi/../jkl", "/abc/jkl"},
{"/abc/def/..", "/abc/"},
{"/abc/def/../..", "/"},
{"/abc/def/../../..", "/"},
{"/abc/def/../../..", "/"},
{"/abc/def/../../../ghi/jkl/../../../mno", "/mno"},
// Combinations
{"abc/./../def", "/def"},
{"abc//./../def", "/def"},
{"abc/../../././../def", "/def"},
}
func Test_cleanPath(t *testing.T) {
if runtime.GOOS == "windows" {
t.SkipNow()
}
req := new(fasthttp.Request)
uri := req.URI()
for _, test := range cleanTests {
uri.SetPath(test.path)
if s := cleanPath(string(uri.Path())); s != test.result {
t.Errorf("cleanPath(%q) = %q, want %q", test.path, s, test.result)
}
uri.SetPath(test.result)
if s := cleanPath(string(uri.Path())); s != test.result {
t.Errorf("cleanPath(%q) = %q, want %q", test.result, s, test.result)
}
}
}
func TestGetOptionalPath(t *testing.T) {
handler := func(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusOK)
}
expected := []struct {
path string
tsr bool
handler fasthttp.RequestHandler
}{
{"/show/{name}", false, handler},
{"/show/{name}/", true, nil},
{"/show/{name}/{surname}", false, handler},
{"/show/{name}/{surname}/", true, nil},
{"/show/{name}/{surname}/at", false, handler},
{"/show/{name}/{surname}/at/", true, nil},
{"/show/{name}/{surname}/at/{address}", false, handler},
{"/show/{name}/{surname}/at/{address}/", true, nil},
{"/show/{name}/{surname}/at/{address}/{id}", false, handler},
{"/show/{name}/{surname}/at/{address}/{id}/", true, nil},
{"/show/{name}/{surname}/at/{address}/{id}/{phone:.*}", false, handler},
{"/show/{name}/{surname}/at/{address}/{id}/{phone:.*}/", true, nil},
}
r := New()
r.GET("/show/{name}/{surname?}/at/{address?}/{id}/{phone?:.*}", handler)
for _, e := range expected {
ctx := new(fasthttp.RequestCtx)
h, tsr := r.Lookup("GET", e.path, ctx)
if tsr != e.tsr {
t.Errorf("TSR (path: %s) == %v, want %v", e.path, tsr, e.tsr)
}
if reflect.ValueOf(h).Pointer() != reflect.ValueOf(e.handler).Pointer() {
t.Errorf("Handler (path: %s) == %p, want %p", e.path, h, e.handler)
}
}
tests := []struct {
path string
optionalPaths []string
}{
{"/hello", nil},
{"/{name}", nil},
{"/{name?:[a-zA-Z]{5}}", []string{"/", "/{name:[a-zA-Z]{5}}"}},
{"/{filepath:^(?!api).*}", nil},
{"/static/{filepath?:^(?!api).*}", []string{"/static", "/static/{filepath:^(?!api).*}"}},
{"/show/{name?}", []string{"/show", "/show/{name}"}},
}
for _, test := range tests {
optionalPaths := getOptionalPaths(test.path)
if len(optionalPaths) != len(test.optionalPaths) {
t.Errorf("getOptionalPaths() len == %d, want %d", len(optionalPaths), len(test.optionalPaths))
}
for _, wantPath := range test.optionalPaths {
if !strings.Include(optionalPaths, wantPath) {
t.Errorf("The optional path is not returned for '%s': %s", test.path, wantPath)
}
}
}
}