♻️ set content type
This commit is contained in:
parent
6636d77cfd
commit
17a53f8829
@ -75,14 +75,15 @@ func (n *Notification) reqBody(message string) (string, error) {
|
|||||||
return "", errors.New("不支持的请求类型")
|
return "", errors.New("不支持的请求类型")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Notification) reqContentType() string {
|
func (n *Notification) setContentType(req *http.Request) {
|
||||||
if n.RequestMethod == NotificationRequestMethodGET {
|
if n.RequestMethod == NotificationRequestMethodGET {
|
||||||
return ""
|
return
|
||||||
}
|
}
|
||||||
if n.RequestType == NotificationRequestTypeForm {
|
if n.RequestType == NotificationRequestTypeForm {
|
||||||
return "application/x-www-form-urlencoded"
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
} else {
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
return "application/json"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Notification) setRequestHeader(req *http.Request) error {
|
func (n *Notification) setRequestHeader(req *http.Request) error {
|
||||||
@ -122,6 +123,8 @@ func (n *Notification) Send(message string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
n.setContentType(req)
|
||||||
|
|
||||||
if err := n.setRequestHeader(req); err != nil {
|
if err := n.setRequestHeader(req); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
msg = "msg"
|
msg = "msg"
|
||||||
reqTypeForm = "application/x-www-form-urlencoded"
|
|
||||||
reqTypeJSON = "application/json"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type testSt struct {
|
type testSt struct {
|
||||||
@ -19,7 +17,6 @@ type testSt struct {
|
|||||||
reqMethod int
|
reqMethod int
|
||||||
expectURL string
|
expectURL string
|
||||||
expectBody string
|
expectBody string
|
||||||
expectType string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func execCase(t *testing.T, item testSt) {
|
func execCase(t *testing.T, item testSt) {
|
||||||
@ -33,7 +30,6 @@ func execCase(t *testing.T, item testSt) {
|
|||||||
reqBody, err := n.reqBody(msg)
|
reqBody, err := n.reqBody(msg)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, item.expectBody, reqBody)
|
assert.Equal(t, item.expectBody, reqBody)
|
||||||
assert.Equal(t, item.expectType, n.reqContentType())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNotification(t *testing.T) {
|
func TestNotification(t *testing.T) {
|
||||||
@ -44,7 +40,6 @@ func TestNotification(t *testing.T) {
|
|||||||
reqMethod: NotificationRequestMethodGET,
|
reqMethod: NotificationRequestMethodGET,
|
||||||
expectURL: "https://example.com",
|
expectURL: "https://example.com",
|
||||||
expectBody: "",
|
expectBody: "",
|
||||||
expectType: "",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
url: "https://example.com/?m=#NEZHA#",
|
url: "https://example.com/?m=#NEZHA#",
|
||||||
@ -52,7 +47,6 @@ func TestNotification(t *testing.T) {
|
|||||||
reqMethod: NotificationRequestMethodGET,
|
reqMethod: NotificationRequestMethodGET,
|
||||||
expectURL: "https://example.com/?m=" + msg,
|
expectURL: "https://example.com/?m=" + msg,
|
||||||
expectBody: "",
|
expectBody: "",
|
||||||
expectType: "",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
url: "https://example.com/?m=#NEZHA#",
|
url: "https://example.com/?m=#NEZHA#",
|
||||||
@ -61,7 +55,6 @@ func TestNotification(t *testing.T) {
|
|||||||
reqType: NotificationRequestTypeForm,
|
reqType: NotificationRequestTypeForm,
|
||||||
expectURL: "https://example.com/?m=" + msg,
|
expectURL: "https://example.com/?m=" + msg,
|
||||||
expectBody: "asd=" + msg,
|
expectBody: "asd=" + msg,
|
||||||
expectType: reqTypeForm,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
url: "https://example.com/?m=#NEZHA#",
|
url: "https://example.com/?m=#NEZHA#",
|
||||||
@ -70,7 +63,6 @@ func TestNotification(t *testing.T) {
|
|||||||
reqType: NotificationRequestTypeForm,
|
reqType: NotificationRequestTypeForm,
|
||||||
expectURL: "https://example.com/?m=" + msg,
|
expectURL: "https://example.com/?m=" + msg,
|
||||||
expectBody: "%23NEZHA%23=" + msg,
|
expectBody: "%23NEZHA%23=" + msg,
|
||||||
expectType: reqTypeForm,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
url: "https://example.com/?m=#NEZHA#",
|
url: "https://example.com/?m=#NEZHA#",
|
||||||
@ -79,7 +71,6 @@ func TestNotification(t *testing.T) {
|
|||||||
reqType: NotificationRequestTypeJSON,
|
reqType: NotificationRequestTypeJSON,
|
||||||
expectURL: "https://example.com/?m=" + msg,
|
expectURL: "https://example.com/?m=" + msg,
|
||||||
expectBody: `{"asd":"msg"}`,
|
expectBody: `{"asd":"msg"}`,
|
||||||
expectType: reqTypeJSON,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
url: "https://example.com/?m=#NEZHA#",
|
url: "https://example.com/?m=#NEZHA#",
|
||||||
@ -88,7 +79,6 @@ func TestNotification(t *testing.T) {
|
|||||||
reqType: NotificationRequestTypeJSON,
|
reqType: NotificationRequestTypeJSON,
|
||||||
expectURL: "https://example.com/?m=" + msg,
|
expectURL: "https://example.com/?m=" + msg,
|
||||||
expectBody: `{"msg":"msg"}`,
|
expectBody: `{"msg":"msg"}`,
|
||||||
expectType: reqTypeJSON,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user