85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
|
package sms
|
|||
|
|
|||
|
import (
|
|||
|
"encoding/json"
|
|||
|
"fmt"
|
|||
|
"net/http"
|
|||
|
"net/http/httptest"
|
|||
|
"testing"
|
|||
|
)
|
|||
|
|
|||
|
// ---------------- 模拟服务 ----------------
|
|||
|
|
|||
|
// mockServer 返回一个 httptest.Server,用于模拟API响应
|
|||
|
func mockServer(t *testing.T, path string, response interface{}) *httptest.Server {
|
|||
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|||
|
if r.URL.Path != path {
|
|||
|
t.Errorf("unexpected path: got %s, want %s", r.URL.Path, path)
|
|||
|
}
|
|||
|
_ = json.NewEncoder(w).Encode(response)
|
|||
|
}))
|
|||
|
}
|
|||
|
|
|||
|
// ---------------- 测试发送短信 ----------------
|
|||
|
|
|||
|
func TestSendSMS(t *testing.T) {
|
|||
|
client := NewClient()
|
|||
|
|
|||
|
content := "【明慧科技】提醒:您的go2ns租卡会员时长仅剩余一个月,现在续费最高立减200元!赶快进入小程序领取优惠吧!"
|
|||
|
returnUrl := "https://telecom.2016js.com/api/v1/sohan/notice"
|
|||
|
body := BusinessBody{
|
|||
|
Content: &content,
|
|||
|
SendList: []SendList{
|
|||
|
{
|
|||
|
OutOrderID: "ORDER777",
|
|||
|
PhoneNumber: "15019230751"},
|
|||
|
},
|
|||
|
ReturnURL: &returnUrl,
|
|||
|
}
|
|||
|
|
|||
|
resp, err := client.SendSMS(body)
|
|||
|
if err != nil {
|
|||
|
t.Fatalf("SendSMS error: %v", err)
|
|||
|
}
|
|||
|
|
|||
|
fmt.Printf("response: %+v", resp)
|
|||
|
|
|||
|
if resp.StatusCode != 1 {
|
|||
|
t.Errorf("unexpected response: %+v", resp)
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// ---------------- 测试查询余额 ----------------
|
|||
|
|
|||
|
func TestGetBalance(t *testing.T) {
|
|||
|
client := NewClient()
|
|||
|
|
|||
|
resp, err := client.GetBalance()
|
|||
|
if err != nil {
|
|||
|
t.Fatalf("GetBalance error: %v", err)
|
|||
|
}
|
|||
|
|
|||
|
fmt.Printf("response: %+v", resp)
|
|||
|
|
|||
|
if resp.StatusCode != 1 {
|
|||
|
t.Errorf("unexpected response: %+v", resp)
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// ---------------- 测试查询状态 ----------------
|
|||
|
|
|||
|
func TestQueryState(t *testing.T) {
|
|||
|
client := NewClient()
|
|||
|
|
|||
|
resp, err := client.QueryState("ORDER123", "15019230751")
|
|||
|
if err != nil {
|
|||
|
t.Fatalf("QueryState error: %v", err)
|
|||
|
}
|
|||
|
|
|||
|
fmt.Printf("response: %+v", resp)
|
|||
|
|
|||
|
if resp.StatusCode != 1 {
|
|||
|
t.Errorf("unexpected sendStatus: %s", resp.Data.SendStatus)
|
|||
|
}
|
|||
|
}
|