diff --git a/app_test.go b/app_test.go index c7cd748e..c59d1fef 100644 --- a/app_test.go +++ b/app_test.go @@ -8,6 +8,7 @@ import ( "crypto/tls" "errors" "fmt" + "io" "io/ioutil" "net" "net/http/httptest" @@ -627,6 +628,15 @@ func Test_App_Mixed_Routes_WithSameLen(t *testing.T) { utils.AssertEqual(t, true, strings.HasPrefix(string(body), ""), "Response: "+string(body)) } +func Test_App_Group_Invalid(t *testing.T) { + defer func() { + if err := recover(); err != nil { + utils.AssertEqual(t, "use: invalid handler int\n", fmt.Sprintf("%v", err)) + } + }() + New().Group("/").Use(1) +} + func Test_App_Group(t *testing.T) { var dummyHandler = func(c *Ctx) {} @@ -725,6 +735,8 @@ func Test_App_Listen(t *testing.T) { utils.AssertEqual(t, false, app.Listen(1.23) == nil) + utils.AssertEqual(t, false, app.Listen(":1.23") == nil) + go func() { time.Sleep(1000 * time.Millisecond) utils.AssertEqual(t, nil, app.Shutdown()) @@ -808,3 +820,21 @@ func Test_App_Handler(t *testing.T) { h := New().Handler() utils.AssertEqual(t, "fasthttp.RequestHandler", reflect.TypeOf(h).String()) } + +type invalidView struct{} + +func (invalidView) Load() error { return errors.New("invalid view") } + +func (i invalidView) Render(io.Writer, string, interface{}, ...string) error { panic("implement me") } + +func Test_App_Init_Error_View(t *testing.T) { + app := New(&Settings{Views: invalidView{}}) + app.init() + + defer func() { + if err := recover(); err != nil { + utils.AssertEqual(t, "implement me", fmt.Sprintf("%v", err)) + } + }() + _ = app.Settings.Views.Render(nil, "", nil) +} diff --git a/ctx_test.go b/ctx_test.go index 1ce82c31..5b871816 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -1416,6 +1416,9 @@ func Test_Ctx_Render(t *testing.T) { }) utils.AssertEqual(t, nil, err) utils.AssertEqual(t, "

Hello, World!

", string(ctx.Fasthttp.Response.Body())) + + err = ctx.Render("./.github/TEST_DATA/invalid.html", nil) + utils.AssertEqual(t, false, err == nil) } type testTemplateEngine struct { diff --git a/prefork.go b/prefork.go index c5afa5b8..cf4bb1c9 100644 --- a/prefork.go +++ b/prefork.go @@ -21,7 +21,6 @@ const ( var ( testPreforkMaster = false - dummyChildCmd = "go" ) // IsChild determines if the current process is a result of Prefork @@ -87,13 +86,9 @@ func (app *App) prefork(addr string, tlsconfig ...*tls.Config) (err error) { cmd := exec.Command(os.Args[0], os.Args[1:]...) if testPreforkMaster { // When test prefork master, - // just start the child process - // a cmd on all os is best - if runtime.GOOS == "windows" { - cmd = exec.Command("cmd", "/C", dummyChildCmd, "version") - } else { - cmd = exec.Command(dummyChildCmd, "version") - } + // just start the child process with a dummy cmd, + // which will exit soon + cmd = dummyCmd() } cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr diff --git a/prefork_dummy.go b/prefork_dummy.go new file mode 100644 index 00000000..afaaac4e --- /dev/null +++ b/prefork_dummy.go @@ -0,0 +1,13 @@ +// +build !windows + +package fiber + +import ( + "os/exec" +) + +var dummyChildCmd = "go" + +func dummyCmd() *exec.Cmd { + return exec.Command(dummyChildCmd, "version") +} diff --git a/prefork_dummy_windows.go b/prefork_dummy_windows.go new file mode 100644 index 00000000..6a13bb60 --- /dev/null +++ b/prefork_dummy_windows.go @@ -0,0 +1,11 @@ +package fiber + +import ( + "os/exec" +) + +var dummyChildCmd = "go" + +func dummyCmd() *exec.Cmd { + return exec.Command("cmd", "/C", dummyChildCmd, "version") +} diff --git a/prefork_test.go b/prefork_test.go index a904482b..02abf4a3 100644 --- a/prefork_test.go +++ b/prefork_test.go @@ -1,6 +1,8 @@ package fiber import ( + "crypto/tls" + "io/ioutil" "os" "testing" "time" @@ -24,6 +26,20 @@ func Test_App_Prefork_Child_Process(t *testing.T) { }() utils.AssertEqual(t, nil, app.prefork("127.0.0.1:")) + + // Create tls certificate + cer, err := tls.LoadX509KeyPair("./.github/TEST_DATA/ssl.pem", "./.github/TEST_DATA/ssl.key") + if err != nil { + utils.AssertEqual(t, nil, err) + } + config := &tls.Config{Certificates: []tls.Certificate{cer}} + + go func() { + time.Sleep(1000 * time.Millisecond) + utils.AssertEqual(t, nil, app.Shutdown()) + }() + + utils.AssertEqual(t, nil, app.prefork("127.0.0.1:", config)) } func Test_App_Prefork_Main_Process(t *testing.T) { @@ -57,3 +73,24 @@ func Test_App_Prefork_TCP6_Addr(t *testing.T) { app.init() utils.AssertEqual(t, "listen: tcp6 is not supported when prefork is enabled", app.Listen(":3000").Error()) } + +func Test_App_Prefork_Child_Process_Never_Show_Startup_Message(t *testing.T) { + utils.AssertEqual(t, nil, os.Setenv(envPreforkChildKey, envPreforkChildVal)) + defer os.Setenv(envPreforkChildKey, "") + + rescueStdout := os.Stdout + defer func() { os.Stdout = rescueStdout }() + + r, w, err := os.Pipe() + utils.AssertEqual(t, nil, err) + + os.Stdout = w + + New().startupMessage(":3000", false, "") + + utils.AssertEqual(t, nil, w.Close()) + + out, err := ioutil.ReadAll(r) + utils.AssertEqual(t, nil, err) + utils.AssertEqual(t, 0, len(out)) +}