]> Sergey Matveev's repositories - btrtrc.git/blob - webseed/request_test.go
7f691e0a04523b16f0e3bc11d9afa32b1ab2b45b
[btrtrc.git] / webseed / request_test.go
1 package webseed
2
3 import (
4         "net/url"
5         "path"
6         "testing"
7
8         qt "github.com/frankban/quicktest"
9 )
10
11 func TestEscapePath(t *testing.T) {
12         c := qt.New(t)
13         test := func(
14                 parts []string, result string,
15                 escaper PathEscaper,
16                 unescaper func(string) (string, error),
17         ) {
18                 unescaped, err := unescaper(escaper(parts))
19                 if !c.Check(err, qt.IsNil) {
20                         return
21                 }
22                 c.Check(unescaped, qt.Equals, result)
23         }
24
25         // Test with nil escapers (always uses url.QueryEscape)
26         // ------
27         test(
28                 []string{"a_b-c", "d + e.f"},
29                 "a_b-c/d + e.f",
30                 defaultPathEscaper,
31                 url.QueryUnescape,
32         )
33         test(
34                 []string{"a_1-b_c2", "d 3. (e, f).g"},
35                 "a_1-b_c2/d 3. (e, f).g",
36                 defaultPathEscaper,
37                 url.QueryUnescape,
38         )
39
40         // Test with custom escapers
41         // ------
42         test(
43                 []string{"a_b-c", "d + e.f"},
44                 "a_b-c/d + e.f",
45                 func(s []string) string {
46                         var ret []string
47                         for _, comp := range s {
48                                 ret = append(ret, url.PathEscape(comp))
49                         }
50                         return path.Join(ret...)
51                 },
52                 url.PathUnescape,
53         )
54         test(
55                 []string{"a_1-b_c2", "d 3. (e, f).g"},
56                 "a_1-b_c2/d 3. (e, f).g",
57                 func(s []string) string {
58                         var ret []string
59                         for _, comp := range s {
60                                 ret = append(ret, url.PathEscape(comp))
61                         }
62                         return path.Join(ret...)
63                 },
64                 url.PathUnescape,
65         )
66 }
67
68 func TestEscapePathForEmptyInfoName(t *testing.T) {
69         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")
70         qt.Check(t, defaultPathEscaper([]string{"hello", "world"}), qt.Equals, "hello/world")
71         qt.Check(t, defaultPathEscaper([]string{"war", "and", "peace"}), qt.Equals, "war/and/peace")
72 }