2020-12-30 21:28:57 +08:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2021-11-05 15:49:01 +08:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2020-12-30 21:28:57 +08:00
|
|
|
"testing"
|
2022-04-17 17:38:45 +08:00
|
|
|
"time"
|
2020-12-30 21:28:57 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-11-05 15:49:01 +08:00
|
|
|
msg = "msg"
|
|
|
|
reqTypeForm = "application/x-www-form-urlencoded"
|
|
|
|
reqTypeJSON = "application/json"
|
2020-12-30 21:28:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type testSt struct {
|
2021-11-05 15:49:01 +08:00
|
|
|
url string
|
|
|
|
body string
|
|
|
|
header string
|
|
|
|
reqType int
|
|
|
|
reqMethod int
|
|
|
|
expectURL string
|
|
|
|
expectBody string
|
|
|
|
expectMethod string
|
|
|
|
expectContentType string
|
|
|
|
expectHeader map[string]string
|
2020-12-30 21:28:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func execCase(t *testing.T, item testSt) {
|
|
|
|
n := Notification{
|
|
|
|
URL: item.url,
|
|
|
|
RequestMethod: item.reqMethod,
|
|
|
|
RequestType: item.reqType,
|
|
|
|
RequestBody: item.body,
|
2021-11-05 15:49:01 +08:00
|
|
|
RequestHeader: item.header,
|
2020-12-30 21:28:57 +08:00
|
|
|
}
|
2022-04-17 17:38:45 +08:00
|
|
|
server := Server{
|
2022-04-17 21:06:30 +08:00
|
|
|
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,
|
|
|
|
},
|
2022-04-17 17:38:45 +08:00
|
|
|
LastActive: time.Time{},
|
|
|
|
TaskClose: nil,
|
|
|
|
TaskStream: nil,
|
|
|
|
PrevHourlyTransferIn: 0,
|
|
|
|
PrevHourlyTransferOut: 0,
|
|
|
|
}
|
|
|
|
ns := NotificationServerBundle{
|
|
|
|
Notification: &n,
|
|
|
|
Server: &server,
|
|
|
|
}
|
|
|
|
assert.Equal(t, item.expectURL, ns.reqURL(msg))
|
|
|
|
reqBody, err := ns.reqBody(msg)
|
2020-12-30 21:28:57 +08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, item.expectBody, reqBody)
|
2021-11-05 15:49:01 +08:00
|
|
|
reqMethod, err := n.reqMethod()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, item.expectMethod, reqMethod)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("", "", strings.NewReader(""))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
n.setContentType(req)
|
|
|
|
assert.Equal(t, item.expectContentType, req.Header.Get("Content-Type"))
|
|
|
|
n.setRequestHeader(req)
|
|
|
|
for k, v := range item.expectHeader {
|
|
|
|
assert.Equal(t, v, req.Header.Get(k))
|
|
|
|
}
|
2020-12-30 21:28:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNotification(t *testing.T) {
|
|
|
|
cases := []testSt{
|
|
|
|
{
|
2021-11-05 15:49:01 +08:00
|
|
|
url: "https://example.com",
|
|
|
|
body: `{"asd":"dsa"}`,
|
|
|
|
header: `{"asd":"dsa"}`,
|
|
|
|
reqMethod: NotificationRequestMethodGET,
|
|
|
|
expectURL: "https://example.com",
|
|
|
|
expectMethod: http.MethodGet,
|
|
|
|
expectContentType: "",
|
|
|
|
expectHeader: map[string]string{"asd": "dsa"},
|
|
|
|
expectBody: "",
|
2020-12-30 21:28:57 +08:00
|
|
|
},
|
|
|
|
{
|
2021-11-05 15:49:01 +08:00
|
|
|
url: "https://example.com/?m=#NEZHA#",
|
|
|
|
body: `{"asd":"dsa"}`,
|
|
|
|
reqMethod: NotificationRequestMethodGET,
|
|
|
|
expectURL: "https://example.com/?m=" + msg,
|
|
|
|
expectMethod: http.MethodGet,
|
|
|
|
expectContentType: "",
|
|
|
|
expectBody: "",
|
2020-12-30 21:28:57 +08:00
|
|
|
},
|
|
|
|
{
|
2021-11-05 15:49:01 +08:00
|
|
|
url: "https://example.com/?m=#NEZHA#",
|
|
|
|
body: `{"asd":"#NEZHA#"}`,
|
|
|
|
reqMethod: NotificationRequestMethodPOST,
|
|
|
|
reqType: NotificationRequestTypeForm,
|
|
|
|
expectURL: "https://example.com/?m=" + msg,
|
|
|
|
expectMethod: http.MethodPost,
|
|
|
|
expectContentType: reqTypeForm,
|
|
|
|
expectBody: "asd=" + msg,
|
2020-12-30 21:28:57 +08:00
|
|
|
},
|
|
|
|
{
|
2021-11-05 15:49:01 +08:00
|
|
|
url: "https://example.com/?m=#NEZHA#",
|
|
|
|
body: `{"#NEZHA#":"#NEZHA#"}`,
|
|
|
|
reqMethod: NotificationRequestMethodPOST,
|
|
|
|
reqType: NotificationRequestTypeForm,
|
|
|
|
expectURL: "https://example.com/?m=" + msg,
|
|
|
|
expectMethod: http.MethodPost,
|
|
|
|
expectContentType: reqTypeForm,
|
|
|
|
expectBody: "%23NEZHA%23=" + msg,
|
2020-12-30 21:28:57 +08:00
|
|
|
},
|
|
|
|
{
|
2021-11-05 15:49:01 +08:00
|
|
|
url: "https://example.com/?m=#NEZHA#",
|
|
|
|
body: `{"asd":"#NEZHA#"}`,
|
|
|
|
reqMethod: NotificationRequestMethodPOST,
|
|
|
|
reqType: NotificationRequestTypeJSON,
|
|
|
|
expectURL: "https://example.com/?m=" + msg,
|
|
|
|
expectMethod: http.MethodPost,
|
|
|
|
expectContentType: reqTypeJSON,
|
|
|
|
expectBody: `{"asd":"msg"}`,
|
2020-12-30 21:28:57 +08:00
|
|
|
},
|
|
|
|
{
|
2021-11-05 15:49:01 +08:00
|
|
|
url: "https://example.com/?m=#NEZHA#",
|
|
|
|
body: `{"#NEZHA#":"#NEZHA#"}`,
|
|
|
|
reqMethod: NotificationRequestMethodPOST,
|
|
|
|
header: `{"asd":"dsa11"}`,
|
|
|
|
reqType: NotificationRequestTypeJSON,
|
|
|
|
expectURL: "https://example.com/?m=" + msg,
|
|
|
|
expectMethod: http.MethodPost,
|
|
|
|
expectContentType: reqTypeJSON,
|
|
|
|
expectBody: `{"msg":"msg"}`,
|
|
|
|
expectHeader: map[string]string{"asd": "dsa11"},
|
2020-12-30 21:28:57 +08:00
|
|
|
},
|
2022-04-17 17:38:45 +08:00
|
|
|
{
|
|
|
|
url: "https://example.com/?m=#NEZHA#",
|
2022-04-17 21:06:30 +08:00
|
|
|
body: `{"Server":"#SERVER.NAME#","ServerIP":"#SERVER.IP#","ServerSWAP":#SERVER.SWAP#}`,
|
2022-04-17 17:38:45 +08:00
|
|
|
reqMethod: NotificationRequestMethodPOST,
|
|
|
|
header: `{"asd":"dsa11"}`,
|
|
|
|
reqType: NotificationRequestTypeJSON,
|
|
|
|
expectURL: "https://example.com/?m=" + msg,
|
|
|
|
expectMethod: http.MethodPost,
|
|
|
|
expectContentType: reqTypeJSON,
|
2022-04-17 21:06:30 +08:00
|
|
|
expectBody: `{"Server":"ServerName","ServerIP":"1.1.1.1","ServerSWAP":8888}`,
|
2022-04-17 17:38:45 +08:00
|
|
|
expectHeader: map[string]string{"asd": "dsa11"},
|
|
|
|
},
|
2022-04-17 21:06:30 +08:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
2020-12-30 21:28:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
execCase(t, c)
|
|
|
|
}
|
|
|
|
}
|