mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 04:30:05 +00:00
update: 增加更多占位符与单元测试
This commit is contained in:
@@ -157,14 +157,42 @@ func (ns *NotificationServerBundle) Send(message string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// replaceParamInString 替换字符串中的占位符
|
||||
func replaceParamsInString(s *Server, str string, message string, mod func(string) string) string {
|
||||
if mod != nil {
|
||||
str = strings.ReplaceAll(str, "#NEZHA#", mod(message))
|
||||
if s != nil {
|
||||
str = strings.ReplaceAll(str, "#SERVER.NAME#", mod(s.Name))
|
||||
str = strings.ReplaceAll(str, "#SERVER.IP#", mod(s.Host.IP))
|
||||
str = strings.ReplaceAll(str, "#SERVER.CPU#", mod(fmt.Sprintf("%f", s.State.CPU)))
|
||||
str = strings.ReplaceAll(str, "#SERVER.MEM#", mod(fmt.Sprintf("%d", s.State.MemUsed)))
|
||||
str = strings.ReplaceAll(str, "#SERVER.SWAP#", mod(fmt.Sprintf("%d", s.State.SwapUsed)))
|
||||
str = strings.ReplaceAll(str, "#SERVER.DISK#", mod(fmt.Sprintf("%d", s.State.DiskUsed)))
|
||||
str = strings.ReplaceAll(str, "#SERVER.NETINSPEED#", mod(fmt.Sprintf("%d", s.State.NetInSpeed)))
|
||||
str = strings.ReplaceAll(str, "#SERVER.NETOUTSPEED#", mod(fmt.Sprintf("%d", s.State.NetOutSpeed)))
|
||||
str = strings.ReplaceAll(str, "#SERVER.TRANSFERIN#", mod(fmt.Sprintf("%d", s.State.NetInTransfer)))
|
||||
str = strings.ReplaceAll(str, "#SERVER.TRANSFEROUT#", mod(fmt.Sprintf("%d", s.State.NetOutTransfer)))
|
||||
str = strings.ReplaceAll(str, "#SERVER.LOAD1#", mod(fmt.Sprintf("%f", s.State.Load1)))
|
||||
str = strings.ReplaceAll(str, "#SERVER.LOAD5#", mod(fmt.Sprintf("%f", s.State.Load5)))
|
||||
str = strings.ReplaceAll(str, "#SERVER.LOAD15#", mod(fmt.Sprintf("%f", s.State.Load15)))
|
||||
}
|
||||
} else {
|
||||
str = strings.ReplaceAll(str, "#NEZHA#", message)
|
||||
}
|
||||
if s != nil {
|
||||
str = strings.ReplaceAll(str, "#SERVER#", s.Name)
|
||||
if s != nil {
|
||||
str = strings.ReplaceAll(str, "#SERVER.NAME#", s.Name)
|
||||
str = strings.ReplaceAll(str, "#SERVER.IP#", s.Host.IP)
|
||||
str = strings.ReplaceAll(str, "#SERVER.CPU#", fmt.Sprintf("%f", s.State.CPU))
|
||||
str = strings.ReplaceAll(str, "#SERVER.MEM#", fmt.Sprintf("%d", s.State.MemUsed))
|
||||
str = strings.ReplaceAll(str, "#SERVER.SWAP#", fmt.Sprintf("%d", s.State.SwapUsed))
|
||||
str = strings.ReplaceAll(str, "#SERVER.DISK#", fmt.Sprintf("%d", s.State.DiskUsed))
|
||||
str = strings.ReplaceAll(str, "#SERVER.NETINSPEED#", fmt.Sprintf("%d", s.State.NetInSpeed))
|
||||
str = strings.ReplaceAll(str, "#SERVER.NETOUTSPEED#", fmt.Sprintf("%d", s.State.NetOutSpeed))
|
||||
str = strings.ReplaceAll(str, "#SERVER.TRANSFERIN#", fmt.Sprintf("%d", s.State.NetInTransfer))
|
||||
str = strings.ReplaceAll(str, "#SERVER.TRANSFEROUT#", fmt.Sprintf("%d", s.State.NetOutTransfer))
|
||||
str = strings.ReplaceAll(str, "#SERVER.LOAD1#", fmt.Sprintf("%f", s.State.Load1))
|
||||
str = strings.ReplaceAll(str, "#SERVER.LOAD5#", fmt.Sprintf("%f", s.State.Load5))
|
||||
str = strings.ReplaceAll(str, "#SERVER.LOAD15#", fmt.Sprintf("%f", s.State.Load15))
|
||||
}
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
@@ -37,14 +37,43 @@ func execCase(t *testing.T, item testSt) {
|
||||
RequestHeader: item.header,
|
||||
}
|
||||
server := Server{
|
||||
Common: Common{},
|
||||
Name: "ServerName",
|
||||
Tag: "",
|
||||
Secret: "",
|
||||
Note: "",
|
||||
DisplayIndex: 0,
|
||||
Host: nil,
|
||||
State: nil,
|
||||
Common: Common{},
|
||||
Name: "ServerName",
|
||||
Tag: "",
|
||||
Secret: "",
|
||||
Note: "",
|
||||
DisplayIndex: 0,
|
||||
Host: &Host{
|
||||
Platform: "",
|
||||
PlatformVersion: "",
|
||||
CPU: nil,
|
||||
MemTotal: 0,
|
||||
DiskTotal: 0,
|
||||
SwapTotal: 0,
|
||||
Arch: "",
|
||||
Virtualization: "",
|
||||
BootTime: 0,
|
||||
IP: "1.1.1.1",
|
||||
CountryCode: "",
|
||||
Version: "",
|
||||
},
|
||||
State: &HostState{
|
||||
CPU: 0,
|
||||
MemUsed: 0,
|
||||
SwapUsed: 8888,
|
||||
DiskUsed: 0,
|
||||
NetInTransfer: 0,
|
||||
NetOutTransfer: 0,
|
||||
NetInSpeed: 0,
|
||||
NetOutSpeed: 0,
|
||||
Uptime: 0,
|
||||
Load1: 0,
|
||||
Load5: 0,
|
||||
Load15: 0,
|
||||
TcpConnCount: 0,
|
||||
UdpConnCount: 0,
|
||||
ProcessCount: 0,
|
||||
},
|
||||
LastActive: time.Time{},
|
||||
TaskClose: nil,
|
||||
TaskStream: nil,
|
||||
@@ -139,16 +168,26 @@ func TestNotification(t *testing.T) {
|
||||
},
|
||||
{
|
||||
url: "https://example.com/?m=#NEZHA#",
|
||||
body: `{"Server":"#SERVER#"}`,
|
||||
body: `{"Server":"#SERVER.NAME#","ServerIP":"#SERVER.IP#","ServerSWAP":#SERVER.SWAP#}`,
|
||||
reqMethod: NotificationRequestMethodPOST,
|
||||
header: `{"asd":"dsa11"}`,
|
||||
reqType: NotificationRequestTypeJSON,
|
||||
expectURL: "https://example.com/?m=" + msg,
|
||||
expectMethod: http.MethodPost,
|
||||
expectContentType: reqTypeJSON,
|
||||
expectBody: `{"Server":"ServerName"}`,
|
||||
expectBody: `{"Server":"ServerName","ServerIP":"1.1.1.1","ServerSWAP":8888}`,
|
||||
expectHeader: map[string]string{"asd": "dsa11"},
|
||||
},
|
||||
{
|
||||
url: "https://example.com/?m=#NEZHA#",
|
||||
body: `{"#NEZHA#":"#NEZHA#","Server":"#SERVER.NAME#","ServerIP":"#SERVER.IP#","ServerSWAP":"#SERVER.SWAP#"}`,
|
||||
reqMethod: NotificationRequestMethodPOST,
|
||||
reqType: NotificationRequestTypeForm,
|
||||
expectURL: "https://example.com/?m=" + msg,
|
||||
expectMethod: http.MethodPost,
|
||||
expectContentType: reqTypeForm,
|
||||
expectBody: "%23NEZHA%23=" + msg + "&Server=ServerName&ServerIP=1.1.1.1&ServerSWAP=8888",
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
|
||||
Reference in New Issue
Block a user