2020-12-21 01:24:24 +00:00
|
|
|
package webseed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
2022-04-22 02:23:43 +00:00
|
|
|
"path"
|
2020-12-21 01:24:24 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
qt "github.com/frankban/quicktest"
|
|
|
|
)
|
|
|
|
|
2022-04-22 02:23:43 +00:00
|
|
|
func TestEscapePath(t *testing.T) {
|
2020-12-21 01:24:24 +00:00
|
|
|
c := qt.New(t)
|
2022-04-22 02:23:43 +00:00
|
|
|
test := func(
|
|
|
|
parts []string, result string,
|
|
|
|
escaper PathEscaper,
|
|
|
|
unescaper func(string) (string, error),
|
|
|
|
) {
|
2022-04-26 00:46:01 +00:00
|
|
|
unescaped, err := unescaper(escaper(parts))
|
2020-12-21 01:24:24 +00:00
|
|
|
if !c.Check(err, qt.IsNil) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Check(unescaped, qt.Equals, result)
|
|
|
|
}
|
2022-04-22 02:23:43 +00:00
|
|
|
|
|
|
|
// Test with nil escapers (always uses url.QueryEscape)
|
|
|
|
// ------
|
|
|
|
test(
|
|
|
|
[]string{"a_b-c", "d + e.f"},
|
|
|
|
"a_b-c/d + e.f",
|
2022-04-26 00:46:01 +00:00
|
|
|
defaultPathEscaper,
|
2022-04-22 02:23:43 +00:00
|
|
|
url.QueryUnescape,
|
|
|
|
)
|
|
|
|
test(
|
|
|
|
[]string{"a_1-b_c2", "d 3. (e, f).g"},
|
|
|
|
"a_1-b_c2/d 3. (e, f).g",
|
2022-04-26 00:46:01 +00:00
|
|
|
defaultPathEscaper,
|
2022-04-22 02:23:43 +00:00
|
|
|
url.QueryUnescape,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Test with custom escapers
|
|
|
|
// ------
|
|
|
|
test(
|
|
|
|
[]string{"a_b-c", "d + e.f"},
|
|
|
|
"a_b-c/d + e.f",
|
|
|
|
func(s []string) string {
|
|
|
|
var ret []string
|
|
|
|
for _, comp := range s {
|
|
|
|
ret = append(ret, url.PathEscape(comp))
|
|
|
|
}
|
|
|
|
return path.Join(ret...)
|
|
|
|
},
|
|
|
|
url.PathUnescape,
|
|
|
|
)
|
|
|
|
test(
|
|
|
|
[]string{"a_1-b_c2", "d 3. (e, f).g"},
|
2020-12-21 01:24:24 +00:00
|
|
|
"a_1-b_c2/d 3. (e, f).g",
|
2022-04-22 02:23:43 +00:00
|
|
|
func(s []string) string {
|
|
|
|
var ret []string
|
|
|
|
for _, comp := range s {
|
|
|
|
ret = append(ret, url.PathEscape(comp))
|
|
|
|
}
|
|
|
|
return path.Join(ret...)
|
|
|
|
},
|
|
|
|
url.PathUnescape,
|
2020-12-21 01:24:24 +00:00
|
|
|
)
|
|
|
|
}
|
2022-03-11 01:03:18 +00:00
|
|
|
|
2022-04-22 02:23:43 +00:00
|
|
|
func TestEscapePathForEmptyInfoName(t *testing.T) {
|
2022-04-26 00:46:01 +00:00
|
|
|
qt.Check(t, defaultPathEscaper([]string{`ノ┬─┬ノ ︵ ( \o°o)\`}), qt.Equals, "%E3%83%8E%E2%94%AC%E2%94%80%E2%94%AC%E3%83%8E+%EF%B8%B5+%28+%5Co%C2%B0o%29%5C")
|
|
|
|
qt.Check(t, defaultPathEscaper([]string{"hello", "world"}), qt.Equals, "hello/world")
|
|
|
|
qt.Check(t, defaultPathEscaper([]string{"war", "and", "peace"}), qt.Equals, "war/and/peace")
|
2022-03-11 01:03:18 +00:00
|
|
|
}
|