@@ -13,35 +13,148 @@ import (
13
13
)
14
14
15
15
func TestURLJoin (t * testing.T ) {
16
- type test struct {
17
- Expected string
16
+ cases := []struct {
18
17
Base string
19
- Elements []string
20
- }
21
- newTest := func (expected , base string , elements ... string ) test {
22
- return test {Expected : expected , Base : base , Elements : elements }
18
+ Elems []string
19
+ Expected string
20
+ }{
21
+ {
22
+ Base : "https://try.gitea.io" ,
23
+ Elems : []string {"a/b" , "c" },
24
+ Expected : "https://try.gitea.io/a/b/c" ,
25
+ },
26
+ {
27
+ Base : "https://try.gitea.io/" ,
28
+ Elems : []string {"a/b" , "c" },
29
+ Expected : "https://try.gitea.io/a/b/c" ,
30
+ },
31
+ {
32
+ Base : "https://try.gitea.io/" ,
33
+ Elems : []string {"/a/b/" , "/c/" },
34
+ Expected : "https://try.gitea.io/a/b/c" ,
35
+ },
36
+ {
37
+ Base : "https://try.gitea.io/" ,
38
+ Elems : []string {"/a/./b/" , "../c/" },
39
+ Expected : "https://try.gitea.io/a/c" ,
40
+ },
41
+ {
42
+ Base : "a" ,
43
+ Elems : []string {"b/c/" },
44
+ Expected : "a/b/c" ,
45
+ },
46
+ {
47
+ Base : "a/" ,
48
+ Elems : []string {"b/c/" , "/../d/" },
49
+ Expected : "a/b/d" ,
50
+ },
51
+ {
52
+ Base : "https://try.gitea.io" ,
53
+ Elems : []string {"a/b" , "c#d" },
54
+ Expected : "https://try.gitea.io/a/b/c#d" ,
55
+ },
56
+ {
57
+ Base : "/a/" ,
58
+ Elems : []string {"b/c/" , "/../d/" },
59
+ Expected : "/a/b/d" ,
60
+ },
61
+ {
62
+ Base : "/a" ,
63
+ Elems : []string {"b/c/" },
64
+ Expected : "/a/b/c" ,
65
+ },
66
+ {
67
+ Base : "/a" ,
68
+ Elems : []string {"b/c#hash" },
69
+ Expected : "/a/b/c#hash" ,
70
+ },
71
+ {
72
+ Base : "\x7f " , // invalid url
73
+ Expected : "" ,
74
+ },
75
+ {
76
+ Base : "path" ,
77
+ Expected : "path/" ,
78
+ },
79
+ {
80
+ Base : "/path" ,
81
+ Expected : "/path/" ,
82
+ },
83
+ {
84
+ Base : "path/" ,
85
+ Expected : "path/" ,
86
+ },
87
+ {
88
+ Base : "/path/" ,
89
+ Expected : "/path/" ,
90
+ },
91
+ {
92
+ Base : "path" ,
93
+ Elems : []string {"sub" },
94
+ Expected : "path/sub" ,
95
+ },
96
+ {
97
+ Base : "/path" ,
98
+ Elems : []string {"sub" },
99
+ Expected : "/path/sub" ,
100
+ },
101
+ {
102
+ Base : "https://gitea.com" ,
103
+ Expected : "https://gitea.com/" ,
104
+ },
105
+ {
106
+ Base : "https://gitea.com/" ,
107
+ Expected : "https://gitea.com/" ,
108
+ },
109
+ {
110
+ Base : "https://gitea.com" ,
111
+ Elems : []string {"sub/path" },
112
+ Expected : "https://gitea.com/sub/path" ,
113
+ },
114
+ {
115
+ Base : "https://gitea.com/" ,
116
+ Elems : []string {"sub" , "path" },
117
+ Expected : "https://gitea.com/sub/path" ,
118
+ },
119
+ {
120
+ Base : "https://gitea.com/" ,
121
+ Elems : []string {"sub" , ".." , "path" },
122
+ Expected : "https://gitea.com/path" ,
123
+ },
124
+ {
125
+ Base : "https://gitea.com/" ,
126
+ Elems : []string {"sub/.." , "path" },
127
+ Expected : "https://gitea.com/path" ,
128
+ },
129
+ {
130
+ Base : "https://gitea.com/" ,
131
+ Elems : []string {"sub" , "../path" },
132
+ Expected : "https://gitea.com/path" ,
133
+ },
134
+ {
135
+ Base : "https://gitea.com/" ,
136
+ Elems : []string {"sub/../path" },
137
+ Expected : "https://gitea.com/path" ,
138
+ },
139
+ {
140
+ Base : "https://gitea.com/" ,
141
+ Elems : []string {"sub" , "." , "path" },
142
+ Expected : "https://gitea.com/sub/path" ,
143
+ },
144
+ {
145
+ Base : "https://gitea.com/" ,
146
+ Elems : []string {"sub" , "/" , "path" },
147
+ Expected : "https://gitea.com/sub/path" ,
148
+ },
149
+ { // https://github.com/go-gitea/gitea/issues/25632
150
+ Base : "/owner/repo/media/branch/main" ,
151
+ Elems : []string {"/../other/image.png" },
152
+ Expected : "/owner/repo/media/branch/other/image.png" ,
153
+ },
23
154
}
24
- for _ , test := range []test {
25
- newTest ("https://try.gitea.io/a/b/c" ,
26
- "https://try.gitea.io" , "a/b" , "c" ),
27
- newTest ("https://try.gitea.io/a/b/c" ,
28
- "https://try.gitea.io/" , "/a/b/" , "/c/" ),
29
- newTest ("https://try.gitea.io/a/c" ,
30
- "https://try.gitea.io/" , "/a/./b/" , "../c/" ),
31
- newTest ("a/b/c" ,
32
- "a" , "b/c/" ),
33
- newTest ("a/b/d" ,
34
- "a/" , "b/c/" , "/../d/" ),
35
- newTest ("https://try.gitea.io/a/b/c#d" ,
36
- "https://try.gitea.io" , "a/b" , "c#d" ),
37
- newTest ("/a/b/d" ,
38
- "/a/" , "b/c/" , "/../d/" ),
39
- newTest ("/a/b/c" ,
40
- "/a" , "b/c/" ),
41
- newTest ("/a/b/c#hash" ,
42
- "/a" , "b/c#hash" ),
43
- } {
44
- assert .Equal (t , test .Expected , URLJoin (test .Base , test .Elements ... ))
155
+
156
+ for i , c := range cases {
157
+ assert .Equal (t , c .Expected , URLJoin (c .Base , c .Elems ... ), "Unexpected result in test case %v" , i )
45
158
}
46
159
}
47
160
0 commit comments