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