Add tls and normal server listen testing.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu
2016-04-02 00:37:58 +08:00
parent fa22f2c655
commit 518e7301cd
5 changed files with 101 additions and 6 deletions

View File

@@ -56,10 +56,13 @@ func GetMainEngine() *gin.Engine {
return r
}
func RunHTTPServer() {
func RunHTTPServer() error {
var err error
if PushConf.Core.SSL && PushConf.Core.CertPath != "" && PushConf.Core.KeyPath != "" {
GetMainEngine().RunTLS(":"+PushConf.Core.Port, PushConf.Core.CertPath, PushConf.Core.KeyPath)
err = GetMainEngine().RunTLS(":"+PushConf.Core.Port, PushConf.Core.CertPath, PushConf.Core.KeyPath)
} else {
GetMainEngine().Run(":" + PushConf.Core.Port)
err = GetMainEngine().Run(":" + PushConf.Core.Port)
}
return err
}

View File

@@ -3,11 +3,14 @@ package gopush
import (
"github.com/appleboy/gofight"
"github.com/buger/jsonparser"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"os"
"runtime"
"testing"
"os"
"time"
)
var go_version = runtime.Version()
@@ -17,10 +20,56 @@ func initTest() {
PushConf.Core.Mode = "test"
}
func testRequest(t *testing.T, url string) {
resp, err := http.Get(url)
defer resp.Body.Close()
assert.NoError(t, err)
_, ioerr := ioutil.ReadAll(resp.Body)
assert.NoError(t, ioerr)
assert.Equal(t, "200 OK", resp.Status, "should get a 200")
}
func TestPrintGoPushVersion(t *testing.T) {
PrintGoPushVersion()
}
func TestRunNormalServer(t *testing.T) {
initTest()
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(":8088"))
testRequest(t, "http://localhost:8088/api/status")
}
// 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")
// }
func TestRootHandler(t *testing.T) {
initTest()