fix constant variable.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-04-24 15:54:41 +08:00
parent b921030688
commit 28bdc4a456
4 changed files with 50 additions and 30 deletions

View File

@ -72,9 +72,6 @@ func main() {
// send android notification // send android notification
if *android { if *android {
checkInput(*token, *message)
gorush.PushConf.Android.Enabled = true gorush.PushConf.Android.Enabled = true
req := gorush.PushNotification{ req := gorush.PushNotification{
Tokens: []string{*token}, Tokens: []string{*token},
@ -82,7 +79,7 @@ func main() {
Message: *message, Message: *message,
} }
err := gorush.CheckGCMMessage(req) err := gorush.CheckMessage(req)
if err != nil { if err != nil {
gorush.LogError.Fatal(err) gorush.LogError.Fatal(err)
@ -96,8 +93,6 @@ func main() {
// send android notification // send android notification
if *ios { if *ios {
checkInput(*token, *message)
if *production { if *production {
gorush.PushConf.Ios.Production = true gorush.PushConf.Ios.Production = true
} }
@ -109,6 +104,12 @@ func main() {
Message: *message, Message: *message,
} }
err := gorush.CheckMessage(req)
if err != nil {
gorush.LogError.Fatal(err)
}
gorush.InitAppStatus() gorush.InitAppStatus()
gorush.InitAPNSClient() gorush.InitAPNSClient()
gorush.PushToIOS(req) gorush.PushToIOS(req)

View File

@ -78,7 +78,7 @@ type PushNotification struct {
} }
// CheckGCMMessage for check GCM Message // CheckGCMMessage for check GCM Message
func CheckGCMMessage(req PushNotification) error { func CheckMessage(req PushNotification) error {
var msg string var msg string
if req.Message == "" { if req.Message == "" {
msg = "the message must not be empty" msg = "the message must not be empty"
@ -92,13 +92,19 @@ func CheckGCMMessage(req PushNotification) error {
return errors.New(msg) return errors.New(msg)
} }
if len(req.Tokens) > 1000 { 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" msg = "the message may specify at most 1000 registration IDs"
LogAccess.Debug(msg) LogAccess.Debug(msg)
return errors.New(msg) return errors.New(msg)
} }
if req.TimeToLive < 0 || 2419200 < req.TimeToLive { if req.Platform == PlatFormAndroid && (req.TimeToLive < 0 || 2419200 < req.TimeToLive) {
msg = "the message's TimeToLive field must be an integer " + msg = "the message's TimeToLive field must be an integer " +
"between 0 and 2419200 (4 weeks)" "between 0 and 2419200 (4 weeks)"
LogAccess.Debug(msg) LogAccess.Debug(msg)
@ -380,9 +386,10 @@ func PushToAndroid(req PushNotification) bool {
var APIKey string var APIKey string
// check message // check message
err := CheckGCMMessage(req) err := CheckMessage(req)
if err != nil { if err != nil {
LogError.Error("request error: " + err.Error())
return false return false
} }

View File

@ -241,7 +241,7 @@ func TestPushToAndroidWrongAPIKey(t *testing.T) {
req := PushNotification{ req := PushNotification{
Tokens: []string{"aaaaaa", "bbbbb"}, Tokens: []string{"aaaaaa", "bbbbb"},
Platform: 2, Platform: PlatFormAndroid,
Message: "Welcome", Message: "Welcome",
} }
@ -257,7 +257,7 @@ func TestPushToAndroidWrongToken(t *testing.T) {
req := PushNotification{ req := PushNotification{
Tokens: []string{"aaaaaa", "bbbbb"}, Tokens: []string{"aaaaaa", "bbbbb"},
Platform: 2, Platform: PlatFormAndroid,
Message: "Welcome", Message: "Welcome",
} }
@ -277,7 +277,7 @@ func TestPushToAndroidRightTokenForJSONLog(t *testing.T) {
req := PushNotification{ req := PushNotification{
Tokens: []string{androidToken, "bbbbb"}, Tokens: []string{androidToken, "bbbbb"},
Platform: 2, Platform: PlatFormAndroid,
Message: "Welcome", Message: "Welcome",
} }
@ -295,7 +295,7 @@ func TestPushToAndroidRightTokenForStringLog(t *testing.T) {
req := PushNotification{ req := PushNotification{
Tokens: []string{androidToken, "bbbbb"}, Tokens: []string{androidToken, "bbbbb"},
Platform: 2, Platform: PlatFormAndroid,
Message: "Welcome", Message: "Welcome",
} }
@ -313,7 +313,7 @@ func TestOverwriteAndroidAPIKey(t *testing.T) {
req := PushNotification{ req := PushNotification{
Tokens: []string{androidToken, "bbbbb"}, Tokens: []string{androidToken, "bbbbb"},
Platform: 2, Platform: PlatFormAndroid,
Message: "Welcome", Message: "Welcome",
// overwrite android api key // overwrite android api key
APIKey: "1234", APIKey: "1234",
@ -342,13 +342,13 @@ func TestSenMultipleNotifications(t *testing.T) {
//ios //ios
{ {
Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"}, Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
Platform: 1, Platform: PlatFormIos,
Message: "Welcome", Message: "Welcome",
}, },
// android // android
{ {
Tokens: []string{androidToken, "bbbbb"}, Tokens: []string{androidToken, "bbbbb"},
Platform: 2, Platform: PlatFormAndroid,
Message: "Welcome", Message: "Welcome",
}, },
}, },
@ -375,13 +375,13 @@ func TestDisabledAndroidNotifications(t *testing.T) {
//ios //ios
{ {
Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"}, Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
Platform: 1, Platform: PlatFormIos,
Message: "Welcome", Message: "Welcome",
}, },
// android // android
{ {
Tokens: []string{androidToken, "bbbbb"}, Tokens: []string{androidToken, "bbbbb"},
Platform: 2, Platform: PlatFormAndroid,
Message: "Welcome", Message: "Welcome",
}, },
}, },
@ -408,13 +408,13 @@ func TestDisabledIosNotifications(t *testing.T) {
//ios //ios
{ {
Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"}, Tokens: []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
Platform: 1, Platform: PlatFormIos,
Message: "Welcome", Message: "Welcome",
}, },
// android // android
{ {
Tokens: []string{androidToken, "bbbbb"}, Tokens: []string{androidToken, "bbbbb"},
Platform: 2, Platform: PlatFormAndroid,
Message: "Welcome", Message: "Welcome",
}, },
}, },
@ -464,7 +464,7 @@ func TestGCMMessage(t *testing.T) {
Message: "", Message: "",
} }
err = CheckGCMMessage(req) err = CheckMessage(req)
assert.Error(t, err) assert.Error(t, err)
// the message must specify at least one registration ID // the message must specify at least one registration ID
@ -473,37 +473,49 @@ func TestGCMMessage(t *testing.T) {
Tokens: []string{}, Tokens: []string{},
} }
err = CheckGCMMessage(req) 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) assert.Error(t, err)
// the message may specify at most 1000 registration IDs // the message may specify at most 1000 registration IDs
req = PushNotification{ req = PushNotification{
Message: "Test", Message: "Test",
Platform: PlatFormAndroid,
Tokens: make([]string, 1001), Tokens: make([]string, 1001),
} }
err = CheckGCMMessage(req) err = CheckMessage(req)
assert.Error(t, err) assert.Error(t, err)
// the message's TimeToLive field must be an integer // the message's TimeToLive field must be an integer
// between 0 and 2419200 (4 weeks) // between 0 and 2419200 (4 weeks)
req = PushNotification{ req = PushNotification{
Message: "Test", Message: "Test",
Platform: PlatFormAndroid,
Tokens: []string{"XXXXXXXXX"}, Tokens: []string{"XXXXXXXXX"},
TimeToLive: 2419201, TimeToLive: 2419201,
} }
err = CheckGCMMessage(req) err = CheckMessage(req)
assert.Error(t, err) assert.Error(t, err)
// Pass // Pass
req = PushNotification{ req = PushNotification{
Message: "Test", Message: "Test",
Platform: PlatFormAndroid,
Tokens: []string{"XXXXXXXXX"}, Tokens: []string{"XXXXXXXXX"},
TimeToLive: 86400, TimeToLive: 86400,
} }
err = CheckGCMMessage(req) err = CheckMessage(req)
assert.NoError(t, err) assert.NoError(t, err)
} }
@ -515,7 +527,7 @@ func TestCheckAndroidMessage(t *testing.T) {
req := PushNotification{ req := PushNotification{
Tokens: []string{"aaaaaa", "bbbbb"}, Tokens: []string{"aaaaaa", "bbbbb"},
Platform: 2, Platform: PlatFormAndroid,
Message: "Welcome", Message: "Welcome",
TimeToLive: 2419201, TimeToLive: 2419201,
} }

View File

@ -156,12 +156,12 @@ func TestOutOfRangeMaxNotifications(t *testing.T) {
"notifications": []gofight.D{ "notifications": []gofight.D{
{ {
"tokens": []string{"aaaaa", "bbbbb"}, "tokens": []string{"aaaaa", "bbbbb"},
"platform": 2, "platform": PlatFormAndroid,
"message": "Welcome", "message": "Welcome",
}, },
{ {
"tokens": []string{"aaaaa", "bbbbb"}, "tokens": []string{"aaaaa", "bbbbb"},
"platform": 2, "platform": PlatFormAndroid,
"message": "Welcome", "message": "Welcome",
}, },
}, },
@ -187,7 +187,7 @@ func TestSuccessPushHandler(t *testing.T) {
"notifications": []gofight.D{ "notifications": []gofight.D{
{ {
"tokens": []string{androidToken, "bbbbb"}, "tokens": []string{androidToken, "bbbbb"},
"platform": 2, "platform": PlatFormAndroid,
"message": "Welcome", "message": "Welcome",
}, },
}, },