openapi-helper/douyin/douyin_openapi_test.go
2025-01-21 16:36:20 +08:00

91 lines
2.1 KiB
Go

package douyin
import (
accessToken "gitea.youtukeji.com.cn/youtu/openapi-helper/douyin/access-token"
"github.com/silenceper/wechat/v2/cache"
"testing"
"time"
)
// 声明测试常量
const (
AppId = "tt8b32fd8f14071db707"
AppSecret = "44018e80b1bde34395a52de67ce1e0c37c572d80"
Token = ""
Salt = ""
)
// 声明一个缓存实例
var Cache cache.Cache
// 声明全局openApi实例
var OpenApi *DouYinOpenApi
func init() {
Cache = cache.NewMemory()
OpenApi = NewDouYinOpenApi(DouYinOpenApiConfig{
AppId: AppId,
AppSecret: AppSecret,
IsSandbox: false,
Token: Token,
Salt: Salt,
})
}
// 测试获取新的token
func TestDouyinOpenapi_NewDefaultAccessToken(t *testing.T) {
token := accessToken.NewDefaultAccessToken(AppId, AppSecret, Cache, true)
getAccessToken, err := token.GetAccessToken()
if err != nil {
t.Errorf("got a error: %s", err.Error())
return
}
t.Logf("got a value: %s", getAccessToken)
}
// 基准测试看获取token的次数?
func BenchmarkDouyinOpenapi_NewDefaultAccessToken(b *testing.B) {
token := accessToken.NewDefaultAccessToken(AppId, AppSecret, Cache, true)
for i := 0; i < b.N; i++ {
getAccessToken, err := token.GetAccessToken()
b.Logf("get token: %s %+v", getAccessToken, err)
}
}
// 测试小程序登录
func TestDouYinOpenApi_Code2Session(t *testing.T) {
session, err := OpenApi.Code2Session("1111", "")
if err != nil {
t.Errorf("got a error %s", err.Error())
return
}
t.Logf("got a value %+v", session)
}
func TestDouYinOpenApi_GetEcpm(t *testing.T) {
// 获取动态的 access_token
token := accessToken.NewDefaultAccessToken(AppId, AppSecret, Cache, true)
at, err := token.GetAccessToken()
if err != nil {
t.Errorf("got a error: %s", err.Error())
return
}
list, err := OpenApi.GetEcpm(GetEcpmParams{
AppId: AppId,
OpenId: "_000aG3MQTzVW-yCUH-ndwv90JIyvdTSD0gf",
AccessToken: at,
DateHour: time.Now().Format(time.DateOnly),
PageSize: 500,
PageNo: 1,
})
if err != nil {
t.Errorf("got a error %s", err.Error())
return
}
t.Logf("got a value %+v", list)
}