2016-03-31 16:35:53 +00:00
|
|
|
package gopush
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/appleboy/gofight"
|
|
|
|
"github.com/buger/jsonparser"
|
2016-04-01 16:37:58 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2016-03-31 16:35:53 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"net/http"
|
2016-04-01 16:37:58 +00:00
|
|
|
"os"
|
2016-04-01 02:35:04 +00:00
|
|
|
"runtime"
|
2016-04-01 03:01:54 +00:00
|
|
|
"testing"
|
2016-04-01 16:37:58 +00:00
|
|
|
"time"
|
2016-03-31 16:35:53 +00:00
|
|
|
)
|
|
|
|
|
2016-04-01 02:35:04 +00:00
|
|
|
var go_version = runtime.Version()
|
|
|
|
|
2016-03-31 16:35:53 +00:00
|
|
|
func initTest() {
|
|
|
|
PushConf = BuildDefaultPushConf()
|
2016-04-01 01:53:10 +00:00
|
|
|
PushConf.Core.Mode = "test"
|
2016-03-31 16:35:53 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 14:15:10 +00:00
|
|
|
func TestPrintGoPushVersion(t *testing.T) {
|
|
|
|
PrintGoPushVersion()
|
|
|
|
}
|
|
|
|
|
2016-04-01 16:37:58 +00:00
|
|
|
func TestRunNormalServer(t *testing.T) {
|
|
|
|
initTest()
|
|
|
|
|
2016-04-03 13:05:32 +00:00
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
|
2016-04-01 16:37:58 +00:00
|
|
|
go func() {
|
|
|
|
assert.NoError(t, RunHTTPServer())
|
|
|
|
}()
|
|
|
|
// have to wait for the goroutine to start and run the server
|
|
|
|
// otherwise the main thread will complete
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
|
2016-04-04 15:05:16 +00:00
|
|
|
assert.Error(t, RunHTTPServer())
|
2016-04-02 17:11:09 +00:00
|
|
|
gofight.TestRequest(t, "http://localhost:8088/api/status")
|
2016-04-01 16:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// func TestRunTLSServer(t *testing.T) {
|
|
|
|
// initTest()
|
|
|
|
|
|
|
|
// PushConf.Core.SSL = true
|
|
|
|
// PushConf.Core.Port = "8087"
|
|
|
|
// PushConf.Core.CertPath = "../certificate/localhost.cert"
|
|
|
|
// PushConf.Core.KeyPath = "../certificate/localhost.key"
|
|
|
|
// router := gin.New()
|
|
|
|
|
|
|
|
// go func() {
|
|
|
|
// assert.NoError(t, RunHTTPServer())
|
|
|
|
// }()
|
|
|
|
// // have to wait for the goroutine to start and run the server
|
|
|
|
// // otherwise the main thread will complete
|
|
|
|
// time.Sleep(5 * time.Millisecond)
|
|
|
|
|
|
|
|
// assert.Error(t, router.Run(":8087"))
|
|
|
|
// testRequest(t, "https://localhost:8087/api/status")
|
|
|
|
// }
|
|
|
|
|
2016-04-01 01:53:10 +00:00
|
|
|
func TestRootHandler(t *testing.T) {
|
2016-03-31 16:35:53 +00:00
|
|
|
initTest()
|
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
r.GET("/").
|
2016-04-04 13:10:59 +00:00
|
|
|
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2016-03-31 16:35:53 +00:00
|
|
|
data := []byte(r.Body.String())
|
|
|
|
|
|
|
|
value, _ := jsonparser.GetString(data, "text")
|
|
|
|
|
|
|
|
assert.Equal(t, "Welcome to notification server.", value)
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|
2016-04-01 02:35:04 +00:00
|
|
|
|
|
|
|
func TestAPIStatusHandler(t *testing.T) {
|
|
|
|
initTest()
|
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
r.GET("/api/status").
|
2016-04-04 13:10:59 +00:00
|
|
|
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2016-04-01 02:35:04 +00:00
|
|
|
data := []byte(r.Body.String())
|
|
|
|
|
|
|
|
value, _ := jsonparser.GetString(data, "go_version")
|
|
|
|
|
|
|
|
assert.Equal(t, go_version, value)
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-01 03:58:34 +00:00
|
|
|
func TestMissingParameterPushHandler(t *testing.T) {
|
2016-04-01 02:35:04 +00:00
|
|
|
initTest()
|
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
// missing some parameter.
|
|
|
|
r.POST("/api/push").
|
|
|
|
SetJSON(gofight.D{
|
|
|
|
"platform": 1,
|
|
|
|
}).
|
2016-04-04 13:10:59 +00:00
|
|
|
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2016-04-01 02:35:04 +00:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
}
|
2016-04-01 03:58:34 +00:00
|
|
|
|
2016-04-01 09:14:23 +00:00
|
|
|
func TestDisabledIosPushHandler(t *testing.T) {
|
2016-04-01 03:58:34 +00:00
|
|
|
initTest()
|
|
|
|
|
|
|
|
PushConf.Ios.Enabled = false
|
|
|
|
InitAPNSClient()
|
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
r.POST("/api/push").
|
|
|
|
SetJSON(gofight.D{
|
2016-04-01 09:59:53 +00:00
|
|
|
"tokens": []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
|
|
|
|
"platform": 1,
|
|
|
|
"message": "Welcome",
|
|
|
|
}).
|
2016-04-04 13:10:59 +00:00
|
|
|
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2016-04-01 09:59:53 +00:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-01 15:06:29 +00:00
|
|
|
func TestMissingIosCertificate(t *testing.T) {
|
2016-04-01 09:59:53 +00:00
|
|
|
initTest()
|
|
|
|
|
|
|
|
PushConf.Ios.Enabled = true
|
2016-04-01 15:06:29 +00:00
|
|
|
PushConf.Ios.PemKeyPath = "test"
|
|
|
|
err := InitAPNSClient()
|
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIosPushDevelopment(t *testing.T) {
|
|
|
|
initTest()
|
|
|
|
|
|
|
|
PushConf.Ios.Enabled = true
|
|
|
|
PushConf.Ios.PemKeyPath = "../certificate/certificate-valid.pem"
|
|
|
|
InitAPNSClient()
|
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
r.POST("/api/push").
|
|
|
|
SetJSON(gofight.D{
|
|
|
|
"tokens": []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
|
|
|
|
"platform": 1,
|
|
|
|
"message": "Welcome",
|
|
|
|
}).
|
2016-04-04 13:10:59 +00:00
|
|
|
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2016-04-01 15:06:29 +00:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIosPushProduction(t *testing.T) {
|
|
|
|
initTest()
|
|
|
|
|
|
|
|
PushConf.Ios.Enabled = true
|
|
|
|
PushConf.Ios.Production = true
|
2016-04-01 09:59:53 +00:00
|
|
|
PushConf.Ios.PemKeyPath = "../certificate/certificate-valid.pem"
|
|
|
|
InitAPNSClient()
|
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
r.POST("/api/push").
|
|
|
|
SetJSON(gofight.D{
|
|
|
|
"tokens": []string{"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7"},
|
2016-04-01 03:58:34 +00:00
|
|
|
"platform": 1,
|
2016-04-01 09:14:23 +00:00
|
|
|
"message": "Welcome",
|
|
|
|
}).
|
2016-04-04 13:10:59 +00:00
|
|
|
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2016-04-01 09:14:23 +00:00
|
|
|
|
|
|
|
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",
|
2016-04-01 03:58:34 +00:00
|
|
|
}).
|
2016-04-04 13:10:59 +00:00
|
|
|
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2016-04-01 03:58:34 +00:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-03 13:05:32 +00:00
|
|
|
func TestAndroidPushHandler(t *testing.T) {
|
2016-04-01 14:15:10 +00:00
|
|
|
initTest()
|
|
|
|
|
|
|
|
PushConf.Android.Enabled = true
|
|
|
|
PushConf.Android.ApiKey = os.Getenv("ANDROID_API_KEY")
|
|
|
|
|
|
|
|
android_token := os.Getenv("ANDROID_TEST_TOKEN")
|
|
|
|
|
|
|
|
r := gofight.New()
|
|
|
|
|
|
|
|
r.POST("/api/push").
|
|
|
|
SetJSON(gofight.D{
|
|
|
|
"tokens": []string{android_token, "bbbbb"},
|
|
|
|
"platform": 2,
|
|
|
|
"message": "Welcome",
|
|
|
|
}).
|
2016-04-04 13:10:59 +00:00
|
|
|
Run(GetMainEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
|
2016-04-01 14:15:10 +00:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
|
|
|
}
|