🎨 refactor: notification

This commit is contained in:
naiba
2020-12-30 21:28:57 +08:00
parent 797321e489
commit d91819a265
6 changed files with 150 additions and 49 deletions

View File

@@ -3,6 +3,7 @@ package model
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
@@ -32,6 +33,42 @@ type Notification struct {
VerifySSL *bool
}
func (n *Notification) reqURL(message string) string {
return replaceParamsInString(n.URL, message)
}
func (n *Notification) reqBody(message string) (string, error) {
if n.RequestMethod == NotificationRequestMethodGET {
return "", nil
}
switch n.RequestType {
case NotificationRequestTypeJSON:
return replaceParamsInString(n.RequestBody, message), nil
case NotificationRequestTypeForm:
var data map[string]string
if err := json.Unmarshal([]byte(n.RequestBody), &data); err != nil {
return "", err
}
params := url.Values{}
for k, v := range data {
params.Add(k, replaceParamsInString(v, message))
}
return params.Encode(), nil
}
return "", errors.New("不支持的请求类型")
}
func (n *Notification) reqContentType() string {
if n.RequestMethod == NotificationRequestMethodGET {
return ""
}
if n.RequestType == NotificationRequestTypeForm {
return "application/x-www-form-urlencoded"
} else {
return "application/json"
}
}
func (n *Notification) Send(message string) error {
var verifySSL bool
@@ -39,39 +76,20 @@ func (n *Notification) Send(message string) error {
verifySSL = true
}
var err error
transCfg := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: verifySSL},
}
client := &http.Client{Transport: transCfg, Timeout: time.Minute * 10}
var reqURL *url.URL
reqURL, err = url.Parse(n.URL)
var data map[string]string
if err == nil && (n.RequestMethod == NotificationRequestMethodGET || n.RequestType == NotificationRequestTypeForm) {
err = json.Unmarshal([]byte(n.RequestBody), &data)
}
reqBody, err := n.reqBody(message)
var resp *http.Response
if err == nil {
if n.RequestMethod == NotificationRequestMethodGET {
var queryValue = reqURL.Query()
for k, v := range data {
queryValue.Set(k, replaceParamsInString(v, message))
}
reqURL.RawQuery = queryValue.Encode()
resp, err = client.Get(reqURL.String())
resp, err = client.Get(n.reqURL(message))
} else {
if n.RequestType == NotificationRequestTypeForm {
params := url.Values{}
for k, v := range data {
params.Add(k, replaceParamsInString(v, message))
}
resp, err = client.PostForm(reqURL.String(), params)
} else {
jsonValue := replaceParamsInJSON(n.RequestBody, message)
resp, err = client.Post(reqURL.String(), "application/json", strings.NewReader(jsonValue))
}
resp, err = client.Post(n.reqURL(message), n.reqContentType(), strings.NewReader(reqBody))
}
}
@@ -86,17 +104,3 @@ func replaceParamsInString(str string, message string) string {
str = strings.ReplaceAll(str, "#NEZHA#", message)
return str
}
func replaceParamsInJSON(str string, message string) string {
str = strings.ReplaceAll(str, "#NEZHA#", message)
return str
}
func jsonEscape(raw interface{}) string {
b, _ := json.Marshal(raw)
strb := string(b)
if strings.HasPrefix(strb, "\"") {
return strb[1 : len(strb)-1]
}
return strb
}

View File

@@ -0,0 +1,98 @@
package model
import (
"testing"
"github.com/stretchr/testify/assert"
)
var (
msg = "msg"
reqTypeForm = "application/x-www-form-urlencoded"
reqTypeJSON = "application/json"
)
type testSt struct {
url string
body string
reqType int
reqMethod int
expectURL string
expectBody string
expectType string
}
func execCase(t *testing.T, item testSt) {
n := Notification{
URL: item.url,
RequestMethod: item.reqMethod,
RequestType: item.reqType,
RequestBody: item.body,
}
assert.Equal(t, item.expectURL, n.reqURL(msg))
reqBody, err := n.reqBody(msg)
assert.Nil(t, err)
assert.Equal(t, item.expectBody, reqBody)
assert.Equal(t, item.expectType, n.reqContentType())
}
func TestNotification(t *testing.T) {
cases := []testSt{
{
url: "https://example.com",
body: `{"asd":"dsa"}`,
reqMethod: NotificationRequestMethodGET,
expectURL: "https://example.com",
expectBody: "",
expectType: "",
},
{
url: "https://example.com/?m=#NEZHA#",
body: `{"asd":"dsa"}`,
reqMethod: NotificationRequestMethodGET,
expectURL: "https://example.com/?m=" + msg,
expectBody: "",
expectType: "",
},
{
url: "https://example.com/?m=#NEZHA#",
body: `{"asd":"#NEZHA#"}`,
reqMethod: NotificationRequestMethodPOST,
reqType: NotificationRequestTypeForm,
expectURL: "https://example.com/?m=" + msg,
expectBody: "asd=" + msg,
expectType: reqTypeForm,
},
{
url: "https://example.com/?m=#NEZHA#",
body: `{"#NEZHA#":"#NEZHA#"}`,
reqMethod: NotificationRequestMethodPOST,
reqType: NotificationRequestTypeForm,
expectURL: "https://example.com/?m=" + msg,
expectBody: "%23NEZHA%23=" + msg,
expectType: reqTypeForm,
},
{
url: "https://example.com/?m=#NEZHA#",
body: `{"asd":"#NEZHA#"}`,
reqMethod: NotificationRequestMethodPOST,
reqType: NotificationRequestTypeJSON,
expectURL: "https://example.com/?m=" + msg,
expectBody: `{"asd":"msg"}`,
expectType: reqTypeJSON,
},
{
url: "https://example.com/?m=#NEZHA#",
body: `{"#NEZHA#":"#NEZHA#"}`,
reqMethod: NotificationRequestMethodPOST,
reqType: NotificationRequestTypeJSON,
expectURL: "https://example.com/?m=" + msg,
expectBody: `{"msg":"msg"}`,
expectType: reqTypeJSON,
},
}
for _, c := range cases {
execCase(t, c)
}
}