🍀 Improve test coverage

pull/960/head
kiyon 2020-10-24 16:24:48 +08:00
parent b1d19f4a21
commit d0e828dbf5
3 changed files with 67 additions and 2 deletions

View File

@ -565,7 +565,6 @@ func Test_App_Static_Direct(t *testing.T) {
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
utils.AssertEqual(t, "", resp.Header.Get(HeaderCacheControl), "CacheControl Control")
body, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, strings.Contains(string(body), "gofiber.io/support"))
@ -877,6 +876,15 @@ func Test_App_Listen(t *testing.T) {
utils.AssertEqual(t, nil, app.Listen(":4003"))
}
// go test -run Test_App_Listen_Prefork
func Test_App_Listen_Prefork(t *testing.T) {
testPreforkMaster = true
app := New(Config{DisableStartupMessage: true, Prefork: true})
utils.AssertEqual(t, nil, app.Listen(":99999"))
}
// go test -run Test_App_Listener
func Test_App_Listener(t *testing.T) {
app := New()
@ -890,6 +898,16 @@ func Test_App_Listener(t *testing.T) {
utils.AssertEqual(t, nil, app.Listener(ln))
}
// go test -run Test_App_Listener_Prefork
func Test_App_Listener_Prefork(t *testing.T) {
testPreforkMaster = true
app := New(Config{DisableStartupMessage: true, Prefork: true})
ln := fasthttputil.NewInmemoryListener()
utils.AssertEqual(t, nil, app.Listener(ln))
}
// go test -v -run=^$ -bench=Benchmark_AcquireCtx -benchmem -count=4
func Benchmark_AcquireCtx(b *testing.B) {
app := New()
@ -1103,7 +1121,7 @@ func Test_App_SmallReadBuffer(t *testing.T) {
func Test_App_Master_Process_Show_Startup_Message(t *testing.T) {
New(Config{Prefork: true}).
startupMessage(":3000", true, "")
startupMessage(":3000", true, strings.Repeat(",11111,22222,33333,44444,55555,60000", 10))
}
func Test_App_Server(t *testing.T) {
@ -1111,3 +1129,15 @@ func Test_App_Server(t *testing.T) {
utils.AssertEqual(t, false, app.Server() == nil)
}
func Test_App_Error_In_Fasthttp_Server(t *testing.T) {
app := New()
app.config.ErrorHandler = func(ctx *Ctx, err error) error {
return errors.New("fake error")
}
app.server.GetOnly = true
resp, err := app.Test(httptest.NewRequest(MethodPost, "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 500, resp.StatusCode)
}

View File

@ -17,6 +17,7 @@ import (
"mime/multipart"
"net/http/httptest"
"os"
"reflect"
"strconv"
"strings"
"sync"
@ -257,6 +258,8 @@ func Test_Ctx_BaseURL(t *testing.T) {
defer app.ReleaseCtx(c)
c.Request().SetRequestURI("http://google.com/test")
utils.AssertEqual(t, "http://google.com", c.BaseURL())
// Check cache
utils.AssertEqual(t, "http://google.com", c.BaseURL())
}
// go test -v -run=^$ -bench=Benchmark_Ctx_BaseURL -benchmem
@ -504,6 +507,9 @@ func Test_Ctx_Format(t *testing.T) {
c.Format("Hello, World!")
utils.AssertEqual(t, `<string>Hello, World!</string>`, string(c.Response().Body()))
err := c.Format(complex(1, 1))
utils.AssertEqual(t, true, err != nil)
c.Request().Header.Set(HeaderAccept, MIMETextPlain)
c.Format(Map{})
utils.AssertEqual(t, "map[]", string(c.Response().Body()))
@ -1256,6 +1262,9 @@ func Test_Ctx_Download(t *testing.T) {
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, expect, c.Response().Body())
utils.AssertEqual(t, `attachment; filename="Awesome+File%21"`, string(c.Response().Header.Peek(HeaderContentDisposition)))
c.Download("ctx.go")
utils.AssertEqual(t, `attachment; filename="ctx.go"`, string(c.Response().Header.Peek(HeaderContentDisposition)))
}
// go test -race -run Test_Ctx_SendFile
@ -1957,6 +1966,14 @@ func Test_Ctx_QueryParser(t *testing.T) {
}
func Test_Ctx_EqualFieldType(t *testing.T) {
var out int
utils.AssertEqual(t, false, equalFieldType(&out, reflect.Int, "key"))
var dummy struct{ f string }
utils.AssertEqual(t, false, equalFieldType(&dummy, reflect.String, "key"))
}
// go test -v -run=^$ -bench=Benchmark_Ctx_QueryParser -benchmem -count=4
func Benchmark_Ctx_QueryParser(b *testing.B) {
app := New()

View File

@ -8,6 +8,7 @@ package fiber
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http/httptest"
@ -261,6 +262,23 @@ func Test_Router_Handler_SetETag(t *testing.T) {
utils.AssertEqual(t, `"13-1831710635"`, string(c.Response.Header.Peek(HeaderETag)))
}
func Test_Router_Handler_Catch_Error(t *testing.T) {
app := New()
app.config.ErrorHandler = func(ctx *Ctx, err error) error {
return errors.New("fake error")
}
app.Get("/", func(c *Ctx) error {
return ErrForbidden
})
c := &fasthttp.RequestCtx{}
app.handler(c)
utils.AssertEqual(t, StatusInternalServerError, c.Response.Header.StatusCode())
}
//////////////////////////////////////////////
///////////////// BENCHMARKS /////////////////
//////////////////////////////////////////////