feat: 增加测试并使通过

This commit is contained in:
tiglog 2023-09-20 23:55:59 +08:00
parent 0d4b189d3b
commit 2b45e952fc
2 changed files with 68 additions and 33 deletions

View File

@ -14,43 +14,20 @@ import (
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
"strings"
) )
// vals := url.Values{}
// vals.Add("name","zhaofan")
// vals.Add("age","22")
func RequestPostForm(url string, vals url.Values) ([]byte, error) {
resp, err := http.PostForm(url, vals)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func RequestPostJson(url string, data map[string]any) ([]byte, error) {
bd, err := json.Marshal(data)
if err != nil {
return nil, err
}
resp, err := http.Post(url, "application/json", bytes.NewReader(bd))
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
// 比较完整的 get 实现 // 比较完整的 get 实现
func RequestGet(uri string, vals url.Values, headers map[string]string) ([]byte, error) { func RequestGet(uri string, vals url.Values, headers map[string]string) ([]byte, error) {
req, err := http.NewRequest("GET", uri, strings.NewReader(vals.Encode())) furl, err := url.Parse(uri)
if err != nil {
return nil, err
}
if furl.RawQuery != "" {
furl.RawQuery += "&" + vals.Encode()
} else {
furl.RawQuery = vals.Encode()
}
req, err := http.NewRequest("GET", furl.String(), nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -62,6 +39,7 @@ func RequestGet(uri string, vals url.Values, headers map[string]string) ([]byte,
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -0,0 +1,57 @@
//
// http_helper_test.go
// Copyright (C) 2023 tiglog <me@tiglog.com>
//
// Distributed under terms of the MIT license.
//
package helper_test
import (
"fmt"
"net/url"
"testing"
"git.hexq.cn/tiglog/golib/gtest"
"git.hexq.cn/tiglog/golib/helper"
)
func TestRequestGet(t *testing.T) {
headers := map[string]string{
"Authorization": "Bearer foo",
}
uri := "http://tapp.cmoodle.com/api/query?aa=11"
vals := url.Values{}
vals.Set("foo", "bar")
vals.Set("hello", "世界")
res, err := helper.RequestGet(uri, vals, headers)
gtest.NoError(t, err)
gtest.NotEqual(t, "{}", string(res))
}
func TestRequestPost(t *testing.T) {
headers := map[string]string{
"Authorization": "Bearer foo",
"Content-Type": "application/json",
}
uri := "http://tapp.cmoodle.com/api/post/data?aa=11"
data := map[string]any{}
data["foo"] = "bar"
data["hello"] = "🌐世界1"
res, err := helper.RequestPost(uri, data, headers)
gtest.NoError(t, err)
fmt.Println(string(res))
headers = map[string]string{
"Authorization": "Bearer foo",
"Content-Type": "application/x-www-form-urlencoded",
}
uri = "http://tapp.cmoodle.com/api/post/form?aa=11"
data = map[string]any{}
data["foo"] = "bar"
data["hello"] = "🌐世界2"
res2, err := helper.RequestPost(uri, data, headers)
gtest.NoError(t, err)
fmt.Println(string(res2))
}