1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-22 09:12:59 +00:00

Merge pull request #682 from kiyonlin/improve-test

Improve test
This commit is contained in:
Fenny 2020-07-29 16:05:49 +02:00 committed by GitHub
commit 8950e94a4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 193 additions and 18 deletions

View File

@ -0,0 +1 @@
<h1>{{.Title}</h1>

View File

@ -5,12 +5,14 @@
package fiber package fiber
import ( import (
"bytes"
"crypto/tls" "crypto/tls"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"net" "net"
"net/http"
"net/http/httptest" "net/http/httptest"
"reflect" "reflect"
"regexp" "regexp"
@ -813,6 +815,23 @@ func Test_Test_Timeout(t *testing.T) {
utils.AssertEqual(t, true, err != nil, "app.Test(req)") utils.AssertEqual(t, true, err != nil, "app.Test(req)")
} }
type errorReader int
func (errorReader) Read([]byte) (int, error) {
return 0, errors.New("errorReader")
}
func Test_Test_DumpError(t *testing.T) {
app := New()
app.Settings.DisableStartupMessage = true
app.Get("/", func(_ *Ctx) {})
resp, err := app.Test(httptest.NewRequest("GET", "/", errorReader(0)))
utils.AssertEqual(t, true, resp == nil)
utils.AssertEqual(t, "errorReader", err.Error())
}
func Test_App_Handler(t *testing.T) { func Test_App_Handler(t *testing.T) {
h := New().Handler() h := New().Handler()
utils.AssertEqual(t, "fasthttp.RequestHandler", reflect.TypeOf(h).String()) utils.AssertEqual(t, "fasthttp.RequestHandler", reflect.TypeOf(h).String())
@ -835,3 +854,116 @@ func Test_App_Init_Error_View(t *testing.T) {
}() }()
_ = app.Settings.Views.Render(nil, "", nil) _ = app.Settings.Views.Render(nil, "", nil)
} }
func Test_App_Stack(t *testing.T) {
app := New()
app.Use("/path0", func(_ *Ctx) {})
app.Get("/path1", func(_ *Ctx) {})
app.Get("/path2", func(_ *Ctx) {})
app.Post("/path3", func(_ *Ctx) {})
stack := app.Stack()
utils.AssertEqual(t, 9, len(stack))
utils.AssertEqual(t, 3, len(stack[methodInt(MethodGet)]))
utils.AssertEqual(t, 3, len(stack[methodInt(MethodHead)]))
utils.AssertEqual(t, 2, len(stack[methodInt(MethodPost)]))
utils.AssertEqual(t, 1, len(stack[methodInt(MethodPut)]))
utils.AssertEqual(t, 1, len(stack[methodInt(MethodPatch)]))
utils.AssertEqual(t, 1, len(stack[methodInt(MethodDelete)]))
utils.AssertEqual(t, 1, len(stack[methodInt(MethodConnect)]))
utils.AssertEqual(t, 1, len(stack[methodInt(MethodOptions)]))
utils.AssertEqual(t, 1, len(stack[methodInt(MethodTrace)]))
}
// go test -run Test_App_ReadTimeout
//func Test_App_ReadTimeout(t *testing.T) {
// app := New(&Settings{
// ReadTimeout: time.Nanosecond,
// IdleTimeout: time.Minute,
// DisableStartupMessage: true,
// DisableKeepalive: true,
// })
//
// app.Get("/read-timeout", func(c *Ctx) {
// c.SendString("I should not be sent")
// })
//
// go func() {
// time.Sleep(500 * time.Millisecond)
//
// conn, err := net.Dial("tcp4", "127.0.0.1:4004")
// utils.AssertEqual(t, nil, err)
// defer conn.Close()
//
// _, err = conn.Write([]byte("HEAD /read-timeout HTTP/1.1\r\n"))
// utils.AssertEqual(t, nil, err)
//
// buf := make([]byte, 1024)
// var n int
// n, err = conn.Read(buf)
//
// utils.AssertEqual(t, nil, err)
// utils.AssertEqual(t, true, bytes.Contains(buf[:n], []byte("408 Request Timeout")))
//
// utils.AssertEqual(t, nil, app.Shutdown())
// }()
//
// utils.AssertEqual(t, nil, app.Listen(4004))
//}
// go test -run Test_App_BadRequest
func Test_App_BadRequest(t *testing.T) {
app := New(&Settings{
DisableStartupMessage: true,
})
app.Get("/bad-request", func(c *Ctx) {
c.SendString("I should not be sent")
})
go func() {
time.Sleep(500 * time.Millisecond)
conn, err := net.Dial("tcp4", "127.0.0.1:4005")
utils.AssertEqual(t, nil, err)
defer conn.Close()
_, err = conn.Write([]byte("BadRequest\r\n"))
utils.AssertEqual(t, nil, err)
buf := make([]byte, 1024)
var n int
n, err = conn.Read(buf)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, bytes.Contains(buf[:n], []byte("400 Bad Request")))
utils.AssertEqual(t, nil, app.Shutdown())
}()
utils.AssertEqual(t, nil, app.Listen(4005))
}
// go test -run Test_App_SmallReadBuffer
func Test_App_SmallReadBuffer(t *testing.T) {
app := New(&Settings{
ReadBufferSize: 1,
DisableStartupMessage: true,
})
app.Get("/small-read-buffer", func(c *Ctx) {
c.SendString("I should not be sent")
})
go func() {
time.Sleep(500 * time.Millisecond)
resp, err := http.Get("http://127.0.0.1:4006/small-read-buffer")
if resp != nil {
utils.AssertEqual(t, 431, resp.StatusCode)
}
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, nil, app.Shutdown())
}()
utils.AssertEqual(t, nil, app.Listen(4006))
}

