forked from cavaliergopher/grab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grab_test.go
298 lines (264 loc) · 7.45 KB
/
grab_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package grab
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"strconv"
"testing"
"time"
)
// ts is a test HTTP server that serves configurable content for all test
// functions.
//
// The following URL query parameters are supported:
//
// * filename=[string] return a filename in the Content-Disposition header of
// the response
//
// * lastmod=[unix] set the Last-Modified header
//
// * nohead disabled support for HEAD requests
//
// * range=[bool] allow byte range requests (default: yes)
//
// * rate=[int] throttle file transfer to the given limit as
// bytes per second
//
// * size=[int] return a file of the specified size in bytes
//
// * sleep=[int] delay the response by the given number of
// milliseconds (before sending headers)
//
// * status=[int] return the given status code
//
var ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// set status code
statusCode := http.StatusOK
if v := r.URL.Query().Get("status"); v != "" {
if _, err := fmt.Sscanf(v, "%d", &statusCode); err != nil {
panic(err)
}
}
if r.Method == "HEAD" {
if v := r.URL.Query().Get("headStatus"); v != "" {
if _, err := fmt.Sscanf(v, "%d", &statusCode); err != nil {
panic(err)
}
}
}
// allow HEAD requests?
if _, ok := r.URL.Query()["nohead"]; ok && r.Method == "HEAD" {
http.Error(w, "HEAD method not allowed", http.StatusMethodNotAllowed)
return
}
// compute transfer size from 'size' parameter (default 1Mb)
size := 1048576
if sizep := r.URL.Query().Get("size"); sizep != "" {
if _, err := fmt.Sscanf(sizep, "%d", &size); err != nil {
panic(err)
}
}
// support ranged requests (default yes)?
ranged := true
if rangep := r.URL.Query().Get("ranged"); rangep != "" {
if _, err := fmt.Sscanf(rangep, "%t", &ranged); err != nil {
panic(err)
}
}
// set filename in headers (default no)?
if filenamep := r.URL.Query().Get("filename"); filenamep != "" {
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=\"%s\"", filenamep))
}
// set Last-Modified header
if lastmodp := r.URL.Query().Get("lastmod"); lastmodp != "" {
lastmodi, err := strconv.ParseInt(lastmodp, 10, 64)
if err != nil {
panic(err)
}
lastmodt := time.Unix(lastmodi, 0).UTC()
lastmod := lastmodt.Format("Mon, 02 Jan 2006 15:04:05") + " GMT"
w.Header().Set("Last-Modified", lastmod)
}
// sleep before responding?
sleep := 0
if sleepp := r.URL.Query().Get("sleep"); sleepp != "" {
if _, err := fmt.Sscanf(sleepp, "%d", &sleep); err != nil {
panic(err)
}
}
// throttle rate to n bps
rate := 0 // bps
var throttle *time.Ticker
defer func() {
if throttle != nil {
throttle.Stop()
}
}()
if ratep := r.URL.Query().Get("rate"); ratep != "" {
if _, err := fmt.Sscanf(ratep, "%d", &rate); err != nil {
panic(err)
}
if rate > 0 {
throttle = time.NewTicker(time.Second / time.Duration(rate))
}
}
// compute offset
offset := 0
if rangeh := r.Header.Get("Range"); rangeh != "" {
if _, err := fmt.Sscanf(rangeh, "bytes=%d-", &offset); err != nil {
panic(err)
}
// make sure range is in range
if offset >= size {
w.WriteHeader(http.StatusRequestedRangeNotSatisfiable)
return
}
}
// delay response
if sleep > 0 {
time.Sleep(time.Duration(sleep) * time.Millisecond)
}
// set response headers
w.Header().Set("Content-Length", fmt.Sprintf("%d", size-offset))
if ranged {
w.Header().Set("Accept-Ranges", "bytes")
}
w.WriteHeader(statusCode)
// serve content body if method == "GET"
if r.Method == "GET" {
// use buffered io to reduce overhead on the reader
bw := bufio.NewWriterSize(w, 4096)
for i := offset; i < size; i++ {
bw.Write([]byte{byte(i)})
if throttle != nil {
<-throttle.C
}
}
bw.Flush()
}
}))
func TestMain(m *testing.M) {
os.Exit(func() int {
// clean up test web server
defer ts.Close()
// chdir to temp so test files downloaded to pwd are isolated and cleaned up
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
tmpDir, err := ioutil.TempDir("", "grab-")
if err != nil {
panic(err)
}
if err := os.Chdir(tmpDir); err != nil {
panic(err)
}
defer func() {
os.Chdir(cwd)
if err := os.RemoveAll(tmpDir); err != nil {
panic(err)
}
}()
return m.Run()
}())
}
// TestTestServer ensures that the test server behaves as expected so that it
// does not pollute other tests.
func TestTestServer(t *testing.T) {
t.Run("default", func(t *testing.T) {
req, _ := http.NewRequest("GET", ts.URL+"?nohead", nil)
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
expectSize := 1048576
if h := resp.ContentLength; h != int64(expectSize) {
t.Fatalf("expected Content-Length: %v, got %v", expectSize, h)
}
b, _ := ioutil.ReadAll(resp.Body)
if len(b) != expectSize {
t.Fatalf("expected body length: %v, got %v", expectSize, len(b))
}
if h := resp.Header.Get("Accept-Ranges"); h != "bytes" {
t.Fatalf("expected Accept-Ranges: bytes, got: %v", h)
}
})
t.Run("nohead", func(t *testing.T) {
req, _ := http.NewRequest("HEAD", ts.URL+"?nohead", nil)
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
if resp.StatusCode != http.StatusMethodNotAllowed {
panic("HEAD request was allowed despite ?nohead being set")
}
})
t.Run("headStatus", func(t *testing.T) {
expect := http.StatusTeapot
req, _ := http.NewRequest(
"HEAD",
fmt.Sprintf("%s?headStatus=%d", ts.URL, expect),
nil)
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
if resp.StatusCode != expect {
t.Fatalf("expected status: %v, got: %v", expect, resp.StatusCode)
}
})
t.Run("size", func(t *testing.T) {
req, _ := http.NewRequest("GET", ts.URL+"?size=321", nil)
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
if resp.ContentLength != 321 {
t.Fatalf("expected Content-Length: %v, got %v", 321, resp.ContentLength)
}
b, _ := ioutil.ReadAll(resp.Body)
if len(b) != 321 {
t.Fatalf("expected body length: %v, got %v", 321, len(b))
}
})
t.Run("ranged=false", func(t *testing.T) {
req, _ := http.NewRequest("GET", ts.URL+"?ranged=false", nil)
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
if h := resp.Header.Get("Accept-Ranges"); h != "" {
t.Fatalf("expected empty Accept-Ranges header, got: %v", h)
}
})
t.Run("filename", func(t *testing.T) {
req, _ := http.NewRequest("GET", ts.URL+"?filename=test", nil)
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
expect := "attachment;filename=\"test\""
if h := resp.Header.Get("Content-Disposition"); h != expect {
t.Fatalf("expected Content-Disposition header: %v, got %v", expect, h)
}
})
t.Run("lastmod", func(t *testing.T) {
req, _ := http.NewRequest("GET", ts.URL+"?lastmod=123456789", nil)
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()
expect := "Thu, 29 Nov 1973 21:33:09 GMT"
if h := resp.Header.Get("Last-Modified"); h != expect {
t.Fatalf("expected Last-Modified header: %v, got %v", expect, h)
}
})
}
// TestGet tests grab.Get
func TestGet(t *testing.T) {
filename := ".testGet"
defer os.Remove(filename)
resp, err := Get(filename, ts.URL)
if err != nil {
t.Fatalf("error in Get(): %v", err)
}
testComplete(t, resp)
}
func ExampleGet() {
// download a file to /tmp
resp, err := Get("/tmp", "http://example.com/example.zip")
if err != nil {
log.Fatal(err)
}
fmt.Println("Download saved to", resp.Filename)
}