Browse Source
- 注册 wechat_auth 的热重载:后台改了 AppSecret 不用重启就生效 (正式服补填 AppSecret 后一直报 41004 appsecret missing,直到重启) - AppID / AppSecret 为空时 OnInit 返回 error,启动日志降级为「未配置」,不再报 success - 后台粘贴带的首尾空白自动去掉;defsys 读写加锁 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>main
4 changed files with 113 additions and 6 deletions
@ -0,0 +1,44 @@ |
|||||
|
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("合法配置应替换实例") |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue