From 3e26a10438bbc58c358929b1b379d47fe4869410 Mon Sep 17 00:00:00 2001 From: syucream Date: Mon, 25 Sep 2017 19:49:01 +0900 Subject: [PATCH 1/2] Add test cases for server.go --- gaurun/server_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 gaurun/server_test.go diff --git a/gaurun/server_test.go b/gaurun/server_test.go new file mode 100644 index 0000000..52de03e --- /dev/null +++ b/gaurun/server_test.go @@ -0,0 +1,63 @@ +package gaurun + +import ( + "net/http" + "net/url" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRegisterHandlers(t *testing.T) { + mux := http.NewServeMux() + + RegisterHandlers(mux) + + entrypoints := []string{ + "/push", + "/stat/app", + "/config/pushers", + "/stat/go", + } + + for _, e := range entrypoints { + _, pattern := mux.Handler(&http.Request{ + Method: "GET", Host: "localhost", URL: &url.URL{Path: e}, + }) + assert.Equal(t, e, pattern) + } +} + +func TestGetListener(t *testing.T) { + validConfig := ConfToml{ + Core: SectionCore{Port: "8080"}, + } + invalidConfigs := []ConfToml{ + // port is empty + {}, + // port is not listenable + {Core: SectionCore{Port: "100000"}}, + // port is invalid UNIX socket + {Core: SectionCore{Port: "unix:/invalid"}}, + // port specified neither TCP port nor UNIX socket + {Core: SectionCore{Port: "invalid:/invalid"}}, + } + + _, err := getListener(&validConfig) + assert.Nil(t, err) + + for _, c := range invalidConfigs { + _, err := getListener(&c) + assert.NotNil(t, err) + } +} + +func TestRunServer(t *testing.T) { + server := &http.Server{} + + invalidConfig := &ConfToml{ + Core: SectionCore{Port: "invalid:/invalid"}, + } + + assert.NotNil(t, RunServer(server, invalidConfig)) +} From e6b8301fac5cd5d4de886bd054707e85a6c6245e Mon Sep 17 00:00:00 2001 From: syucream Date: Fri, 29 Sep 2017 18:04:39 +0900 Subject: [PATCH 2/2] Fix and remove some test cases --- gaurun/server_test.go | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/gaurun/server_test.go b/gaurun/server_test.go index 52de03e..e2c3908 100644 --- a/gaurun/server_test.go +++ b/gaurun/server_test.go @@ -29,35 +29,26 @@ func TestRegisterHandlers(t *testing.T) { } func TestGetListener(t *testing.T) { - validConfig := ConfToml{ - Core: SectionCore{Port: "8080"}, + validConfigs := []ConfToml{ + {Core: SectionCore{Port: "8080"}}, + {Core: SectionCore{Port: "unix:/tmp/gaurun.sock"}}, } invalidConfigs := []ConfToml{ // port is empty {}, // port is not listenable {Core: SectionCore{Port: "100000"}}, - // port is invalid UNIX socket - {Core: SectionCore{Port: "unix:/invalid"}}, // port specified neither TCP port nor UNIX socket {Core: SectionCore{Port: "invalid:/invalid"}}, } - _, err := getListener(&validConfig) - assert.Nil(t, err) + for _, c := range validConfigs { + _, err := getListener(&c) + assert.Nil(t, err) + } for _, c := range invalidConfigs { _, err := getListener(&c) assert.NotNil(t, err) } } - -func TestRunServer(t *testing.T) { - server := &http.Server{} - - invalidConfig := &ConfToml{ - Core: SectionCore{Port: "invalid:/invalid"}, - } - - assert.NotNil(t, RunServer(server, invalidConfig)) -}