-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathpget_test.go
138 lines (112 loc) · 2.88 KB
/
pget_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
package pget
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
stdout = ioutil.Discard
os.Exit(m.Run())
}
func TestPget(t *testing.T) {
// listening file server
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/moo", http.StatusFound)
})
mux.HandleFunc("/moo", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/mooo", http.StatusFound)
})
mux.HandleFunc("/mooo", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/test.tar.gz", http.StatusFound)
})
mux.HandleFunc("/test.tar.gz", func(w http.ResponseWriter, r *http.Request) {
fp := "_testdata/test.tar.gz"
data, err := ioutil.ReadFile(fp)
if err != nil {
t.Errorf("failed to readfile: %s", err)
}
http.ServeContent(w, r, fp, time.Now(), bytes.NewReader(data))
})
ts := httptest.NewServer(mux)
defer ts.Close()
// begin tests
url := ts.URL
tmpdir := t.TempDir()
cfg := &DownloadConfig{
Filename: "test.tar.gz",
ContentLength: 1719652,
Dirname: tmpdir,
Procs: 4,
URLs: []string{ts.URL},
Client: newDownloadClient(1),
}
t.Run("check", func(t *testing.T) {
target, err := Check(context.Background(), &CheckConfig{
URLs: []string{url},
Timeout: 10 * time.Second,
})
if err != nil {
t.Fatalf("failed to check header: %s", err)
}
if len(target.URLs) == 0 {
t.Fatalf("invalid URL length %d", len(target.URLs))
}
// could redirect?
assert.NotEqual(t, target.URLs[0], url, "failed to get of the last url in the redirect")
})
t.Run("download", func(t *testing.T) {
err := Download(context.Background(), cfg)
if err != nil {
t.Fatal(err)
}
// check of the file to exists
for i := 0; i < cfg.Procs; i++ {
filename := filepath.Join(tmpdir, "_test.tar.gz.4", fmt.Sprintf("test.tar.gz.2.%d", i))
_, err := os.Stat(filename)
if err == nil {
t.Errorf("%q does not exist: %v", filename, err)
}
}
cmpFileChecksum(t, "_testdata/test.tar.gz", filepath.Join(tmpdir, cfg.Filename))
})
}
func get2md5(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
hash := md5.New()
if _, err := io.Copy(hash, f); err != nil {
return "", err
}
// get the 16 bytes hash
bytes := hash.Sum(nil)[:16]
return hex.EncodeToString(bytes), nil
}
func cmpFileChecksum(t *testing.T, wantPath, gotPath string) {
t.Helper()
want, err := get2md5(wantPath)
if err != nil {
t.Fatalf("failed to md5sum of original file: %s", err)
}
resultfp, err := get2md5(gotPath)
if err != nil {
t.Fatalf("failed to md5sum of result file: %s", err)
}
if want != resultfp {
t.Errorf("expected %s got %s", want, resultfp)
}
}