]> Sergey Matveev's repositories - btrtrc.git/blobdiff - webseed/request_test.go
fixup! [webseed] Add a custom URL encoder for webseeds
[btrtrc.git] / webseed / request_test.go
index b59e7784d67e482298ed80deefb7274d3f9cb1ea..d39e20e9b3b108269f5ae53e2201825b70d23548 100644 (file)
@@ -2,28 +2,71 @@ package webseed
 
 import (
        "net/url"
+       "path"
        "testing"
 
        qt "github.com/frankban/quicktest"
 )
 
-func TestTrailingPath(t *testing.T) {
+func TestEscapePath(t *testing.T) {
        c := qt.New(t)
-       test := func(parts []string, result string) {
-               unescaped, err := url.QueryUnescape(trailingPath(parts[0], parts[1:], url.QueryEscape))
+       test := func(
+               parts []string, result string,
+               escaper PathEscaper,
+               unescaper func(string) (string, error),
+       ) {
+               unescaped, err := unescaper(escapePath(parts, escaper))
                if !c.Check(err, qt.IsNil) {
                        return
                }
                c.Check(unescaped, qt.Equals, result)
        }
-       test([]string{"a_b-c", "d + e.f"}, "a_b-c/d + e.f")
-       test([]string{"a_1-b_c2", "d 3. (e, f).g"},
+
+       // Test with nil escapers (always uses url.QueryEscape)
+       // ------
+       test(
+               []string{"a_b-c", "d + e.f"},
+               "a_b-c/d + e.f",
+               nil,
+               url.QueryUnescape,
+       )
+       test(
+               []string{"a_1-b_c2", "d 3. (e, f).g"},
+               "a_1-b_c2/d 3. (e, f).g",
+               nil,
+               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"},
                "a_1-b_c2/d 3. (e, f).g",
+               func(s []string) string {
+                       var ret []string
+                       for _, comp := range s {
+                               ret = append(ret, url.PathEscape(comp))
+                       }
+                       return path.Join(ret...)
+               },
+               url.PathUnescape,
        )
 }
 
-func TestTrailingPathForEmptyInfoName(t *testing.T) {
-       qt.Check(t, trailingPath("", []string{`ノ┬─┬ノ ︵ ( \o°o)\`}, url.QueryEscape), 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, trailingPath("", []string{"hello", "world"}, url.QueryEscape), qt.Equals, "hello/world")
-       qt.Check(t, trailingPath("war", []string{"and", "peace"}, url.QueryEscape), qt.Equals, "war/and/peace")
+func TestEscapePathForEmptyInfoName(t *testing.T) {
+       qt.Check(t, escapePath([]string{`ノ┬─┬ノ ︵ ( \o°o)\`}, nil), 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, escapePath([]string{"hello", "world"}, nil), qt.Equals, "hello/world")
+       qt.Check(t, escapePath([]string{"war", "and", "peace"}, nil), qt.Equals, "war/and/peace")
 }