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.
89 lines
3.1 KiB
89 lines
3.1 KiB
package sms
|
|
|
|
import "testing"
|
|
|
|
// 纯本地逻辑测试(不发网络):号码归属判定、阿里云号码格式、模板选取、配置校验。
|
|
|
|
func TestIsDomestic(t *testing.T) {
|
|
cases := map[string]bool{
|
|
"+8615336758045": true,
|
|
"008615336758045": true,
|
|
"15336758045": true,
|
|
"+852 7027 8175": false,
|
|
"+85270278175": false,
|
|
"0085270278175": false,
|
|
"+14155552671": false,
|
|
}
|
|
for mobile, want := range cases {
|
|
if got := isDomestic(mobile); got != want {
|
|
t.Errorf("isDomestic(%q)=%v, want %v", mobile, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAliMobile(t *testing.T) {
|
|
cases := map[string]string{
|
|
"+8615336758045": "15336758045",
|
|
"008615336758045": "15336758045",
|
|
"153-3675-8045": "15336758045",
|
|
"+85270278175": "0085270278175",
|
|
"0085270278175": "0085270278175",
|
|
}
|
|
for in, want := range cases {
|
|
if got := aliMobile(in); got != want {
|
|
t.Errorf("aliMobile(%q)=%q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPickTemplate(t *testing.T) {
|
|
o := Options{Template1: "domestic", Template2: "oversea"}
|
|
if got, _ := pickTemplate(o, "+8615336758045"); got != "domestic" {
|
|
t.Errorf("国内号应选 Template1,得到 %q", got)
|
|
}
|
|
if got, _ := pickTemplate(o, "+85270278175"); got != "oversea" {
|
|
t.Errorf("国际号应选 Template2,得到 %q", got)
|
|
}
|
|
// 只配了国内模板时,国际号回退到国内模板而不是发一条没有模板的空请求。
|
|
only := Options{Template1: "domestic"}
|
|
if got, _ := pickTemplate(only, "+85270278175"); got != "domestic" {
|
|
t.Errorf("缺国际模板应回退 Template1,得到 %q", got)
|
|
}
|
|
if _, err := pickTemplate(Options{}, "15336758045"); err == nil {
|
|
t.Error("两个模板都没配时应报错")
|
|
}
|
|
}
|
|
|
|
func TestOptionsValidateAndDefaults(t *testing.T) {
|
|
// Provider 留空 = 腾讯云(兼容加入阿里云之前的配置)。
|
|
o, err := newOptionsChecked(map[string]interface{}{"SecretId": "id", "SecretKey": "key"})
|
|
if err != nil {
|
|
t.Fatalf("腾讯云旧配置应通过校验: %v", err)
|
|
}
|
|
if o.Provider != ProviderTencent {
|
|
t.Errorf("Provider 留空应归一为 %s,得到 %q", ProviderTencent, o.Provider)
|
|
}
|
|
|
|
// 阿里云:缺 AccessKey 应报错,而不是等到发送时才失败。
|
|
if _, err := newOptionsChecked(map[string]interface{}{"Provider": "aliyun", "SignName": "签名"}); err == nil {
|
|
t.Error("阿里云缺 AccessKeyId 应校验失败")
|
|
}
|
|
|
|
// 阿里云:地域与模板变量名留空时填默认值。
|
|
o, err = newOptionsChecked(map[string]interface{}{
|
|
"Provider": "Aliyun", "AccessKeyId": "ak", "AccessKeySecret": "sk", "SignName": "签名",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("阿里云配置应通过校验: %v", err)
|
|
}
|
|
if o.Provider != ProviderAliyun {
|
|
t.Errorf("Provider 应忽略大小写归一为 %s,得到 %q", ProviderAliyun, o.Provider)
|
|
}
|
|
if o.RegionId != defaultAliRegionId || o.TemplateParamName != defaultAliTemplateParam {
|
|
t.Errorf("阿里云默认值未填充: RegionId=%q TemplateParamName=%q", o.RegionId, o.TemplateParamName)
|
|
}
|
|
|
|
if _, err := newOptionsChecked(map[string]interface{}{"Provider": "huawei"}); err == nil {
|
|
t.Error("未知服务商应校验失败")
|
|
}
|
|
}
|
|
|