Hot fix fasthttp bug

pull/436/head
Fenny 2020-06-03 17:16:10 +02:00
parent 961fda2b8d
commit 64d74e840a
4 changed files with 102 additions and 12 deletions

View File

@ -28,6 +28,43 @@ func testStatus200(t *testing.T, app *App, url string, method string) {
// }
// func Test_App_ErrorHandler(t *testing.T) {
// app := New()
// app.Get("/", func(c *Ctx) {
// c.Next(errors.New("Hi, I'm an error!"))
// })
// resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
// utils.AssertEqual(t, nil, err, "app.Test(req)")
// utils.AssertEqual(t, 500, resp.StatusCode, "Status code")
// body, err := ioutil.ReadAll(resp.Body)
// utils.AssertEqual(t, nil, err)
// utils.AssertEqual(t, "Hi, I'm an error!", string(body))
// }
// func Test_App_ErrorHandler_Custom(t *testing.T) {
// app := New(&Settings{
// ErrorHandler: func(ctx *Ctx, err error) {
// ctx.Status(200).SendString("Hi, I'm an custom error!")
// },
// })
// app.Get("/", func(c *Ctx) {
// c.Next(errors.New("Hi, I'm an error!"))
// })
// resp, err := app.Test(httptest.NewRequest("GET", "/", nil))
// utils.AssertEqual(t, nil, err, "app.Test(req)")
// utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
// body, err := ioutil.ReadAll(resp.Body)
// utils.AssertEqual(t, nil, err)
// utils.AssertEqual(t, "Hi, I'm an custom error!", string(body))
// }
func Test_App_Nested_Params(t *testing.T) {
app := New()
@ -204,8 +241,8 @@ func Test_App_Shutdown(t *testing.T) {
_ = app.Shutdown()
}
// go test -run Test_App_Static
func Test_App_Static_Index(t *testing.T) {
// go test -run Test_App_Static_Index_Default
func Test_App_Static_Index_Default(t *testing.T) {
app := New()
app.Static("/prefix", "./.github/workflows")
@ -222,6 +259,33 @@ func Test_App_Static_Index(t *testing.T) {
utils.AssertEqual(t, true, strings.Contains(string(body), "Hello, World!"))
}
// go test -run Test_App_Static_Index
func Test_App_Static_Direct(t *testing.T) {
app := New()
app.Static("/", "./.github")
resp, err := app.Test(httptest.NewRequest("GET", "/index.html", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
utils.AssertEqual(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type"))
body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, strings.Contains(string(body), "Hello, World!"))
resp, err = app.Test(httptest.NewRequest("GET", "/FUNDING.yml", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
body, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, strings.Contains(string(body), "buymeacoffee"))
}
func Test_App_Static_Group(t *testing.T) {
app := New()
@ -264,6 +328,10 @@ func Test_App_Static_Wildcard(t *testing.T) {
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, strings.Contains(string(body), "buymeacoffee"))
}
func Test_App_Static_Prefix_Wildcard(t *testing.T) {
@ -277,6 +345,18 @@ func Test_App_Static_Prefix_Wildcard(t *testing.T) {
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
app.Static("/my/nameisjohn*", "./.github/FUNDING.yml")
resp, err = app.Test(httptest.NewRequest("GET", "/my/nameisjohn/no/its/not", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, true, strings.Contains(string(body), "buymeacoffee"))
}
func Test_App_Static_Prefix(t *testing.T) {

3
go.mod
View File

@ -3,7 +3,8 @@ module github.com/gofiber/fiber
go 1.11
require (
github.com/gofiber/utils v0.0.3
github.com/gofiber/utils v0.0.4
github.com/google/uuid v1.1.1 // indirect
github.com/gorilla/schema v1.1.0
github.com/valyala/bytebufferpool v1.0.0
github.com/valyala/fasthttp v1.14.0

4
go.sum
View File

@ -1,7 +1,7 @@
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/gofiber/utils v0.0.3 h1:nNQKfZbZAGmOHqTOYplJwwOvX1Mg/NsTjfFO4/wTGrU=
github.com/gofiber/utils v0.0.3/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
github.com/gofiber/utils v0.0.4 h1:gcOb+WZDQ319bELqAp5ePDYwxostDDzWh4pY7S0KiMI=
github.com/gofiber/utils v0.0.4/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss11wc=

View File

@ -96,6 +96,19 @@ func (app *App) next(ctx *Ctx) bool {
func (app *App) handler(rctx *fasthttp.RequestCtx) {
// Acquire Ctx with fasthttp request from pool
ctx := app.AcquireCtx(rctx)
// // Possible feature for v1.12
// // Add recover by default
// defer func() {
// if r := recover(); r != nil {
// err, ok := r.(error)
// if !ok {
// err = fmt.Errorf("%v", r)
// }
// app.Settings.ErrorHandler(ctx, err)
// app.ReleaseCtx(ctx)
// return
// }
// }()
// Prettify path
ctx.prettifyPath()
// Find match in stack
@ -233,7 +246,8 @@ func (app *App) registerStatic(prefix, root string, config ...Static) *Route {
path = path[prefixLen:]
}
}
return append(path, '/')
path = append([]byte("/"), path...)
return path
},
PathNotFound: func(ctx *fasthttp.RequestCtx) {
ctx.Response.SetStatusCode(404)
@ -262,12 +276,7 @@ func (app *App) registerStatic(prefix, root string, config ...Static) *Route {
c.Fasthttp.Response.SetStatusCode(200)
c.Fasthttp.Response.SetBodyString("")
// Next middleware
match := c.app.next(c)
// If no other route is executed return 404 Not Found
if !match {
c.Fasthttp.Response.SetStatusCode(404)
c.Fasthttp.Response.SetBodyString("Not Found")
}
c.Next()
}
route := &Route{
use: true,