✅ add tests
This commit is contained in:
		
							parent
							
								
									96c25f6fba
								
							
						
					
					
						commit
						f63d8ba613
					
				@ -41,11 +41,14 @@ func (n *Notification) reqURL(message string) string {
 | 
				
			|||||||
	})
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (n *Notification) reqMethod() string {
 | 
					func (n *Notification) reqMethod() (string, error) {
 | 
				
			||||||
	if n.RequestMethod == NotificationRequestMethodPOST {
 | 
						switch n.RequestMethod {
 | 
				
			||||||
		return http.MethodPost
 | 
						case NotificationRequestMethodPOST:
 | 
				
			||||||
 | 
							return http.MethodPost, nil
 | 
				
			||||||
 | 
						case NotificationRequestMethodGET:
 | 
				
			||||||
 | 
							return http.MethodGet, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return http.MethodGet
 | 
						return "", errors.New("不支持的请求方式")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (n *Notification) reqBody(message string) (string, error) {
 | 
					func (n *Notification) reqBody(message string) (string, error) {
 | 
				
			||||||
@ -115,7 +118,12 @@ func (n *Notification) Send(message string) error {
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	req, err := http.NewRequest(n.reqMethod(), n.reqURL(message), strings.NewReader(reqBody))
 | 
						reqMethod, err := n.reqMethod()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						req, err := http.NewRequest(reqMethod, n.reqURL(message), strings.NewReader(reqBody))
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,8 @@
 | 
				
			|||||||
package model
 | 
					package model
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/stretchr/testify/assert"
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
@ -8,15 +10,21 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
	msg         = "msg"
 | 
						msg         = "msg"
 | 
				
			||||||
 | 
						reqTypeForm = "application/x-www-form-urlencoded"
 | 
				
			||||||
 | 
						reqTypeJSON = "application/json"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type testSt struct {
 | 
					type testSt struct {
 | 
				
			||||||
	url               string
 | 
						url               string
 | 
				
			||||||
	body              string
 | 
						body              string
 | 
				
			||||||
 | 
						header            string
 | 
				
			||||||
	reqType           int
 | 
						reqType           int
 | 
				
			||||||
	reqMethod         int
 | 
						reqMethod         int
 | 
				
			||||||
	expectURL         string
 | 
						expectURL         string
 | 
				
			||||||
	expectBody        string
 | 
						expectBody        string
 | 
				
			||||||
 | 
						expectMethod      string
 | 
				
			||||||
 | 
						expectContentType string
 | 
				
			||||||
 | 
						expectHeader      map[string]string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func execCase(t *testing.T, item testSt) {
 | 
					func execCase(t *testing.T, item testSt) {
 | 
				
			||||||
@ -25,11 +33,24 @@ func execCase(t *testing.T, item testSt) {
 | 
				
			|||||||
		RequestMethod: item.reqMethod,
 | 
							RequestMethod: item.reqMethod,
 | 
				
			||||||
		RequestType:   item.reqType,
 | 
							RequestType:   item.reqType,
 | 
				
			||||||
		RequestBody:   item.body,
 | 
							RequestBody:   item.body,
 | 
				
			||||||
 | 
							RequestHeader: item.header,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	assert.Equal(t, item.expectURL, n.reqURL(msg))
 | 
						assert.Equal(t, item.expectURL, n.reqURL(msg))
 | 
				
			||||||
	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)
 | 
				
			||||||
 | 
						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) {
 | 
					func TestNotification(t *testing.T) {
 | 
				
			||||||
@ -37,8 +58,12 @@ func TestNotification(t *testing.T) {
 | 
				
			|||||||
		{
 | 
							{
 | 
				
			||||||
			url:               "https://example.com",
 | 
								url:               "https://example.com",
 | 
				
			||||||
			body:              `{"asd":"dsa"}`,
 | 
								body:              `{"asd":"dsa"}`,
 | 
				
			||||||
 | 
								header:            `{"asd":"dsa"}`,
 | 
				
			||||||
			reqMethod:         NotificationRequestMethodGET,
 | 
								reqMethod:         NotificationRequestMethodGET,
 | 
				
			||||||
			expectURL:         "https://example.com",
 | 
								expectURL:         "https://example.com",
 | 
				
			||||||
 | 
								expectMethod:      http.MethodGet,
 | 
				
			||||||
 | 
								expectContentType: "",
 | 
				
			||||||
 | 
								expectHeader:      map[string]string{"asd": "dsa"},
 | 
				
			||||||
			expectBody:        "",
 | 
								expectBody:        "",
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
@ -46,6 +71,8 @@ func TestNotification(t *testing.T) {
 | 
				
			|||||||
			body:              `{"asd":"dsa"}`,
 | 
								body:              `{"asd":"dsa"}`,
 | 
				
			||||||
			reqMethod:         NotificationRequestMethodGET,
 | 
								reqMethod:         NotificationRequestMethodGET,
 | 
				
			||||||
			expectURL:         "https://example.com/?m=" + msg,
 | 
								expectURL:         "https://example.com/?m=" + msg,
 | 
				
			||||||
 | 
								expectMethod:      http.MethodGet,
 | 
				
			||||||
 | 
								expectContentType: "",
 | 
				
			||||||
			expectBody:        "",
 | 
								expectBody:        "",
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
@ -54,6 +81,8 @@ func TestNotification(t *testing.T) {
 | 
				
			|||||||
			reqMethod:         NotificationRequestMethodPOST,
 | 
								reqMethod:         NotificationRequestMethodPOST,
 | 
				
			||||||
			reqType:           NotificationRequestTypeForm,
 | 
								reqType:           NotificationRequestTypeForm,
 | 
				
			||||||
			expectURL:         "https://example.com/?m=" + msg,
 | 
								expectURL:         "https://example.com/?m=" + msg,
 | 
				
			||||||
 | 
								expectMethod:      http.MethodPost,
 | 
				
			||||||
 | 
								expectContentType: reqTypeForm,
 | 
				
			||||||
			expectBody:        "asd=" + msg,
 | 
								expectBody:        "asd=" + msg,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
@ -62,6 +91,8 @@ func TestNotification(t *testing.T) {
 | 
				
			|||||||
			reqMethod:         NotificationRequestMethodPOST,
 | 
								reqMethod:         NotificationRequestMethodPOST,
 | 
				
			||||||
			reqType:           NotificationRequestTypeForm,
 | 
								reqType:           NotificationRequestTypeForm,
 | 
				
			||||||
			expectURL:         "https://example.com/?m=" + msg,
 | 
								expectURL:         "https://example.com/?m=" + msg,
 | 
				
			||||||
 | 
								expectMethod:      http.MethodPost,
 | 
				
			||||||
 | 
								expectContentType: reqTypeForm,
 | 
				
			||||||
			expectBody:        "%23NEZHA%23=" + msg,
 | 
								expectBody:        "%23NEZHA%23=" + msg,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
@ -70,15 +101,21 @@ func TestNotification(t *testing.T) {
 | 
				
			|||||||
			reqMethod:         NotificationRequestMethodPOST,
 | 
								reqMethod:         NotificationRequestMethodPOST,
 | 
				
			||||||
			reqType:           NotificationRequestTypeJSON,
 | 
								reqType:           NotificationRequestTypeJSON,
 | 
				
			||||||
			expectURL:         "https://example.com/?m=" + msg,
 | 
								expectURL:         "https://example.com/?m=" + msg,
 | 
				
			||||||
 | 
								expectMethod:      http.MethodPost,
 | 
				
			||||||
 | 
								expectContentType: reqTypeJSON,
 | 
				
			||||||
			expectBody:        `{"asd":"msg"}`,
 | 
								expectBody:        `{"asd":"msg"}`,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			url:               "https://example.com/?m=#NEZHA#",
 | 
								url:               "https://example.com/?m=#NEZHA#",
 | 
				
			||||||
			body:              `{"#NEZHA#":"#NEZHA#"}`,
 | 
								body:              `{"#NEZHA#":"#NEZHA#"}`,
 | 
				
			||||||
			reqMethod:         NotificationRequestMethodPOST,
 | 
								reqMethod:         NotificationRequestMethodPOST,
 | 
				
			||||||
 | 
								header:            `{"asd":"dsa11"}`,
 | 
				
			||||||
			reqType:           NotificationRequestTypeJSON,
 | 
								reqType:           NotificationRequestTypeJSON,
 | 
				
			||||||
			expectURL:         "https://example.com/?m=" + msg,
 | 
								expectURL:         "https://example.com/?m=" + msg,
 | 
				
			||||||
 | 
								expectMethod:      http.MethodPost,
 | 
				
			||||||
 | 
								expectContentType: reqTypeJSON,
 | 
				
			||||||
			expectBody:        `{"msg":"msg"}`,
 | 
								expectBody:        `{"msg":"msg"}`,
 | 
				
			||||||
 | 
								expectHeader:      map[string]string{"asd": "dsa11"},
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user