From da85c85c14ab3f98b29b46e65b4cc54c4642c711 Mon Sep 17 00:00:00 2001 From: Fenny <25108519+Fenny@users.noreply.github.com> Date: Wed, 15 Jul 2020 17:43:30 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Increase=20test=20coverage:=2096?= =?UTF-8?q?%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.go | 5 ++--- app_test.go | 26 +++++++++++++++++++++++--- group.go | 4 ++-- prefork_test.go | 15 +++++++++++++-- router.go | 6 +++--- router_test.go | 11 +++++++++++ 6 files changed, 54 insertions(+), 13 deletions(-) diff --git a/app.go b/app.go index 61033ed5..4dc7d052 100644 --- a/app.go +++ b/app.go @@ -13,7 +13,6 @@ import ( "bufio" "crypto/tls" "fmt" - "log" "net" "net/http" "net/http/httputil" @@ -307,7 +306,7 @@ func (app *App) Use(args ...interface{}) *Route { case Handler: handlers = append(handlers, arg) default: - log.Fatalf("Use: Invalid Handler %v", reflect.TypeOf(arg)) + panic(fmt.Sprintf("use: invalid handler %v\n", reflect.TypeOf(arg))) } } return app.register(methodUse, prefix, handlers...) @@ -492,7 +491,7 @@ func (app *App) Listen(address interface{}, tlsconfig ...*tls.Config) error { // Start prefork if app.Settings.Prefork { if app.Settings.Network == "tcp6" || isIPv6(addr) { - log.Fatal("prefork does not support tcp6 networking") + return fmt.Errorf("listen: tcp6 is not supported when prefork is enabled") } return app.prefork(addr, tlsconfig...) } diff --git a/app_test.go b/app_test.go index c7580751..2d4dcfc2 100644 --- a/app_test.go +++ b/app_test.go @@ -225,6 +225,28 @@ func Test_App_Use_Params(t *testing.T) { resp, err = app.Test(httptest.NewRequest("GET", "/foo", nil)) utils.AssertEqual(t, nil, err, "app.Test(req)") utils.AssertEqual(t, 200, resp.StatusCode, "Status code") + + defer func() { + if err := recover(); err != nil { + utils.AssertEqual(t, "use: invalid handler func()\n", fmt.Sprintf("%v", err)) + } + }() + + app.Use("/:param/*", func() { + // this should panic + }) +} + +func Test_App_Add_Method_Test(t *testing.T) { + app := New() + defer func() { + if err := recover(); err != nil { + utils.AssertEqual(t, "add: invalid http method JOHN\n", fmt.Sprintf("%v", err)) + } + }() + app.Add("JOHN", "/doe", func(c *Ctx) { + + }) } func Test_App_Use_Params_Group(t *testing.T) { @@ -659,9 +681,7 @@ func Test_App_Next_Method(t *testing.T) { // go test -run Test_App_Listen func Test_App_Listen(t *testing.T) { - app := New(&Settings{ - DisableStartupMessage: true, - }) + app := New() utils.AssertEqual(t, false, app.Listen(1.23) == nil) diff --git a/group.go b/group.go index bd853731..ab9b78e3 100644 --- a/group.go +++ b/group.go @@ -5,7 +5,7 @@ package fiber import ( - "log" + "fmt" "reflect" ) @@ -32,7 +32,7 @@ func (grp *Group) Use(args ...interface{}) *Route { case Handler: handlers = append(handlers, arg) default: - log.Fatalf("Use: Invalid Handler %v", reflect.TypeOf(arg)) + panic(fmt.Sprintf("use: invalid handler %v\n", reflect.TypeOf(arg))) } } return grp.app.register(methodUse, getGroupPath(grp.prefix, path), handlers...) diff --git a/prefork_test.go b/prefork_test.go index 2e850dfa..a904482b 100644 --- a/prefork_test.go +++ b/prefork_test.go @@ -13,7 +13,6 @@ func Test_App_Prefork_Child_Process(t *testing.T) { defer os.Setenv(envPreforkChildKey, "") app := New() - app.Settings.DisableStartupMessage = true app.init() err := app.prefork("invalid") @@ -31,7 +30,6 @@ func Test_App_Prefork_Main_Process(t *testing.T) { testPreforkMaster = true app := New() - app.Settings.DisableStartupMessage = true app.init() go func() { @@ -46,3 +44,16 @@ func Test_App_Prefork_Main_Process(t *testing.T) { err := app.prefork("127.0.0.1:") utils.AssertEqual(t, false, err == nil) } + +func Test_App_Prefork_TCP6_Addr(t *testing.T) { + app := New() + app.Settings.Prefork = true + app.Settings.DisableStartupMessage = true + + app.init() + utils.AssertEqual(t, "listen: tcp6 is not supported when prefork is enabled", app.Listen("[::1]:3000").Error()) + + app.Settings.Network = "tcp6" + app.init() + utils.AssertEqual(t, "listen: tcp6 is not supported when prefork is enabled", app.Listen(":3000").Error()) +} diff --git a/router.go b/router.go index 09aad11e..ba436c0f 100644 --- a/router.go +++ b/router.go @@ -5,7 +5,7 @@ package fiber import ( - "log" + "fmt" "strings" "time" @@ -147,11 +147,11 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) *Route { method = utils.ToUpper(method) // Check if the HTTP method is valid unless it's USE if methodInt(method) == -1 { - log.Fatalf("Add: Invalid HTTP method %s", method) + panic(fmt.Sprintf("add: invalid http method %s\n", method)) } // A route requires atleast one ctx handler if len(handlers) == 0 { - log.Fatalf("Missing func(c *fiber.Ctx) handler in route: %s", pathRaw) + panic(fmt.Sprintf("missing handler in route: %s\n", pathRaw)) } // Cannot have an empty path if pathRaw == "" { diff --git a/router_test.go b/router_test.go index bb0289e0..71a2e33c 100644 --- a/router_test.go +++ b/router_test.go @@ -8,6 +8,7 @@ package fiber import ( "encoding/json" + "fmt" "io/ioutil" "net/http/httptest" "testing" @@ -204,6 +205,16 @@ func Test_Route_Match_Middleware_Root(t *testing.T) { utils.AssertEqual(t, "middleware", getString(body)) } +func Test_Router_Register_Missing_Handler(t *testing.T) { + app := New() + defer func() { + if err := recover(); err != nil { + utils.AssertEqual(t, "missing handler in route: /doe\n", fmt.Sprintf("%v", err)) + } + }() + app.register("USE", "/doe") +} + func Test_Ensure_Router_Interface_Implementation(t *testing.T) { var app interface{} = (*App)(nil) _, ok := app.(Router)