🧪 Increase test coverage: 96%

pull/617/head
Fenny 2020-07-15 17:43:30 +02:00
parent ad2052cf8f
commit da85c85c14
6 changed files with 54 additions and 13 deletions

5
app.go
View File

@ -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...)
}

View File

@ -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)

View File

@ -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...)

View File

@ -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())
}

View File

@ -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 == "" {

View File

@ -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)