add android payload testing.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-04-01 17:14:23 +08:00
parent a41e63bcb7
commit 5bd75c8624
3 changed files with 101 additions and 37 deletions

View File

@ -112,11 +112,13 @@ func pushNotification(notification RequestPushNotification) bool {
return success
}
// The iOS Notification Payload
// ref: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html
func GetIOSNotification(req RequestPushNotification) *apns.Notification {
notification := &apns.Notification{}
if len(req.ApnsID) > 0 {
notification.ApnsID = req.ApnsID
notification.ApnsID = req.ApnsID
}
if len(req.Topic) > 0 {
@ -157,6 +159,7 @@ func GetIOSNotification(req RequestPushNotification) *apns.Notification {
payload.AlertTitleLocKey(req.Alert.TitleLocKey)
}
// Need send PR to apns2 repo.
// if len(req.Alert.LocArgs) > 0 {
// payload.AlertLocArgs(req.Alert.LocArgs)
// }
@ -204,8 +207,6 @@ func pushNotificationIos(req RequestPushNotification) bool {
notification := GetIOSNotification(req)
// The Remote Notification Payload
// https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html
for _, token := range req.Tokens {
notification.DeviceToken = token
@ -228,10 +229,9 @@ func pushNotificationIos(req RequestPushNotification) bool {
return true
}
func pushNotificationAndroid(req RequestPushNotification) bool {
// HTTP Connection Server Reference for Android
// https://developers.google.com/cloud-messaging/http-server-ref
// HTTP Connection Server Reference for Android
// https://developers.google.com/cloud-messaging/http-server-ref
func GetAndroidNotification(req RequestPushNotification) gcm.HttpMessage {
notification := gcm.HttpMessage{}
notification.RegistrationIds = req.Tokens
@ -279,6 +279,13 @@ func pushNotificationAndroid(req RequestPushNotification) bool {
notification.Notification.Body = req.Message
}
return notification
}
func pushNotificationAndroid(req RequestPushNotification) bool {
notification := GetAndroidNotification(req)
res, err := gcm.SendHttp(PushConf.Android.ApiKey, notification)
if err != nil {

View File

@ -1,11 +1,12 @@
package gopush
import (
"github.com/stretchr/testify/assert"
"github.com/buger/jsonparser"
"encoding/json"
"testing"
"github.com/buger/jsonparser"
"github.com/google/go-gcm"
"github.com/stretchr/testify/assert"
"log"
"testing"
)
func TestIOSNotificationStructure(t *testing.T) {
@ -14,25 +15,25 @@ func TestIOSNotificationStructure(t *testing.T) {
test := "test"
message := "Welcome notification Server"
req := RequestPushNotification{
ApnsID: test,
Topic: test,
Priority: "normal",
Message: message,
Badge: 1,
Sound: test,
ApnsID: test,
Topic: test,
Priority: "normal",
Message: message,
Badge: 1,
Sound: test,
ContentAvailable: true,
Extend: []ExtendJSON{
{
Key: "key1",
Key: "key1",
Value: "1",
},
{
Key: "key2",
Key: "key2",
Value: "2",
},
},
Category: test,
URLArgs: []string{"a", "b"},
URLArgs: []string{"a", "b"},
}
notification := GetIOSNotification(req)
@ -75,15 +76,15 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
test := "test"
req := RequestPushNotification{
Alert: Alert{
Action: test,
Action: test,
ActionLocKey: test,
Body: test,
LaunchImage: test,
LocArgs: []string{"a", "b"},
LocKey: test,
Title: test,
Body: test,
LaunchImage: test,
LocArgs: []string{"a", "b"},
LocKey: test,
Title: test,
TitleLocArgs: []string{"a", "b"},
TitleLocKey: test,
TitleLocKey: test,
},
}
@ -98,12 +99,12 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
}
action, _ := jsonparser.GetString(data, "aps", "alert", "action")
actionLocKey, _ := jsonparser.GetString(data, "aps", "alert","action-loc-key")
body, _ := jsonparser.GetString(data, "aps", "alert","body")
launchImage, _ := jsonparser.GetString(data, "aps", "alert","launch-image")
locKey, _ := jsonparser.GetString(data, "aps", "alert","loc-key")
title, _ := jsonparser.GetString(data, "aps", "alert","title")
titleLocKey, _ := jsonparser.GetString(data, "aps", "alert","title-loc-key")
actionLocKey, _ := jsonparser.GetString(data, "aps", "alert", "action-loc-key")
body, _ := jsonparser.GetString(data, "aps", "alert", "body")
launchImage, _ := jsonparser.GetString(data, "aps", "alert", "launch-image")
locKey, _ := jsonparser.GetString(data, "aps", "alert", "loc-key")
title, _ := jsonparser.GetString(data, "aps", "alert", "title")
titleLocKey, _ := jsonparser.GetString(data, "aps", "alert", "title-loc-key")
aps := dat["aps"].(map[string]interface{})
alert := aps["alert"].(map[string]interface{})
titleLocArgs := alert["title-loc-args"].([]interface{})
@ -118,3 +119,40 @@ func TestIOSAlertNotificationStructure(t *testing.T) {
assert.Contains(t, titleLocArgs, "a")
assert.Contains(t, titleLocArgs, "b")
}
func TestAndroidNotificationStructure(t *testing.T) {
test := "test"
req := RequestPushNotification{
Tokens: []string{"a", "b"},
Message: "Welcome",
To: test,
Priority: "high",
CollapseKey: "1",
ContentAvailable: true,
DelayWhileIdle: true,
TimeToLive: 100,
RestrictedPackageName: test,
DryRun: true,
Data: map[string]interface{}{
"a": "1",
"b": "2",
},
Notification: gcm.Notification{
Title: test,
},
}
notification := GetAndroidNotification(req)
assert.Equal(t, test, notification.To)
assert.Equal(t, "high", notification.Priority)
assert.Equal(t, "1", notification.CollapseKey)
assert.True(t, notification.ContentAvailable)
assert.True(t, notification.DelayWhileIdle)
assert.Equal(t, 100, int(notification.TimeToLive))
assert.Equal(t, test, notification.RestrictedPackageName)
assert.True(t, notification.DryRun)
assert.Equal(t, test, notification.Notification.Title)
assert.Equal(t, "Welcome", notification.Notification.Body)
}

View File

@ -64,7 +64,7 @@ func TestMissingParameterPushHandler(t *testing.T) {
})
}
func TestIosPushHandler(t *testing.T) {
func TestDisabledIosPushHandler(t *testing.T) {
initTest()
PushConf.Ios.Enabled = false
@ -74,9 +74,28 @@ func TestIosPushHandler(t *testing.T) {
r.POST("/api/push").
SetJSON(gofight.D{
"tokens": []string{"dc0ca2819417e528d8a4a01fc3e6bc7d2518ce738930c3b11203c0ef7c0fab8c"},
"tokens": []string{"dc0ca2819417e528d8a4a01fc3e6bc7d2518ce738930c3b11203c0ef7c0fab8c"},
"platform": 1,
"message": "Welcome",
"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) {
@ -93,9 +112,9 @@ func TestAndroidPushHandler(t *testing.T) {
r.POST("/api/push").
SetJSON(gofight.D{
"tokens": []string{"aaaaaa", "bbbbb"},
"tokens": []string{"aaaaaa", "bbbbb"},
"platform": 2,
"message": "Welcome",
"message": "Welcome",
}).
Run(GetMainEngine(), func(r gofight.HttpResponse, rq gofight.HttpRequest) {