add tests

This commit is contained in:
naiba
2021-11-05 15:49:01 +08:00
parent 96c25f6fba
commit f63d8ba613
2 changed files with 91 additions and 46 deletions

View File

@@ -1,22 +1,30 @@
package model
import (
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var (
msg = "msg"
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
url string
body string
header string
reqType int
reqMethod int
expectURL string
expectBody string
expectMethod string
expectContentType string
expectHeader map[string]string
}
func execCase(t *testing.T, item testSt) {
@@ -25,60 +33,89 @@ func execCase(t *testing.T, item testSt) {
RequestMethod: item.reqMethod,
RequestType: item.reqType,
RequestBody: item.body,
RequestHeader: item.header,
}
assert.Equal(t, item.expectURL, n.reqURL(msg))
reqBody, err := n.reqBody(msg)
assert.Nil(t, err)
assert.Equal(t, item.expectBody, reqBody)
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))
}
}
func TestNotification(t *testing.T) {
cases := []testSt{
{
url: "https://example.com",
body: `{"asd":"dsa"}`,
reqMethod: NotificationRequestMethodGET,
expectURL: "https://example.com",
expectBody: "",
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: "",
},
{
url: "https://example.com/?m=#NEZHA#",
body: `{"asd":"dsa"}`,
reqMethod: NotificationRequestMethodGET,
expectURL: "https://example.com/?m=" + msg,
expectBody: "",
url: "https://example.com/?m=#NEZHA#",
body: `{"asd":"dsa"}`,
reqMethod: NotificationRequestMethodGET,
expectURL: "https://example.com/?m=" + msg,
expectMethod: http.MethodGet,
expectContentType: "",
expectBody: "",
},
{
url: "https://example.com/?m=#NEZHA#",
body: `{"asd":"#NEZHA#"}`,
reqMethod: NotificationRequestMethodPOST,
reqType: NotificationRequestTypeForm,
expectURL: "https://example.com/?m=" + msg,
expectBody: "asd=" + msg,
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,
},
{
url: "https://example.com/?m=#NEZHA#",
body: `{"#NEZHA#":"#NEZHA#"}`,
reqMethod: NotificationRequestMethodPOST,
reqType: NotificationRequestTypeForm,
expectURL: "https://example.com/?m=" + msg,
expectBody: "%23NEZHA%23=" + msg,
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,
},
{
url: "https://example.com/?m=#NEZHA#",
body: `{"asd":"#NEZHA#"}`,
reqMethod: NotificationRequestMethodPOST,
reqType: NotificationRequestTypeJSON,
expectURL: "https://example.com/?m=" + msg,
expectBody: `{"asd":"msg"}`,
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"}`,
},
{
url: "https://example.com/?m=#NEZHA#",
body: `{"#NEZHA#":"#NEZHA#"}`,
reqMethod: NotificationRequestMethodPOST,
reqType: NotificationRequestTypeJSON,
expectURL: "https://example.com/?m=" + msg,
expectBody: `{"msg":"msg"}`,
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"},
},
}