douyin-openapi/douyin_openapi_test.go

63 lines
1.4 KiB
Go
Raw Normal View History

2025-01-13 22:58:50 +08:00
package douyin_openapi
import (
accessToken "gitea.youtukeji.com.cn/xiabin/douyin-openapi/access-token"
"gitea.youtukeji.com.cn/xiabin/douyin-openapi/cache"
"testing"
)
// 声明测试常量
const (
AppId = ""
AppSecret = ""
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)
}