-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprogress_test.go
69 lines (58 loc) · 1.15 KB
/
progress_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
// Copyright 2015 Bowery, Inc.
package progress
import (
"bytes"
"net/http"
"os"
"testing"
)
func TestCopyFileSuccess(t *testing.T) {
file, err := os.Open("progress_test.go")
if err != nil {
t.Fatal(err)
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
progChan, errChan := Copy(&buf, file, stat.Size())
for {
select {
case status := <-progChan:
if status.IsFinished() {
// Actual testing occurs here.
if int64(buf.Len()) != stat.Size() {
t.Error("Copied length does not match file length")
}
return
}
case err := <-errChan:
t.Error(err)
}
}
}
func TestCopyGetRequestSuccess(t *testing.T) {
res, err := http.Get("http://bowery.io")
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
var buf bytes.Buffer
progChan, errChan := Copy(&buf, res.Body, res.ContentLength)
for {
select {
case status := <-progChan:
if status.IsFinished() {
// Actual testing occurs here.
if int64(buf.Len()) != res.ContentLength {
t.Error("Copied length does not match file length")
}
return
}
case err := <-errChan:
t.Error(err)
}
}
}