Read this in other languages: English, 中文.
os.MkdirAll(testTempDirPath, 0666)
savePath := filepath.Join(testTempDirPath, "testFileOffsetWriter")
file, err := os.OpenFile(savePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
t.Error(err)
}
var mutex sync.Mutex
writer := NewOffsetWriter(file, &mutex, 0, 1024)
writer.Write([]byte("1234567890"))
writer.ResetOffset()
writer.Write([]byte("1234567890"))
file.Close()
bs, err := os.ReadFile(savePath)
if err != nil {
t.Error(err)
}
if string(bs) != "1234567890" {
t.Error(string(bs))
}
os.MkdirAll(testTempDirPath, 0666)
dialer := bwlimit.NewDialer()
dialer.RxBwLimit().SetBwLimit(20 * 1024 * 1024)
hc := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
downloader := &HttpDownloader{
HttpClient: hc,
ConBlockChan: make(chan struct{}, 10),
BlockSize: 1024 * 1024,
RetryCnt: 1,
Headers: http.Header{"TestKey": []string{"TestValue"}},
}
savePath := filepath.Join(testTempDirPath, "vc_redist")
url := "https://aka.ms/vs/17/release/vc_redist.arm64.exe"
resp, err := http.Head(url)
if err != nil {
t.Error(err)
}
if resp != nil {
resp.Body.Close()
}
contentLen := resp.Header.Get("Content-Length")
fileSize, _ := strconv.ParseInt(contentLen, 10, 64)
err = downloader.Download(context.Background(), url, http.Header{
"User-Agent": []string{"Mozilla/5.0 (Linux; Android 10; Pixel 4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Mobile Safari/537.36"},
}, savePath)
if err != nil {
t.Error(err)
}
fileInfo, err := os.Stat(savePath)
if err != nil {
t.Error(err)
}
if fileInfo.Size() != fileSize {
t.Error(fileSize)
}
clientX := getHcx()
for i := 0; i < 3; i++ {
resp, err := clientX.Get("http://127.0.0.1:" + testHttpxPortH1 + "/goutils/httpx")
if err != nil {
t.Error(fmt.Errorf("error making request: %v", err))
}
if resp.StatusCode >= 400 {
t.Error(resp.StatusCode)
}
if resp.Proto != "HTTP/1.1" {
t.Error(resp.Proto)
}
}
for i := 0; i < 3; i++ {
resp, err := clientX.Get("http://127.0.0.1:" + testHttpxPortH2 + "/goutils/httpx")
if err != nil {
t.Error(fmt.Errorf("error making request: %v", err))
}
if resp.StatusCode >= 400 {
t.Error(resp.StatusCode)
}
if resp.Proto != "HTTP/2.0" {
t.Error(resp.Proto)
}
}
clientX := getHcx()
for i := 0; i < 3; i++ {
resp, err := clientX.Post("http://127.0.0.1:"+testHttpxPortH1+"/goutils/httpx", "application/json", strings.NewReader(""))
if err != nil {
t.Error(fmt.Errorf("error making request: %v", err))
}
if resp.StatusCode >= 400 {
t.Error(resp.StatusCode)
}
if resp.Proto != "HTTP/1.1" {
t.Error(resp.Proto)
}
}
for i := 0; i < 3; i++ {
resp, err := clientX.Post("http://127.0.0.1:"+testHttpxPortH2+"/goutils/httpx", "application/json", strings.NewReader(""))
if err != nil {
t.Error(fmt.Errorf("error making request: %v", err))
}
if resp.StatusCode >= 400 {
t.Error(resp.StatusCode)
}
if resp.Proto != "HTTP/2.0" {
t.Error(resp.Proto)
}
}
clientX := getHcx()
for i := 0; i < 3; i++ {
req, _ := http.NewRequest("POST", "http://127.0.0.1:"+testHttpxPortH1+"/goutils/httpx", strings.NewReader(""))
req.Header.Set("Content-Type", "application/json")
resp, err := clientX.Do(req)
if err != nil {
t.Error(fmt.Errorf("error making request: %v", err))
}
if resp.StatusCode >= 400 {
t.Error(resp.StatusCode)
}
if resp.Proto != "HTTP/1.1" {
t.Error(resp.Proto)
}
}
for i := 0; i < 3; i++ {
req, _ := http.NewRequest("POST", "http://127.0.0.1:"+testHttpxPortH2+"/goutils/httpx", strings.NewReader(""))
req.Header.Set("Content-Type", "application/json")
resp, err := clientX.Do(req)
if err != nil {
t.Error(fmt.Errorf("error making request: %v", err))
}
if resp.StatusCode >= 400 {
t.Error(resp.StatusCode)
}
if resp.Proto != "HTTP/2.0" {
t.Error(resp.Proto)
}
}
clientX := getHcx()
for i := 0; i < 3; i++ {
resp, err := clientX.Head("http://127.0.0.1:" + testHttpxPortH1 + "/goutils/httpx")
if err != nil {
t.Error(fmt.Errorf("error making request: %v", err))
}
if resp.StatusCode >= 400 {
t.Error(resp.StatusCode)
}
if resp.Proto != "HTTP/1.1" {
t.Error(resp.Proto)
}
}
for i := 0; i < 3; i++ {
resp, err := clientX.Head("http://127.0.0.1:" + testHttpxPortH2 + "/goutils/httpx")
if err != nil {
t.Error(fmt.Errorf("error making request: %v", err))
}
if resp.StatusCode >= 400 {
t.Error(resp.StatusCode)
}
if resp.Proto != "HTTP/2.0" {
t.Error(resp.Proto)
}
}
clientX := getHcx()
for i := 0; i < 3; i++ {
resp, err := clientX.PostForm("http://127.0.0.1:"+testHttpxPortH1+"/goutils/httpx", url.Values{"key": []string{"value"}})
if err != nil {
t.Error(fmt.Errorf("error making request: %v", err))
}
if resp.StatusCode >= 400 {
t.Error(resp.StatusCode)
}
if resp.Proto != "HTTP/1.1" {
t.Error(resp.Proto)
}
}
for i := 0; i < 3; i++ {
resp, err := clientX.PostForm("http://127.0.0.1:"+testHttpxPortH2+"/goutils/httpx", url.Values{"key": []string{"value"}})
if err != nil {
t.Error(fmt.Errorf("error making request: %v", err))
}
if resp.StatusCode >= 400 {
t.Error(resp.StatusCode)
}
if resp.Proto != "HTTP/2.0" {
t.Error(resp.Proto)
}
}
clientX := getHcx()
_, err := clientX.Get("file://127.0.0.1:" + testHttpxPortH1 + "/goutils/httpx")
if err == nil {
t.FailNow()
}
_, err = clientX.Post("file://127.0.0.1:"+testHttpxPortH1+"/goutils/httpx", "application/json", strings.NewReader(""))
if err == nil {
t.FailNow()
}
req, _ := http.NewRequest("POST", "file://127.0.0.1:"+testHttpxPortH1+"/goutils/httpx", strings.NewReader(""))
req.Header.Set("Content-Type", "application/json")
_, err = clientX.Do(req)
if err == nil {
t.FailNow()
}
_, err = clientX.Head("file://127.0.0.1:" + testHttpxPortH1 + "/goutils/httpx")
if err == nil {
t.FailNow()
}
clientX := getHcx()
_, err := clientX.Get("file://127.0.0.1:" + testHttpxPortH2 + "/goutils/httpx")
if err == nil {
t.FailNow()
}
_, err = clientX.Post("file://127.0.0.1:"+testHttpxPortH2+"/goutils/httpx", "application/json", strings.NewReader(""))
if err == nil {
t.FailNow()
}
req, _ := http.NewRequest("POST", "file://127.0.0.1:"+testHttpxPortH2+"/goutils/httpx", strings.NewReader(""))
req.Header.Set("Content-Type", "application/json")
_, err = clientX.Do(req)
if err == nil {
t.FailNow()
}
_, err = clientX.Head("file://127.0.0.1:" + testHttpxPortH2 + "/goutils/httpx")
if err == nil {
t.FailNow()
}
client := getSshClient(t)
defer client.Close()
session, err := client.NewSession()
if err != nil {
t.Fatalf("Create session failed %v", err)
}
defer session.Close()
// run command and capture stdout/stderr
output, err := session.CombinedOutput("ls -l /data")
if err != nil {
t.Fatalf("CombinedOutput failed %v", err)
}
t.Log(string(output))
client := getSshClient(t)
defer client.Close()
//test时候,打开,会引入mysql包
//mysql.RegisterDialContext("tcp", func(ctx context.Context, addr string) (net.Conn, error) {
// return client.Dial("tcp", addr)
//})
db, err := sql.Open("", "")
if err != nil {
t.Fatalf("open db failed %v", err)
}
defer db.Close()
rs, err := db.Query("select limit 10")
if err != nil {
t.Fatalf("open db failed %v", err)
}
defer rs.Close()
for rs.Next() {
}