fix server testing.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-04-10 12:02:44 +08:00
parent bbfc058d05
commit c61aeeec7f
4 changed files with 34 additions and 126 deletions

View File

@ -42,7 +42,7 @@ type Alert struct {
}
type RequestPush struct {
Notifications []PushNotification `json:"notifications"`
Notifications []PushNotification `json:"notifications" binding:"required"`
}
type PushNotification struct {

View File

@ -4,8 +4,8 @@ import (
"encoding/json"
"github.com/buger/jsonparser"
"github.com/google/go-gcm"
"github.com/stretchr/testify/assert"
"github.com/sideshow/apns2"
"github.com/stretchr/testify/assert"
"log"
"os"
"testing"

View File

@ -21,15 +21,20 @@ func rootHandler(c *gin.Context) {
}
func pushHandler(c *gin.Context) {
var form RequestPushNotification
var form RequestPush
if err := c.BindJSON(&form); err != nil {
AbortWithError(c, http.StatusBadRequest, "Bad input request, please refer to README guide.")
AbortWithError(c, http.StatusBadRequest, "Missing nitifications field.")
return
}
if len(form.Notifications) == 0 {
AbortWithError(c, http.StatusBadRequest, "Notification field is empty.")
return
}
// process notification.
pushNotification(form)
go SendNotification(form)
c.JSON(http.StatusOK, gin.H{
"text": "Welcome to notification server.",

View File

@ -92,15 +92,28 @@ func TestAPIStatusHandler(t *testing.T) {
})
}
func TestMissingParameterPushHandler(t *testing.T) {
func TestMissingNotificationsParameter(t *testing.T) {
initTest()
r := gofight.New()
// missing some parameter.
// missing notifications parameter.
r.POST("/api/push").
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusBadRequest, r.Code)
})
}
func TestEmptyNotifications(t *testing.T) {
initTest()
r := gofight.New()
// notifications is empty.
r.POST("/api/push").
SetJSON(gofight.D{
"platform": 1,
"notifications": []PushNotification{},
}).
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
@ -108,99 +121,7 @@ func TestMissingParameterPushHandler(t *testing.T) {
})
}
func TestDisabledIosPushHandler(t *testing.T) {
initTest()
PushConf.Ios.Enabled = false
InitAPNSClient()
r := gofight.New()
r.POST("/api/push").
SetJSON(gofight.D{
"tokens": []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
"platform": 1,
"message": "Welcome",
}).
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestMissingIosCertificate(t *testing.T) {
initTest()
PushConf.Ios.Enabled = true
PushConf.Ios.PemKeyPath = "test"
err := InitAPNSClient()
assert.Error(t, err)
}
func TestIosPushDevelopment(t *testing.T) {
initTest()
PushConf.Ios.Enabled = true
PushConf.Ios.PemKeyPath = "../certificate/certificate-valid.pem"
InitAPNSClient()
r := gofight.New()
r.POST("/api/push").
SetJSON(gofight.D{
"tokens": []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
"platform": 1,
"message": "Welcome",
}).
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestIosPushProduction(t *testing.T) {
initTest()
PushConf.Ios.Enabled = true
PushConf.Ios.Production = true
PushConf.Ios.PemKeyPath = "../certificate/certificate-valid.pem"
InitAPNSClient()
r := gofight.New()
r.POST("/api/push").
SetJSON(gofight.D{
"tokens": []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
"platform": 1,
"message": "Welcome",
}).
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestDisabledAndroidPushHandler(t *testing.T) {
initTest()
PushConf.Android.Enabled = false
r := gofight.New()
r.POST("/api/push").
SetJSON(gofight.D{
"tokens": []string{"aaaaaa", "bbbbb"},
"platform": 2,
"message": "Welcome",
}).
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestHalfSuccessAndroidPushHandler(t *testing.T) {
func TestSuccessPushHandler(t *testing.T) {
initTest()
PushConf.Android.Enabled = true
@ -212,31 +133,13 @@ func TestHalfSuccessAndroidPushHandler(t *testing.T) {
r.POST("/api/push").
SetJSON(gofight.D{
"tokens": []string{android_token, "bbbbb"},
"platform": 2,
"message": "Welcome",
}).
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestAllSuccessAndroidPushHandler(t *testing.T) {
initTest()
PushConf.Android.Enabled = true
PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY")
android_token := os.Getenv("ANDROID_TEST_TOKEN")
r := gofight.New()
r.POST("/api/push").
SetJSON(gofight.D{
"tokens": []string{android_token, android_token},
"platform": 2,
"message": "Welcome",
"notifications": []gofight.D{
gofight.D{
"tokens": []string{android_token, "bbbbb"},
"platform": 2,
"message": "Welcome",
},
},
}).
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {