You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
3.1 KiB
140 lines
3.1 KiB
2 weeks ago
|
package testUnit
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"crypto/md5"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"math/rand"
|
||
|
"mime/multipart"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"testing"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func TestPOST(t *testing.T) {
|
||
|
|
||
|
url := "http://example.com/upload" // 替换为你的目标URL
|
||
|
|
||
|
filePath := "path/to/file.txt" // 替换为你的文件路径
|
||
|
file, err := os.Open(filePath)
|
||
|
if err != nil {
|
||
|
fmt.Println("无法打开文件:", err)
|
||
|
return
|
||
|
}
|
||
|
defer file.Close()
|
||
|
|
||
|
body := &bytes.Buffer{}
|
||
|
writer := multipart.NewWriter(body)
|
||
|
part, err := writer.CreateFormFile("file", filePath)
|
||
|
if err != nil {
|
||
|
fmt.Println("创建表单文件失败:", err)
|
||
|
return
|
||
|
}
|
||
|
_, err = io.Copy(part, file)
|
||
|
if err != nil {
|
||
|
fmt.Println("复制文件内容失败:", err)
|
||
|
return
|
||
|
}
|
||
|
writer.Close()
|
||
|
|
||
|
request, err := http.NewRequest("POST", url, body)
|
||
|
if err != nil {
|
||
|
fmt.Println("创建HTTP请求失败:", err)
|
||
|
return
|
||
|
}
|
||
|
request.Header.Set("Content-Type", writer.FormDataContentType())
|
||
|
|
||
|
client := &http.Client{}
|
||
|
response, err := client.Do(request)
|
||
|
if err != nil {
|
||
|
fmt.Println("执行HTTP请求失败:", err)
|
||
|
return
|
||
|
}
|
||
|
defer response.Body.Close()
|
||
|
|
||
|
fmt.Println("HTTP响应码:", response.StatusCode)
|
||
|
|
||
|
}
|
||
|
|
||
|
const (
|
||
|
KC_RAND_KIND_NUM = 0 // 纯数字
|
||
|
KC_RAND_KIND_LOWER = 1 // 小写字母
|
||
|
KC_RAND_KIND_UPPER = 2 // 大写字母
|
||
|
KC_RAND_KIND_ALL = 3 // 数字、大小写字母
|
||
|
)
|
||
|
|
||
|
// 随机字符串
|
||
|
func Krand(size int, kind int) []byte {
|
||
|
ikind, kinds, result := kind, [][]int{[]int{10, 48}, []int{26, 97}, []int{26, 65}}, make([]byte, size)
|
||
|
is_all := kind > 2 || kind < 0
|
||
|
rand.Seed(time.Now().UnixNano())
|
||
|
for i := 0; i < size; i++ {
|
||
|
if is_all { // random ikind
|
||
|
ikind = rand.Intn(3)
|
||
|
}
|
||
|
scope, base := kinds[ikind][0], kinds[ikind][1]
|
||
|
result[i] = uint8(base + rand.Intn(scope))
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
func TestGzGZMToken(t *testing.T) {
|
||
|
|
||
|
AppKey := "gykj"
|
||
|
AppSecret := "gykj"
|
||
|
|
||
|
randomStr16 := string(Krand(16, KC_RAND_KIND_NUM))
|
||
|
timestamp := time.Now().UnixMilli()
|
||
|
secretRaw := fmt.Sprintf("%s%s%s%d", AppKey, AppSecret, randomStr16, timestamp)
|
||
|
println(secretRaw)
|
||
|
h := md5.New()
|
||
|
|
||
|
io.WriteString(h, secretRaw)
|
||
|
|
||
|
secret := fmt.Sprintf("%x", h.Sum(nil))
|
||
|
println(randomStr16, timestamp, secret)
|
||
|
|
||
|
url := "http://example.com/upload" // 替换为你的目标URL
|
||
|
|
||
|
filePath := "path/to/file.txt" // 替换为你的文件路径
|
||
|
file, err := os.Open(filePath)
|
||
|
if err != nil {
|
||
|
fmt.Println("无法打开文件:", err)
|
||
|
return
|
||
|
}
|
||
|
defer file.Close()
|
||
|
|
||
|
body := &bytes.Buffer{}
|
||
|
writer := multipart.NewWriter(body)
|
||
|
part, err := writer.CreateFormFile("file", filePath)
|
||
|
if err != nil {
|
||
|
fmt.Println("创建表单文件失败:", err)
|
||
|
return
|
||
|
}
|
||
|
_, err = io.Copy(part, file)
|
||
|
if err != nil {
|
||
|
fmt.Println("复制文件内容失败:", err)
|
||
|
return
|
||
|
}
|
||
|
writer.Close()
|
||
|
|
||
|
request, err := http.NewRequest("POST", url, body)
|
||
|
if err != nil {
|
||
|
fmt.Println("创建HTTP请求失败:", err)
|
||
|
return
|
||
|
}
|
||
|
request.Header.Set("Content-Type", writer.FormDataContentType())
|
||
|
|
||
|
client := &http.Client{}
|
||
|
response, err := client.Do(request)
|
||
|
if err != nil {
|
||
|
fmt.Println("执行HTTP请求失败:", err)
|
||
|
return
|
||
|
}
|
||
|
defer response.Body.Close()
|
||
|
|
||
|
fmt.Println("HTTP响应码:", response.StatusCode)
|
||
|
|
||
|
}
|