fix #37 support number of notifications

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-04-10 13:04:32 +08:00
parent 91099ff74e
commit 32507de908
3 changed files with 46 additions and 4 deletions

View File

@ -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"

View File

@ -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
}

View File

@ -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()