Merge pull request #34 from appleboy/apikey
fix #32 support overwrite android api key from post field.
This commit is contained in:
commit
8298bb4405
|
@ -37,9 +37,9 @@ ios:
|
||||||
|
|
||||||
log:
|
log:
|
||||||
format: "string" # string or json
|
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"
|
access_level: "debug"
|
||||||
error_log: "stderr"
|
error_log: "stderr" # stderr: output to console, or define log path like "log/error_log"
|
||||||
error_level: "error"
|
error_level: "error"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,7 @@ type RequestPushNotification struct {
|
||||||
ContentAvailable bool `json:"content_available,omitempty"`
|
ContentAvailable bool `json:"content_available,omitempty"`
|
||||||
|
|
||||||
// Android
|
// Android
|
||||||
|
ApiKey string `json:"api_key,omitempty"`
|
||||||
To string `json:"to,omitempty"`
|
To string `json:"to,omitempty"`
|
||||||
CollapseKey string `json:"collapse_key,omitempty"`
|
CollapseKey string `json:"collapse_key,omitempty"`
|
||||||
DelayWhileIdle bool `json:"delay_while_idle,omitempty"`
|
DelayWhileIdle bool `json:"delay_while_idle,omitempty"`
|
||||||
|
@ -311,10 +312,15 @@ func GetAndroidNotification(req RequestPushNotification) gcm.HttpMessage {
|
||||||
}
|
}
|
||||||
|
|
||||||
func PushToAndroid(req RequestPushNotification) bool {
|
func PushToAndroid(req RequestPushNotification) bool {
|
||||||
|
var apiKey string
|
||||||
|
|
||||||
notification := GetAndroidNotification(req)
|
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 {
|
if err != nil {
|
||||||
// GCM server error
|
// GCM server error
|
||||||
|
|
|
@ -261,7 +261,27 @@ func TestPushToAndroidWrongToken(t *testing.T) {
|
||||||
assert.True(t, success)
|
assert.True(t, success)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPushToAndroidRightToken(t *testing.T) {
|
func TestPushToAndroidRightTokenForJSONLog(t *testing.T) {
|
||||||
|
PushConf = BuildDefaultPushConf()
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
req := RequestPushNotification{
|
||||||
|
Tokens: []string{android_token, "bbbbb"},
|
||||||
|
Platform: 2,
|
||||||
|
Message: "Welcome",
|
||||||
|
}
|
||||||
|
|
||||||
|
success := PushToAndroid(req)
|
||||||
|
assert.True(t, success)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPushToAndroidRightTokenForStringLog(t *testing.T) {
|
||||||
PushConf = BuildDefaultPushConf()
|
PushConf = BuildDefaultPushConf()
|
||||||
|
|
||||||
PushConf.Android.Enabled = true
|
PushConf.Android.Enabled = true
|
||||||
|
@ -278,3 +298,23 @@ func TestPushToAndroidRightToken(t *testing.T) {
|
||||||
success := PushToAndroid(req)
|
success := PushToAndroid(req)
|
||||||
assert.True(t, success)
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -205,8 +205,6 @@ func TestHalfSuccessAndroidPushHandler(t *testing.T) {
|
||||||
|
|
||||||
PushConf.Android.Enabled = true
|
PushConf.Android.Enabled = true
|
||||||
PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY")
|
PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY")
|
||||||
// log for json
|
|
||||||
PushConf.Log.Format = "json"
|
|
||||||
|
|
||||||
android_token := os.Getenv("ANDROID_TEST_TOKEN")
|
android_token := os.Getenv("ANDROID_TEST_TOKEN")
|
||||||
|
|
||||||
|
@ -229,8 +227,6 @@ func TestAllSuccessAndroidPushHandler(t *testing.T) {
|
||||||
|
|
||||||
PushConf.Android.Enabled = true
|
PushConf.Android.Enabled = true
|
||||||
PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY")
|
PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY")
|
||||||
// log for json
|
|
||||||
PushConf.Log.Format = "json"
|
|
||||||
|
|
||||||
android_token := os.Getenv("ANDROID_TEST_TOKEN")
|
android_token := os.Getenv("ANDROID_TEST_TOKEN")
|
||||||
|
|
||||||
|
@ -246,7 +242,4 @@ func TestAllSuccessAndroidPushHandler(t *testing.T) {
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, r.Code)
|
assert.Equal(t, http.StatusOK, r.Code)
|
||||||
})
|
})
|
||||||
|
|
||||||
// wait push response
|
|
||||||
time.Sleep(3000 * time.Millisecond)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue