You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

44 lines
1.3 KiB

package wechat_auth
import "testing"
// 空 AppSecret 必须判失败:以前 OnInit 照单全收、日志报 success,线上每次登录都被微信回 41004。
func TestReloadRejectsMissingAndKeepsOld(t *testing.T) {
set(nil)
defer set(nil)
if err := OnInit(map[string]interface{}{"AppID": "wx1", "AppSecret": ""}); err == nil {
t.Fatal("AppSecret 为空时 OnInit 应返回 error")
}
if get() != nil {
t.Fatal("OnInit 失败不应留下实例")
}
if err := Reload(map[string]interface{}{"AppID": " wx1 ", "AppSecret": " s1\n"}); err != nil {
t.Fatalf("合法配置 Reload 失败: %v", err)
}
old := get().(*WeChat)
if old.options.AppID != "wx1" || old.options.AppSecret != "s1" {
t.Fatalf("首尾空白未去掉: %+v", old.options)
}
for _, cfg := range []map[string]interface{}{
{"AppID": "wx2", "AppSecret": ""},
{"AppID": "", "AppSecret": "s2"},
{"AppID": "wx2", "AppSecret": " "},
} {
if err := Reload(cfg); err == nil {
t.Fatalf("非法配置 %v 应返回 error", cfg)
}
if get() != old {
t.Fatalf("非法配置 %v 不应替换旧实例", cfg)
}
}
if err := Reload(map[string]interface{}{"AppID": "wx3", "AppSecret": "s3"}); err != nil {
t.Fatal(err)
}
if get().(*WeChat).options.AppID != "wx3" {
t.Fatal("合法配置应替换实例")
}
}