]> Sergey Matveev's repositories - btrtrc.git/blob - webseed/request_test.go
Merge fs module back into the root module
[btrtrc.git] / webseed / request_test.go
1 package webseed
2
3 import (
4         "net/url"
5         "testing"
6
7         qt "github.com/frankban/quicktest"
8 )
9
10 func TestDefaultPathEscaper(t *testing.T) {
11         c := qt.New(t)
12         test := func(unescaped string, parts ...string) {
13                 assertPartsUnescape(c, unescaped, parts...)
14         }
15         for _, tc := range defaultPathEscapeTestCases {
16                 test(tc.escaped, tc.parts...)
17         }
18 }
19
20 // So we can manually check, and use these to seed fuzzing.
21 var defaultPathEscapeTestCases = []struct {
22         escaped string
23         parts   []string
24 }{
25         {"/", []string{"", ""}},
26         {"a_b-c/d + e.f", []string{"a_b-c", "d + e.f"}},
27         {"a_1-b_c2/d 3. (e, f).g", []string{"a_1-b_c2", "d 3. (e, f).g"}},
28         {"a_b-c/d + e.f", []string{"a_b-c", "d + e.f"}},
29         {"a_1-b_c2/d 3. (e, f).g", []string{"a_1-b_c2", "d 3. (e, f).g"}},
30         {"war/and/peace", []string{"war", "and", "peace"}},
31         {"he//o#world/world", []string{"he//o#world", "world"}},
32         {`ノ┬─┬ノ ︵ ( \o°o)\`, []string{`ノ┬─┬ノ ︵ ( \o°o)\`}},
33         {
34                 `%aa + %bb/Parsi Tv - سرقت و باز کردن در ماشین در کم‌تر از ۳ ثانیه + فیلم.webm`,
35                 []string{`%aa + %bb`, `Parsi Tv - سرقت و باز کردن در ماشین در کم‌تر از ۳ ثانیه + فیلم.webm`},
36         },
37 }
38
39 func assertPartsUnescape(c *qt.C, unescaped string, parts ...string) {
40         escaped := defaultPathEscaper(parts)
41         pathUnescaped, err := url.PathUnescape(escaped)
42         c.Assert(err, qt.IsNil)
43         c.Assert(pathUnescaped, qt.Equals, unescaped)
44         queryUnescaped, err := url.QueryUnescape(escaped)
45         c.Assert(err, qt.IsNil)
46         c.Assert(queryUnescaped, qt.Equals, unescaped)
47 }
48
49 func FuzzDefaultPathEscaper(f *testing.F) {
50         for _, tc := range defaultPathEscapeTestCases {
51                 if len(tc.parts) == 2 {
52                         f.Add(tc.parts[0], tc.parts[1])
53                 }
54         }
55         // I think a single separator is enough to test special handling around /. Also fuzzing doesn't
56         // let us take []string as an input.
57         f.Fuzz(func(t *testing.T, first, second string) {
58                 assertPartsUnescape(qt.New(t), first+"/"+second, first, second)
59         })
60 }