From 32507de908ad392ab1bcec79e461b0d308a7cafa Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 10 Apr 2016 13:04:32 +0800 Subject: [PATCH 1/2] fix #37 support number of notifications Signed-off-by: Bo-Yi Wu --- gorush/config.go | 4 ++-- gorush/server.go | 17 +++++++++++++++-- gorush/server_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) 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() From 2028c1360492b8d66c8c6a369f06f1b00ce6a96d Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 10 Apr 2016 13:06:34 +0800 Subject: [PATCH 2/2] [ci skip] update readme. Signed-off-by: Bo-Yi Wu --- README.md | 2 +- config/config.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0214e4c..6c5cf3f 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ See the [YAML config eample](config/config.yaml): ```yaml core: port: "8088" - notification_max: 100 + max_notification: 100 mode: "release" ssl: false cert_path: "cert.pem" diff --git a/config/config.yaml b/config/config.yaml index 04c7049..7db709a 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,6 +1,6 @@ core: port: "8088" - notification_max: 100 + max_notification: 100 mode: "release" ssl: false cert_path: "cert.pem"