nezha/model/notification_test.go

126 lines
3.6 KiB
Go
Raw Normal View History

2020-12-30 08:28:57 -05:00
package model
import (
2021-11-05 03:49:01 -04:00
"net/http"
"strings"
2020-12-30 08:28:57 -05:00
"testing"
"github.com/stretchr/testify/assert"
)
var (
2021-11-05 03:49:01 -04:00
msg = "msg"
reqTypeForm = "application/x-www-form-urlencoded"
reqTypeJSON = "application/json"
2020-12-30 08:28:57 -05:00
)
type testSt struct {
2021-11-05 03:49:01 -04: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 08:28:57 -05: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 03:49:01 -04:00
RequestHeader: item.header,
2020-12-30 08:28:57 -05:00
}
assert.Equal(t, item.expectURL, n.reqURL(msg))
reqBody, err := n.reqBody(msg)
assert.Nil(t, err)
assert.Equal(t, item.expectBody, reqBody)
2021-11-05 03:49:01 -04: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 08:28:57 -05:00
}
func TestNotification(t *testing.T) {
cases := []testSt{
{
2021-11-05 03:49:01 -04: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 08:28:57 -05:00
},
{
2021-11-05 03:49:01 -04: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 08:28:57 -05:00
},
{
2021-11-05 03:49:01 -04: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 08:28:57 -05:00
},
{
2021-11-05 03:49:01 -04: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 08:28:57 -05:00
},
{
2021-11-05 03:49:01 -04: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 08:28:57 -05:00
},
{
2021-11-05 03:49:01 -04: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 08:28:57 -05:00
},
}
for _, c := range cases {
execCase(t, c)
}
}