golib/helper/http_helper_test.go

58 lines
1.3 KiB
Go

//
// 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))
}