-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrawhttp_test.go
86 lines (81 loc) · 1.77 KB
/
rawhttp_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
package rawhttp
import (
"bufio"
"fmt"
"io"
"net/http"
"os"
"testing"
)
func TestGetSource(t *testing.T) {
url := "https://sourceforge.net/projects/nsis/files/NSIS%203/3.10/nsis-3.10-src.tar.bz2"
client := NewClient(DefaultOptions)
resp, err := client.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
bufRead(resp, "nsis-3.10-src.tar.bz2")
}
func TestGetTxtSource(t *testing.T) {
url := "https://sourceforge.net/projects/liblcl/files/v2.3.7/md5.txt"
client := NewClient(DefaultOptions)
resp, err := client.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
bufRead(resp, "md5.txt")
}
func TestProxyGetSource(t *testing.T) {
url := "https://sourceforge.net/projects/liblcl/files/v2.3.8/liblcl-109.Windows64.zip"
options := *DefaultOptions
options.Proxy = "http://127.0.0.1:10809"
client := NewClient(&options)
resp, err := client.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
bufRead(resp, "liblcl-109.Windows64.zip")
}
func bufRead(resp *http.Response, saveName string) {
if resp.Body == nil {
return
}
file, err := os.Create(saveName)
if err != nil {
return
}
defer file.Close()
read := bufio.NewReader(resp.Body)
var (
fSize = resp.ContentLength // strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 32)
buf = make([]byte, 1024*10)
written int64
nw int
)
var callback = func(totalLength, processLength int64) {
fmt.Println("totalLength:", totalLength, "processLength:", processLength)
}
for {
nr, er := read.Read(buf)
if nr > 0 {
nw, err = file.Write(buf[0:nr])
if nw > 0 {
written += int64(nw)
}
callback(fSize, written)
if err != nil || nr != nw {
err = io.ErrShortWrite
break
}
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
}