1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-22 10:13:11 +00:00

👷 improve test coverage

This commit is contained in:
kiyon 2020-07-16 16:42:36 +08:00 committed by ReneWerner87
parent 9ec400b5d5
commit 09c77765c3
6 changed files with 97 additions and 8 deletions

View File

@ -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), "<!DOCTYPE html>"), "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)
}

View File

@ -1416,6 +1416,9 @@ func Test_Ctx_Render(t *testing.T) {
})
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "<h1>Hello, World!</h1>", string(ctx.Fasthttp.Response.Body()))
err = ctx.Render("./.github/TEST_DATA/invalid.html", nil)
utils.AssertEqual(t, false, err == nil)
}
type testTemplateEngine struct {

View File

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

13
prefork_dummy.go Normal file
View File

@ -0,0 +1,13 @@
// +build !windows
package fiber
import (
"os/exec"
)
var dummyChildCmd = "go"
func dummyCmd() *exec.Cmd {
return exec.Command(dummyChildCmd, "version")
}

11
prefork_dummy_windows.go Normal file
View File

@ -0,0 +1,11 @@
package fiber
import (
"os/exec"
)
var dummyChildCmd = "go"
func dummyCmd() *exec.Cmd {
return exec.Command("cmd", "/C", dummyChildCmd, "version")
}

View File

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