From 09a90e28fa64f8abebee26d3f490a1a5572253a6 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 9 Apr 2016 14:06:03 +0800 Subject: [PATCH 1/2] fix #32 support overwrite android api key from post field. Signed-off-by: Bo-Yi Wu --- README.md | 4 ++-- gorush/notification.go | 8 +++++++- gorush/notification_test.go | 22 ++++++++++++++++++++++ gorush/server_test.go | 5 ----- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 759bd41..0214e4c 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,9 @@ ios: log: format: "string" # string or json - access_log: "stdout" + access_log: "stdout" # stdout: output to console, or define log path like "log/access_log" access_level: "debug" - error_log: "stderr" + error_log: "stderr" # stderr: output to console, or define log path like "log/error_log" error_level: "error" ``` diff --git a/gorush/notification.go b/gorush/notification.go index 2861c6e..e84d5e6 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -50,6 +50,7 @@ type RequestPushNotification struct { ContentAvailable bool `json:"content_available,omitempty"` // Android + ApiKey string `json:"api_key,omitempty"` To string `json:"to,omitempty"` CollapseKey string `json:"collapse_key,omitempty"` DelayWhileIdle bool `json:"delay_while_idle,omitempty"` @@ -311,10 +312,15 @@ func GetAndroidNotification(req RequestPushNotification) gcm.HttpMessage { } func PushToAndroid(req RequestPushNotification) bool { + var apiKey string notification := GetAndroidNotification(req) - res, err := gcm.SendHttp(PushConf.Android.ApiKey, notification) + if apiKey = PushConf.Android.ApiKey; req.ApiKey != "" { + apiKey = req.ApiKey + } + + res, err := gcm.SendHttp(apiKey, notification) if err != nil { // GCM server error diff --git a/gorush/notification_test.go b/gorush/notification_test.go index 0445798..17c5697 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -266,6 +266,8 @@ func TestPushToAndroidRightToken(t *testing.T) { PushConf.Android.Enabled = true PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY") + // log for json + PushConf.Log.Format = "json" android_token := os.Getenv("ANDROID_TEST_TOKEN") @@ -278,3 +280,23 @@ func TestPushToAndroidRightToken(t *testing.T) { success := PushToAndroid(req) assert.True(t, success) } + +func TestOverwriteAndroidApiKey(t *testing.T) { + PushConf = BuildDefaultPushConf() + + PushConf.Android.Enabled = true + PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY") + + android_token := os.Getenv("ANDROID_TEST_TOKEN") + + req := RequestPushNotification{ + Tokens: []string{android_token, "bbbbb"}, + Platform: 2, + Message: "Welcome", + // overwrite android api key + ApiKey: "1234", + } + + success := PushToAndroid(req) + assert.False(t, success) +} diff --git a/gorush/server_test.go b/gorush/server_test.go index f6fd780..0049695 100644 --- a/gorush/server_test.go +++ b/gorush/server_test.go @@ -229,8 +229,6 @@ func TestAllSuccessAndroidPushHandler(t *testing.T) { PushConf.Android.Enabled = true PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY") - // log for json - PushConf.Log.Format = "json" android_token := os.Getenv("ANDROID_TEST_TOKEN") @@ -246,7 +244,4 @@ func TestAllSuccessAndroidPushHandler(t *testing.T) { assert.Equal(t, http.StatusOK, r.Code) }) - - // wait push response - time.Sleep(3000 * time.Millisecond) } From 86002dd4be416b2e1a613e0b68cebd0fb2380f3a Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 9 Apr 2016 14:16:36 +0800 Subject: [PATCH 2/2] testing string format of log. Signed-off-by: Bo-Yi Wu --- gorush/notification_test.go | 20 +++++++++++++++++++- gorush/server_test.go | 2 -- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/gorush/notification_test.go b/gorush/notification_test.go index 17c5697..c4c45b0 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -261,7 +261,7 @@ func TestPushToAndroidWrongToken(t *testing.T) { assert.True(t, success) } -func TestPushToAndroidRightToken(t *testing.T) { +func TestPushToAndroidRightTokenForJSONLog(t *testing.T) { PushConf = BuildDefaultPushConf() PushConf.Android.Enabled = true @@ -281,6 +281,24 @@ func TestPushToAndroidRightToken(t *testing.T) { assert.True(t, success) } +func TestPushToAndroidRightTokenForStringLog(t *testing.T) { + PushConf = BuildDefaultPushConf() + + PushConf.Android.Enabled = true + PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY") + + android_token := os.Getenv("ANDROID_TEST_TOKEN") + + req := RequestPushNotification{ + Tokens: []string{android_token, "bbbbb"}, + Platform: 2, + Message: "Welcome", + } + + success := PushToAndroid(req) + assert.True(t, success) +} + func TestOverwriteAndroidApiKey(t *testing.T) { PushConf = BuildDefaultPushConf() diff --git a/gorush/server_test.go b/gorush/server_test.go index 0049695..b108dda 100644 --- a/gorush/server_test.go +++ b/gorush/server_test.go @@ -205,8 +205,6 @@ func TestHalfSuccessAndroidPushHandler(t *testing.T) { PushConf.Android.Enabled = true PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY") - // log for json - PushConf.Log.Format = "json" android_token := os.Getenv("ANDROID_TEST_TOKEN")