plugins/utils/request/request_test.go

82 lines
1.8 KiB
Go

package request
import (
"context"
"fmt"
"net/http"
"net/url"
"testing"
"time"
)
func Test_Get(t *testing.T) {
uri := "https://gateway.dev.cdlsxd.cn/adminyx/admin/v1/key_batch/list"
headers := map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
}
uv := url.Values{}
uv.Set("page", "1")
uv.Set("limit", "2")
respBody, respHeader, err := Get(context.Background(), uri+"?"+uv.Encode(), WithHeaders(headers))
if err != nil {
fmt.Println(err)
return
}
fmt.Println("响应体:", string(respBody))
fmt.Println("响应头:", respHeader)
}
func Test_Request(t *testing.T) {
url := "https://gateway.dev.cdlsxd.cn/adminyx/admin/v1/key_batch/get_zip_url/109"
respBody, respHeader, err := Get(context.Background(), url)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("响应体:", string(respBody))
fmt.Println("响应头:", respHeader)
}
func Test_RequestHeaders(t *testing.T) {
url := "http://example.com/api"
body := []byte("request body")
headers := map[string]string{
"Content-Type": "application/json",
"Authorization": "Bearer token",
}
respBody, respHeader, err := Post(context.Background(), url, body, WithTimeout(10*time.Second), WithHeaders(headers))
if err != nil {
fmt.Println(err)
return
}
fmt.Println("响应体:", string(respBody))
fmt.Println("响应头:", respHeader)
}
func Test_RequestStatusCode(t *testing.T) {
url := "http://example.com/api/update"
body := []byte("update data")
isSuccess := func(code int) bool {
return code == http.StatusOK || code == http.StatusCreated
}
respBody, respHeader, err := Put(context.Background(), url, body, WithStatusCodeFunc(isSuccess))
if err != nil {
fmt.Println(err)
return
}
fmt.Println("响应体:", string(respBody))
fmt.Println("响应头:", respHeader)
}