diff --git a/gorush/config.go b/gorush/config.go index 087a7ff..67c6e92 100644 --- a/gorush/config.go +++ b/gorush/config.go @@ -16,7 +16,7 @@ type ConfYaml struct { type SectionCore struct { Port string `yaml:"port"` - NotificationMax int `yaml:"notification_max"` + MaxNotification int `yaml:"max_notification"` Mode string `yaml:"mode"` SSL bool `yaml:"ssl"` CertPath string `yaml:"cert_path"` @@ -53,11 +53,11 @@ func BuildDefaultPushConf() ConfYaml { // Core conf.Core.Port = "8088" - conf.Core.NotificationMax = 100 conf.Core.Mode = "release" conf.Core.SSL = false conf.Core.CertPath = "cert.pem" conf.Core.KeyPath = "key.pem" + conf.Core.MaxNotification = 100 // Api conf.Api.PushUri = "/api/push" diff --git a/gorush/server.go b/gorush/server.go index 206c2fa..d370b76 100644 --- a/gorush/server.go +++ b/gorush/server.go @@ -4,6 +4,7 @@ import ( api "github.com/appleboy/gin-status-api" "github.com/gin-gonic/gin" "net/http" + "fmt" ) func AbortWithError(c *gin.Context, code int, message string) { @@ -22,14 +23,26 @@ func rootHandler(c *gin.Context) { func pushHandler(c *gin.Context) { var form RequestPush + var msg string if err := c.BindJSON(&form); err != nil { - AbortWithError(c, http.StatusBadRequest, "Missing nitifications field.") + msg = "Missing nitifications field." + LogAccess.Debug(msg) + AbortWithError(c, http.StatusBadRequest, msg) return } if len(form.Notifications) == 0 { - AbortWithError(c, http.StatusBadRequest, "Notification field is empty.") + msg = "Notification field is empty." + LogAccess.Debug(msg) + AbortWithError(c, http.StatusBadRequest, msg) + return + } + + if len(form.Notifications) > PushConf.Core.MaxNotification { + msg = fmt.Sprintf("Number of notifications(%d) over limit(%d)", len(form.Notifications), PushConf.Core.MaxNotification) + LogAccess.Debug(msg) + AbortWithError(c, http.StatusBadRequest, msg) return } diff --git a/gorush/server_test.go b/gorush/server_test.go index 9bfcda4..a95d2d4 100644 --- a/gorush/server_test.go +++ b/gorush/server_test.go @@ -121,6 +121,35 @@ func TestEmptyNotifications(t *testing.T) { }) } +func TestOutOfRangeMaxNotifications(t *testing.T) { + initTest() + + PushConf.Core.MaxNotification = 1 + + r := gofight.New() + + // notifications is empty. + r.POST("/api/push"). + SetJSON(gofight.D{ + "notifications": []gofight.D{ + gofight.D{ + "tokens": []string{"aaaaa", "bbbbb"}, + "platform": 2, + "message": "Welcome", + }, + gofight.D{ + "tokens": []string{"aaaaa", "bbbbb"}, + "platform": 2, + "message": "Welcome", + }, + }, + }). + Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { + + assert.Equal(t, http.StatusBadRequest, r.Code) + }) +} + func TestSuccessPushHandler(t *testing.T) { initTest()