gorush/gorush/server_test.go

66 lines
1.3 KiB
Go
Raw Normal View History

package gopush
import (
"github.com/appleboy/gofight"
"github.com/buger/jsonparser"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
"runtime"
)
var go_version = runtime.Version()
func initTest() {
PushConf = BuildDefaultPushConf()
PushConf.Core.Mode = "test"
}
func TestRootHandler(t *testing.T) {
initTest()
r := gofight.New()
r.GET("/").
Run(GetMainEngine(), func(r gofight.HttpResponse, rq gofight.HttpRequest) {
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)
})
}
func TestAPIStatusHandler(t *testing.T) {
initTest()
r := gofight.New()
r.GET("/api/status").
Run(GetMainEngine(), func(r gofight.HttpResponse, rq gofight.HttpRequest) {
data := []byte(r.Body.String())
value, _ := jsonparser.GetString(data, "go_version")
assert.Equal(t, go_version, value)
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestPushHandler(t *testing.T) {
initTest()
r := gofight.New()
// missing some parameter.
r.POST("/api/push").
SetJSON(gofight.D{
"platform": 1,
}).
Run(GetMainEngine(), func(r gofight.HttpResponse, rq gofight.HttpRequest) {
assert.Equal(t, http.StatusBadRequest, r.Code)
})
}