base-golang/pkg/downloader/downloader_test.go
2024-07-31 16:49:14 +08:00

46 lines
1.2 KiB
Go

package downloader
import (
"fmt"
"gitea.bvbej.com/bvbej/base-golang/pkg/downloader/base"
"gitea.bvbej.com/bvbej/base-golang/pkg/downloader/controller"
"gitea.bvbej.com/bvbej/base-golang/tool"
"golang.org/x/net/proxy"
"net/http"
"net/url"
"runtime"
"testing"
"time"
)
func TestNewDownloader(t *testing.T) {
dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:1080", nil, proxy.Direct)
parse, _ := url.Parse(`socks5://127.0.0.1:1080`)
if err != nil {
t.Fatal(err, dialer)
}
err = <-New(
controller.WithDialer(dialer), // todo: use dialer proxy
controller.WithCookie(nil),
controller.WithProxy(http.ProxyURL(parse)), // todo: use http client proxy
controller.WithTimeout(time.Second*3),
).URL("http://10.0.1.34/com.tencent.tmgp.jxqy.apk").
Listener(func(event *Event) {
if event.Key == EventKeyFinally {
fmt.Println("下载完成!")
}
if event.Key == EventKeyProgress {
fmt.Printf("下载速度:%s/s 已下载:%s 已用时:%s \n",
tool.ByteFmt(event.Task.Progress.Speed),
tool.ByteFmt(event.Task.Progress.Downloaded),
time.Duration(event.Task.Progress.Used),
)
}
}).
Create(&base.Options{
Connections: runtime.NumCPU(),
})
t.Log(err)
}