From 518e7301cd2435423376012414a4953c67a714e4 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 2 Apr 2016 00:37:58 +0800 Subject: [PATCH] Add tls and normal server listen testing. Signed-off-by: Bo-Yi Wu --- .gitignore | 2 -- certificate/localhost.cert | 18 ++++++++++++++ certificate/localhost.key | 27 ++++++++++++++++++++ gorush/server.go | 9 ++++--- gorush/server_test.go | 51 +++++++++++++++++++++++++++++++++++++- 5 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 certificate/localhost.cert create mode 100644 certificate/localhost.key diff --git a/.gitignore b/.gitignore index 63082bc..611c651 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,4 @@ key.pem config.yaml bin/* .DS_Store -*.cert -*.key coverage.out diff --git a/certificate/localhost.cert b/certificate/localhost.cert new file mode 100644 index 0000000..2471fc2 --- /dev/null +++ b/certificate/localhost.cert @@ -0,0 +1,18 @@ +-----BEGIN CERTIFICATE----- +MIIC+zCCAeOgAwIBAgIJALbZEDvUQrFKMA0GCSqGSIb3DQEBBQUAMBQxEjAQBgNV +BAMMCWxvY2FsaG9zdDAeFw0xNjAzMjgwMzMwNDFaFw0yNjAzMjYwMzMwNDFaMBQx +EjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC +ggEBAMj1+xg4jVLzVnB5j7n1ul30WEE4BCzcNFxg5AOB5H5q+wje0YYiVFg6PQyv +GCipqIRXVRdVQ1hHSeunYGKe8lq3Sb1X8PUJ12v9uRbpS9DK1Owqk8rsPDu6sVTL +qKKgH1Z8yazzaS0AbXuA5e9gO/RzijbnpEP+quM4dueiMPVEJyLq+EoIQY+MM8MP +8dZzL4XZl7wL4UsCN7rPcO6W3tlnT0iO3h9c/Ym2hFhz+KNJ9KRRCvtPGZESigtK +bHsXH099WDo8v/Wp5/evBw/+JD0opxmCfHIBALHt9v53RvvsDZ1t33Rpu5C8znEY +Y2Ay7NgxhqjqoWJqA48lJeA0clsCAwEAAaNQME4wHQYDVR0OBBYEFC0bTU1Xofeh +NKIelashIsqKidDYMB8GA1UdIwQYMBaAFC0bTU1XofehNKIelashIsqKidDYMAwG +A1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAAiJL8IMTwNX9XqQWYDFgkG4 +AnrVwQhreAqC9rSxDCjqqnMHPHGzcCeDMLAMoh0kOy20nowUGNtCZ0uBvnX2q1bN +g1jt+GBcLJDR3LL4CpNOlm3YhOycuNfWMxTA7BXkmnSrZD/7KhArsBEY8aulxwKJ +HRgNlIwe1oFD1YdX1BS5pp4t25B6Vq4A3FMMUkVoWE688nE168hvQgwjrHkgHhwe +eN8lGE2DhFraXnWmDMdwaHD3HRFGhyppIFN+f7BqbWX9gM+T2YRTfObIXLWbqJLD +3Mk/NkxqVcg4eY54wJ1ufCUGAYAIaY6fQqiNUz8nhwK3t45NBVT9y/uJXqnTLyY= +-----END CERTIFICATE----- diff --git a/certificate/localhost.key b/certificate/localhost.key new file mode 100644 index 0000000..4d28833 --- /dev/null +++ b/certificate/localhost.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEAyPX7GDiNUvNWcHmPufW6XfRYQTgELNw0XGDkA4Hkfmr7CN7R +hiJUWDo9DK8YKKmohFdVF1VDWEdJ66dgYp7yWrdJvVfw9QnXa/25FulL0MrU7CqT +yuw8O7qxVMuooqAfVnzJrPNpLQBte4Dl72A79HOKNuekQ/6q4zh256Iw9UQnIur4 +SghBj4wzww/x1nMvhdmXvAvhSwI3us9w7pbe2WdPSI7eH1z9ibaEWHP4o0n0pFEK ++08ZkRKKC0psexcfT31YOjy/9ann968HD/4kPSinGYJ8cgEAse32/ndG++wNnW3f +dGm7kLzOcRhjYDLs2DGGqOqhYmoDjyUl4DRyWwIDAQABAoIBAGTKqsN9KbSfA42q +CqI0UuLouJMNa1qsnz5uAi6YKWgWdA4A44mpEjCmFRSVhUJvxWuK+cyYIQzXxIWD +D16n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TgZGPoVRetft2LDFLnPxp2sEH5OFLsQ +R+K/kAOul8eStWuMXOFA9pMzGkGEgIFJMJOyaJON3kedQI8deCM= +-----END RSA PRIVATE KEY----- diff --git a/gorush/server.go b/gorush/server.go index 5fc9e0e..e325cf9 100644 --- a/gorush/server.go +++ b/gorush/server.go @@ -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 } diff --git a/gorush/server_test.go b/gorush/server_test.go index 11019c4..6518389 100644 --- a/gorush/server_test.go +++ b/gorush/server_test.go @@ -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()