111 lines
3.1 KiB
Go
111 lines
3.1 KiB
Go
// webhook_test.go Day 27:企微/钉钉 webhook 消息体与错误分支。
|
|
package notify
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
// TestWebhook_WeChatPayload 企微:markdown 消息体 + errcode 0 成功。
|
|
func TestWebhook_WeChatPayload(t *testing.T) {
|
|
var mu sync.Mutex
|
|
var body map[string]any
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
raw, _ := io.ReadAll(r.Body)
|
|
mu.Lock()
|
|
_ = json.Unmarshal(raw, &body)
|
|
mu.Unlock()
|
|
_, _ = w.Write([]byte(`{"errcode":0,"errmsg":"ok"}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
if err := NewWebhook("wechat", srv.URL).Send(context.Background(), "标题", "内容"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if body["msgtype"] != "markdown" {
|
|
t.Fatalf("msgtype = %v, 期望 markdown", body["msgtype"])
|
|
}
|
|
md := body["markdown"].(map[string]any)
|
|
if !strings.Contains(md["content"].(string), "标题") {
|
|
t.Fatalf("内容缺标题: %v", md)
|
|
}
|
|
}
|
|
|
|
// TestWebhook_DingTalkPayload 钉钉:text 消息体。
|
|
func TestWebhook_DingTalkPayload(t *testing.T) {
|
|
var mu sync.Mutex
|
|
var body map[string]any
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
raw, _ := io.ReadAll(r.Body)
|
|
mu.Lock()
|
|
_ = json.Unmarshal(raw, &body)
|
|
mu.Unlock()
|
|
_, _ = w.Write([]byte(`{"errcode":0}`))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
if err := NewWebhook("dingtalk", srv.URL).Send(context.Background(), "T", "C"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if body["msgtype"] != "text" {
|
|
t.Fatalf("msgtype = %v, 期望 text", body["msgtype"])
|
|
}
|
|
}
|
|
|
|
// TestWebhook_Errors 空 URL / 业务 errcode≠0 / HTTP 5xx。
|
|
func TestWebhook_Errors(t *testing.T) {
|
|
if err := NewWebhook("wechat", "").Send(context.Background(), "t", "c"); err == nil {
|
|
t.Fatal("空 URL 应失败")
|
|
}
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`{"errcode":93000,"errmsg":"invalid webhook"}`))
|
|
}))
|
|
defer srv.Close()
|
|
if err := NewWebhook("wechat", srv.URL).Send(context.Background(), "t", "c"); err == nil {
|
|
t.Fatal("errcode≠0 应失败")
|
|
}
|
|
srv5 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}))
|
|
defer srv5.Close()
|
|
if err := NewWebhook("dingtalk", srv5.URL).Send(context.Background(), "t", "c"); err == nil {
|
|
t.Fatal("5xx 应失败")
|
|
}
|
|
}
|
|
|
|
// fakeN 测试假通知器。
|
|
type fakeN struct {
|
|
err error
|
|
calls int
|
|
}
|
|
|
|
func (f *fakeN) Send(context.Context, string, string) error { f.calls++; return f.err }
|
|
|
|
// TestMulti_PartialSuccess 部分成功即成功;全部失败才报错。
|
|
func TestMulti_PartialSuccess(t *testing.T) {
|
|
ok := &fakeN{}
|
|
bad := &fakeN{err: context.Canceled}
|
|
m1 := Multi{ok, bad}
|
|
if err := m1.Send(context.Background(), "t", "c"); err != nil {
|
|
t.Fatalf("部分成功不应报错: %v", err)
|
|
}
|
|
if ok.calls != 1 {
|
|
t.Fatal("ok 渠道应已发送")
|
|
}
|
|
bad2 := &fakeN{err: context.DeadlineExceeded}
|
|
m2 := Multi{bad, bad2}
|
|
if err := m2.Send(context.Background(), "t", "c"); err == nil {
|
|
t.Fatal("全部失败应报错")
|
|
}
|
|
}
|