Skip to content

Commit

Permalink
Merge pull request #65 from appleboy/GCM
Browse files Browse the repository at this point in the history
Fix #64 check user request message format.
  • Loading branch information
appleboy committed Apr 24, 2016
2 parents ba5a679 + 6c1c509 commit 4d91f4f
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 19 deletions.
17 changes: 12 additions & 5 deletions gorush.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,19 @@ func main() {

// send android notification
if *android {

checkInput(*token, *message)

gorush.PushConf.Android.Enabled = true
req := gorush.PushNotification{
Tokens: []string{*token},
Platform: gorush.PlatFormAndroid,
Message: *message,
}

err := gorush.CheckMessage(req)

if err != nil {
gorush.LogError.Fatal(err)
}

gorush.InitAppStatus()
gorush.PushToAndroid(req)

Expand All @@ -90,8 +93,6 @@ func main() {

// send android notification
if *ios {
checkInput(*token, *message)

if *production {
gorush.PushConf.Ios.Production = true
}
Expand All @@ -103,6 +104,12 @@ func main() {
Message: *message,
}

err := gorush.CheckMessage(req)

if err != nil {
gorush.LogError.Fatal(err)
}

gorush.InitAppStatus()
gorush.InitAPNSClient()
gorush.PushToIOS(req)
Expand Down
46 changes: 46 additions & 0 deletions gorush/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,44 @@ type PushNotification struct {
Alert Alert `json:"alert,omitempty"`
}

// CheckMessage for check request message
func CheckMessage(req PushNotification) error {
var msg string
if req.Message == "" {
msg = "the message must not be empty"
LogAccess.Debug(msg)
return errors.New(msg)
}

if len(req.Tokens) == 0 {
msg = "the message must specify at least one registration ID"
LogAccess.Debug(msg)
return errors.New(msg)
}

if len(req.Tokens) == PlatFormIos && len(req.Tokens[0]) == 0 {
msg = "the token must not be empty"
LogAccess.Debug(msg)
return errors.New(msg)
}

if req.Platform == PlatFormAndroid && len(req.Tokens) > 1000 {
msg = "the message may specify at most 1000 registration IDs"
LogAccess.Debug(msg)
return errors.New(msg)
}

// ref: https://developers.google.com/cloud-messaging/http-server-ref
if req.Platform == PlatFormAndroid && (req.TimeToLive < 0 || 2419200 < req.TimeToLive) {
msg = "the message's TimeToLive field must be an integer " +
"between 0 and 2419200 (4 weeks)"
LogAccess.Debug(msg)
return errors.New(msg)
}

return nil
}

// CheckPushConf provide check your yml config.
func CheckPushConf() error {
if !PushConf.Ios.Enabled && !PushConf.Android.Enabled {
Expand Down Expand Up @@ -348,6 +386,14 @@ func GetAndroidNotification(req PushNotification) gcm.HttpMessage {
func PushToAndroid(req PushNotification) bool {
var APIKey string

// check message
err := CheckMessage(req)

if err != nil {
LogError.Error("request error: " + err.Error())
return false
}

notification := GetAndroidNotification(req)

if APIKey = PushConf.Android.APIKey; req.APIKey != "" {
Expand Down
103 changes: 92 additions & 11 deletions gorush/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func TestPushToAndroidWrongAPIKey(t *testing.T) {

req := PushNotification{
Tokens: []string{"aaaaaa", "bbbbb"},
Platform: 2,
Platform: PlatFormAndroid,
Message: "Welcome",
}

Expand All @@ -257,7 +257,7 @@ func TestPushToAndroidWrongToken(t *testing.T) {

req := PushNotification{
Tokens: []string{"aaaaaa", "bbbbb"},
Platform: 2,
Platform: PlatFormAndroid,
Message: "Welcome",
}

Expand All @@ -277,7 +277,7 @@ func TestPushToAndroidRightTokenForJSONLog(t *testing.T) {

req := PushNotification{
Tokens: []string{androidToken, "bbbbb"},
Platform: 2,
Platform: PlatFormAndroid,
Message: "Welcome",
}

Expand All @@ -295,7 +295,7 @@ func TestPushToAndroidRightTokenForStringLog(t *testing.T) {

req := PushNotification{
Tokens: []string{androidToken, "bbbbb"},
Platform: 2,
Platform: PlatFormAndroid,
Message: "Welcome",
}

Expand All @@ -313,7 +313,7 @@ func TestOverwriteAndroidAPIKey(t *testing.T) {

req := PushNotification{
Tokens: []string{androidToken, "bbbbb"},
Platform: 2,
Platform: PlatFormAndroid,
Message: "Welcome",
// overwrite android api key
APIKey: "1234",
Expand Down Expand Up @@ -342,13 +342,13 @@ func TestSenMultipleNotifications(t *testing.T) {
//ios
{
Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
Platform: 1,
Platform: PlatFormIos,
Message: "Welcome",
},
// android
{
Tokens: []string{androidToken, "bbbbb"},
Platform: 2,
Platform: PlatFormAndroid,
Message: "Welcome",
},
},
Expand All @@ -375,13 +375,13 @@ func TestDisabledAndroidNotifications(t *testing.T) {
//ios
{
Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
Platform: 1,
Platform: PlatFormIos,
Message: "Welcome",
},
// android
{
Tokens: []string{androidToken, "bbbbb"},
Platform: 2,
Platform: PlatFormAndroid,
Message: "Welcome",
},
},
Expand All @@ -408,13 +408,13 @@ func TestDisabledIosNotifications(t *testing.T) {
//ios
{
Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
Platform: 1,
Platform: PlatFormIos,
Message: "Welcome",
},
// android
{
Tokens: []string{androidToken, "bbbbb"},
Platform: 2,
Platform: PlatFormAndroid,
Message: "Welcome",
},
},
Expand Down Expand Up @@ -454,3 +454,84 @@ func TestAPNSClientProdHost(t *testing.T) {

assert.Equal(t, apns2.HostProduction, ApnsClient.Host)
}

func TestGCMMessage(t *testing.T) {
var req PushNotification
var err error

// the message must not be empty
req = PushNotification{
Message: "",
}

err = CheckMessage(req)
assert.Error(t, err)

// the message must specify at least one registration ID
req = PushNotification{
Message: "Test",
Tokens: []string{},
}

err = CheckMessage(req)
assert.Error(t, err)

// the token must not be empty
req = PushNotification{
Message: "Test",
Tokens: []string{""},
}

err = CheckMessage(req)
assert.Error(t, err)

// the message may specify at most 1000 registration IDs
req = PushNotification{
Message: "Test",
Platform: PlatFormAndroid,
Tokens: make([]string, 1001),
}

err = CheckMessage(req)
assert.Error(t, err)

// the message's TimeToLive field must be an integer
// between 0 and 2419200 (4 weeks)
req = PushNotification{
Message: "Test",
Platform: PlatFormAndroid,
Tokens: []string{"XXXXXXXXX"},
TimeToLive: 2419201,
}

err = CheckMessage(req)
assert.Error(t, err)

// Pass
req = PushNotification{
Message: "Test",
Platform: PlatFormAndroid,
Tokens: []string{"XXXXXXXXX"},
TimeToLive: 86400,
}

err = CheckMessage(req)
assert.NoError(t, err)
}

func TestCheckAndroidMessage(t *testing.T) {
PushConf = BuildDefaultPushConf()

PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY")

req := PushNotification{
Tokens: []string{"aaaaaa", "bbbbb"},
Platform: PlatFormAndroid,
Message: "Welcome",
TimeToLive: 2419201,
}

success := PushToAndroid(req)
assert.False(t, success)
}
6 changes: 3 additions & 3 deletions gorush/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ func TestOutOfRangeMaxNotifications(t *testing.T) {
"notifications": []gofight.D{
{
"tokens": []string{"aaaaa", "bbbbb"},
"platform": 2,
"platform": PlatFormAndroid,
"message": "Welcome",
},
{
"tokens": []string{"aaaaa", "bbbbb"},
"platform": 2,
"platform": PlatFormAndroid,
"message": "Welcome",
},
},
Expand All @@ -187,7 +187,7 @@ func TestSuccessPushHandler(t *testing.T) {
"notifications": []gofight.D{
{
"tokens": []string{androidToken, "bbbbb"},
"platform": 2,
"platform": PlatFormAndroid,
"message": "Welcome",
},
},
Expand Down

0 comments on commit 4d91f4f

Please sign in to comment.