forked from cavaliergopher/grab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
123 lines (109 loc) · 2.48 KB
/
util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package grab
import (
"fmt"
"net/http"
"net/url"
"testing"
)
func TestURLFilenames(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
expect := "filename"
testCases := []string{
"http://test.com/filename",
"http://test.com/path/filename",
"http://test.com/deep/path/filename",
"http://test.com/filename?with=args",
"http://test.com/filename#with-fragment",
}
for _, tc := range testCases {
req, _ := http.NewRequest("GET", tc, nil)
resp := &http.Response{
Request: req,
}
actual, err := guessFilename(resp)
if err != nil {
t.Errorf("%v", err)
}
if actual != expect {
t.Errorf("expected '%v', got '%v'", expect, actual)
}
}
})
t.Run("Invalid", func(t *testing.T) {
testCases := []string{
"http://test.com",
"http://test.com/",
"http://test.com/filename/",
"http://test.com/filename/?with=args",
"http://test.com/filename/#with-fragment",
"http://test.com/filename\x00",
}
for _, tc := range testCases {
req, _ := http.NewRequest("GET", tc, nil)
resp := &http.Response{
Request: req,
}
_, err := guessFilename(resp)
if err != ErrNoFilename {
t.Errorf("expected '%v', got '%v'", ErrNoFilename, err)
}
}
})
}
func TestHeaderFilenames(t *testing.T) {
u, _ := url.ParseRequestURI("http://test.com/badfilename")
resp := &http.Response{
Request: &http.Request{
URL: u,
},
Header: http.Header{},
}
setFilename := func(resp *http.Response, filename string) {
resp.Header.Set("Content-Disposition", fmt.Sprintf("attachment;filename=\"%s\"", filename))
}
t.Run("Valid", func(t *testing.T) {
expect := "filename"
testCases := []string{
"filename",
"path/filename",
"/path/filename",
"../../filename",
"/path/../../filename",
"/../../././///filename",
}
for _, tc := range testCases {
setFilename(resp, tc)
actual, err := guessFilename(resp)
if err != nil {
t.Errorf("error (%v): %v", tc, err)
}
if actual != expect {
t.Errorf("expected '%v' (%v), got '%v'", expect, tc, actual)
}
}
})
t.Run("Invalid", func(t *testing.T) {
testCases := []string{
"",
"/",
".",
"/.",
"/./",
"..",
"../",
"/../",
"/path/",
"../path/",
"filename\x00",
"filename/",
"filename//",
"filename/..",
}
for _, tc := range testCases {
setFilename(resp, tc)
if actual, err := guessFilename(resp); err != ErrNoFilename {
t.Errorf("expected: %v (%v), got: %v (%v)", ErrNoFilename, tc, err, actual)
}
}
})
}