-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrab_test.go
74 lines (66 loc) · 1.3 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
package grab
import (
"fmt"
"io/ioutil"
"log"
"os"
"testing"
"github.com/cavaliercoder/grab/grabtest"
)
func TestMain(m *testing.M) {
os.Exit(func() int {
// 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()
}())
}
// TestGet tests grab.Get
func TestGet(t *testing.T) {
filename := ".testGet"
defer os.Remove(filename)
grabtest.WithTestServer(t, func(url string) {
resp, err := Get(filename, 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)
}
func mustNewRequest(dst, urlStr string) *Request {
req, err := NewRequest(dst, urlStr)
if err != nil {
panic(err)
}
return req
}
func mustDo(req *Request) *Response {
resp := DefaultClient.Do(req)
if err := resp.Err(); err != nil {
panic(err)
}
return resp
}