-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpath_test.go
More file actions
87 lines (76 loc) · 2.12 KB
/
path_test.go
File metadata and controls
87 lines (76 loc) · 2.12 KB
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
package hime
import (
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuildPath(t *testing.T) {
t.Parallel()
cases := []struct {
Input string
Output string
}{
{"", "/"},
{"/", "/"},
{"/p", "/p"},
{"/p/123", "/p/123"},
{"https://google.com", "https://google.com/"},
{"https://google.com/test", "https://google.com/test"},
{"https://google.com/test/", "https://google.com/test/"},
{"https://google.com/test?p=1", "https://google.com/test?p=1"},
{"http://google.com/test?p=1", "http://google.com/test?p=1"},
{"//a?p=1", "//a/?p=1"},
{"app:///a?p=1", "app:///a?p=1"},
}
for _, c := range cases {
assert.Equal(t, c.Output, buildPath(c.Input))
}
}
func TestBuildPathParams(t *testing.T) {
t.Parallel()
cases := []struct {
Base string
Params []any
Output string
}{
{"", []any{""}, "/"},
{"/", []any{"/"}, "/"},
{"/a", []any{}, "/a"},
{"/a", []any{"/b"}, "/a/b"},
{"/a?x=1", []any{"/b"}, "/a/b?x=1"},
{"/a/", []any{"/b/", "/c/"}, "/a/b/c"},
{"/a", []any{url.Values{"id": []string{"10"}}}, "/a?id=10"},
{"/a", []any{"/b", url.Values{"id": []string{"10"}}}, "/a/b?id=10"},
{"/a", []any{"/b/", url.Values{"id": []string{"10"}}}, "/a/b?id=10"},
{"/a", []any{"/b/", map[string]string{"id": "10"}}, "/a/b?id=10"},
{"/a", []any{"/b/", map[string]any{"id": 10}}, "/a/b?id=10"},
{"/a", []any{"/b", &Param{Name: "id", Value: 3456}}, "/a/b?id=3456"},
{"/a?x=1", []any{"/b", &Param{Name: "id", Value: 3456}}, "/a/b?id=3456&x=1"},
}
for _, c := range cases {
assert.Equal(t, c.Output, buildPath(c.Base, c.Params...))
}
}
func TestSafeRedirectPath(t *testing.T) {
t.Parallel()
cases := []struct {
Input string
Output string
}{
{"", "/"},
{"/", "/"},
{"/p", "/p"},
{"/p/123", "/p/123"},
{"https://google.com", "/"},
{"https://google.com/test", "/test"},
{"https://google.com/test?p=1", "/test?p=1"},
{"http://google.com/test?p=1", "/test?p=1"},
{"//a?p=1", "/a?p=1"},
{"app:///a?p=1", "/a?p=1"},
{"/p/123?id=3", "/p/123?id=3"},
{"/p/123/?id=3", "/p/123/?id=3"},
}
for _, c := range cases {
assert.Equal(t, c.Output, SafeRedirectPath(c.Input))
}
}