-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgodownload_test.go
126 lines (107 loc) · 2.91 KB
/
godownload_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
package godownload
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
const masterZIP = "master.zip"
func exist(path string) bool {
return checkExist(path)
}
func remove(t *testing.T, path string) {
err := os.Remove(path)
if err != nil {
t.Fatal(err)
}
}
func createFileWithLinks(filename string) {
data := `This file contains interesting links!
First: https://github.com/saromanov/godownload/archive/master.zip \n
Second: http://arxiv.org/pdf/1206.5538v3.pdf`
err := ioutil.WriteFile(filename, []byte(data), 0777)
if err != nil {
panic(err)
}
}
func TestDownload(t *testing.T) {
gd := &GoDownload{}
gd.Download("https://github.com/saromanov/godownload/archive/master.zip", nil)
path := masterZIP
if !exist(path) {
t.Fatal(fmt.Sprintf("TestDownload. Downloaded file %s not found", path))
}
remove(t, path)
}
func TestDownloadAlwaysNew(t *testing.T) {
gd := &GoDownload{}
gd.Download("https://github.com/saromanov/godownload/archive/master.zip", nil)
gd.Download("https://github.com/saromanov/godownload/archive/master.zip", &Options{
Outpath: masterZIP,
Alwaysnew: true,
})
if !exist("master_2.zip") {
t.Fatal(fmt.Sprintf("TestDownloadAlwaysNew. Downloaded file %s not found", "master_2.zip"))
}
remove(t, masterZIP)
remove(t, "master_2.zip")
}
func TestDownloadMany(t *testing.T) {
path1 := "first.zip"
path2 := "second.zip"
items := []*Options{
&Options{
URL: "https://github.com/saromanov/godownload/archive/master.zip",
Outpath: "first.zip",
},
&Options{
URL: "http://arxiv.org/pdf/1206.5538v3.pdf",
Outpath: "second.zip",
},
}
gd := &GoDownload{}
gd.DownloadMany(items)
if !exist(path1) {
t.Fatal(fmt.Sprintf("TestDownloadMany. Downloaded file %s not found", path1))
}
if !exist(path2) {
t.Fatal(fmt.Sprintf("TestDownloadMany. Downloaded file %s not found", path2))
}
remove(t, path1)
remove(t, path2)
}
func TestFromFile(t *testing.T) {
path1 := masterZIP
path2 := "1206.5538v3.pdf"
createFileWithLinks("simple")
gd := &GoDownload{}
gd.FromFile("simple")
if !exist(path1) {
t.Fatal(fmt.Sprintf("TestFromFile. Downloaded file %s not found", path1))
}
if !exist(path2) {
t.Fatal(fmt.Sprintf("TestFromFile. Downloaded file %s not found", path2))
}
remove(t, path1)
remove(t, path2)
remove(t, "simple")
}
func TestOverwriteGlobal(t *testing.T) {
gd := &GoDownload{Overwrite: true}
gd.Download("https://github.com/saromanov/godownload/archive/master.zip", nil)
path := masterZIP
if !exist(path) {
t.Fatal(fmt.Sprintf("TestOverwriteGlobal. Downloaded file %s not found", path))
}
gd.Download("https://github.com/saromanov/godownload/archive/master.zip", nil)
remove(t, path)
}
func TestArchiveData(t *testing.T) {
gd := &GoDownload{Archive: "zip"}
gd.Download("http://arxiv.org/pdf/1206.5538v3.pdf", nil)
path := "1206.5538v3.pdf.zip"
if !exist(path) {
t.Fatal(fmt.Sprintf("TestArchiveData. Downloaded file %s not found", path))
}
remove(t, path)
}