11
ctx.go
View File

@ -15,7 +15,6 @@ import (
"log" "log"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -791,15 +790,7 @@ func (ctx *Ctx) Render(name string, bind interface{}, layouts ...string) (err er
} else { } else {
// Render raw template using 'name' as filepath if no engine is set // Render raw template using 'name' as filepath if no engine is set
var tmpl *template.Template var tmpl *template.Template
// Read file if _, err = readContent(buf, name); err != nil {
f, err := os.Open(filepath.Clean(name))
if err != nil {
return err
}
if _, err = buf.ReadFrom(f); err != nil {
return err
}
if err = f.Close(); err != nil {
return err return err
} }
// Parse template // Parse template

View File

@ -10,6 +10,7 @@ package fiber
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -1259,13 +1260,20 @@ func Test_Ctx_SendFile(t *testing.T) {
utils.AssertEqual(t, StatusNotModified, ctx.Fasthttp.Response.StatusCode()) utils.AssertEqual(t, StatusNotModified, ctx.Fasthttp.Response.StatusCode())
utils.AssertEqual(t, []byte(nil), ctx.Fasthttp.Response.Body()) utils.AssertEqual(t, []byte(nil), ctx.Fasthttp.Response.Body())
app.ReleaseCtx(ctx) app.ReleaseCtx(ctx)
}
// test 404 // go test -race -run Test_Ctx_SendFile_404
// ctx = app.AcquireCtx(&fasthttp.RequestCtx{}) func Test_Ctx_SendFile_404(t *testing.T) {
// err = ctx.SendFile("./john_doe.go") t.Parallel()
// // check expectation app := New()
// utils.AssertEqual(t, StatusNotFound, ctx.Fasthttp.Response.StatusCode()) app.Get("/", func(ctx *Ctx) {
// app.ReleaseCtx(ctx) err := ctx.SendFile("./john_dow.go/")
utils.AssertEqual(t, false, err == nil)
})
resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, StatusNotFound, resp.StatusCode)
} }
// go test -race -run Test_Ctx_SendFile_Immutable // go test -race -run Test_Ctx_SendFile_Immutable
@ -1493,7 +1501,10 @@ func Test_Ctx_Render(t *testing.T) {
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "<h1>Hello, World!</h1>", string(ctx.Fasthttp.Response.Body())) utils.AssertEqual(t, "<h1>Hello, World!</h1>", string(ctx.Fasthttp.Response.Body()))
err = ctx.Render("./.github/TEST_DATA/invalid.html", nil) err = ctx.Render("./.github/TEST_DATA/template-non-exists.html", nil)
utils.AssertEqual(t, false, err == nil)
err = ctx.Render("./.github/TEST_DATA/template-invalid.html", nil)
utils.AssertEqual(t, false, err == nil) utils.AssertEqual(t, false, err == nil)
} }
@ -1526,6 +1537,7 @@ func Test_Ctx_Render_Engine(t *testing.T) {
utils.AssertEqual(t, "<h1>Hello, World!</h1>", string(ctx.Fasthttp.Response.Body())) utils.AssertEqual(t, "<h1>Hello, World!</h1>", string(ctx.Fasthttp.Response.Body()))
} }
// go test -v -run=^$ -bench=Benchmark_Ctx_Render_Engine -benchmem -count=4
func Benchmark_Ctx_Render_Engine(b *testing.B) { func Benchmark_Ctx_Render_Engine(b *testing.B) {
engine := &testTemplateEngine{} engine := &testTemplateEngine{}
err := engine.Load() err := engine.Load()
@ -1545,19 +1557,43 @@ func Benchmark_Ctx_Render_Engine(b *testing.B) {
utils.AssertEqual(b, "<h1>Hello, World!</h1>", string(ctx.Fasthttp.Response.Body())) utils.AssertEqual(b, "<h1>Hello, World!</h1>", string(ctx.Fasthttp.Response.Body()))
} }
type errorTemplateEngine struct{}
func (t errorTemplateEngine) Render(w io.Writer, name string, bind interface{}, layout ...string) error {
return errors.New("errorTemplateEngine")
}
func (t errorTemplateEngine) Load() error { return nil }
// go test -run Test_Ctx_Render_Engine_Error
func Test_Ctx_Render_Engine_Error(t *testing.T) {
app := New()
app.Settings.Views = errorTemplateEngine{}
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(ctx)
err := ctx.Render("index.tmpl", nil)
utils.AssertEqual(t, false, err == nil)
}
// go test -run Test_Ctx_Render_Go_Template // go test -run Test_Ctx_Render_Go_Template
func Test_Ctx_Render_Go_Template(t *testing.T) { func Test_Ctx_Render_Go_Template(t *testing.T) {
t.Parallel() t.Parallel()
file, err := ioutil.TempFile(os.TempDir(), "fiber") file, err := ioutil.TempFile(os.TempDir(), "fiber")
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
defer os.Remove(file.Name()) defer os.Remove(file.Name())
_, err = file.Write([]byte("template")) _, err = file.Write([]byte("template"))
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
err = file.Close() err = file.Close()
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
app := New() app := New()
ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(ctx) defer app.ReleaseCtx(ctx)
err = ctx.Render(file.Name(), nil) err = ctx.Render(file.Name(), nil)
utils.AssertEqual(t, nil, err) utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "template", string(ctx.Fasthttp.Response.Body())) utils.AssertEqual(t, "template", string(ctx.Fasthttp.Response.Body()))

View File

@ -8,7 +8,10 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"hash/crc32" "hash/crc32"
"io"
"net" "net"
"os"
"path/filepath"
"strings" "strings"
"time" "time"
@ -17,6 +20,19 @@ import (
fasthttp "github.com/valyala/fasthttp" fasthttp "github.com/valyala/fasthttp"
) )
// readContent opens a named file and read content from it
func readContent(rf io.ReaderFrom, name string) (n int64, err error) {
// Read file
f, err := os.Open(filepath.Clean(name))
if err != nil {
return 0, err
}
defer func() {
err = f.Close()
}()
return rf.ReadFrom(f)
}
// quoteString escape special characters in a given string // quoteString escape special characters in a given string
func quoteString(raw string) string { func quoteString(raw string) string {
bb := bytebufferpool.Get() bb := bytebufferpool.Get()
@ -248,7 +264,6 @@ func (a testAddr) String() string {
} }
type testConn struct { type testConn struct {
net.Conn
r bytes.Buffer r bytes.Buffer
w bytes.Buffer w bytes.Buffer
} }