mirror of https://github.com/gofiber/fiber.git
Hot fix fasthttp bug
parent
961fda2b8d
commit
64d74e840a
84
app_test.go
84
app_test.go
|
@ -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) {
|
func Test_App_Nested_Params(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
|
||||||
|
@ -204,8 +241,8 @@ func Test_App_Shutdown(t *testing.T) {
|
||||||
_ = app.Shutdown()
|
_ = app.Shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
// go test -run Test_App_Static
|
// go test -run Test_App_Static_Index_Default
|
||||||
func Test_App_Static_Index(t *testing.T) {
|
func Test_App_Static_Index_Default(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
|
||||||
app.Static("/prefix", "./.github/workflows")
|
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!"))
|
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) {
|
func Test_App_Static_Group(t *testing.T) {
|
||||||
app := New()
|
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, false, resp.Header.Get("Content-Length") == "")
|
||||||
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
|
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) {
|
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, 200, resp.StatusCode, "Status code")
|
||||||
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
utils.AssertEqual(t, false, resp.Header.Get("Content-Length") == "")
|
||||||
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
|
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) {
|
func Test_App_Static_Prefix(t *testing.T) {
|
||||||
|
|
3
go.mod
3
go.mod
|
@ -3,7 +3,8 @@ module github.com/gofiber/fiber
|
||||||
go 1.11
|
go 1.11
|
||||||
|
|
||||||
require (
|
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/gorilla/schema v1.1.0
|
||||||
github.com/valyala/bytebufferpool v1.0.0
|
github.com/valyala/bytebufferpool v1.0.0
|
||||||
github.com/valyala/fasthttp v1.14.0
|
github.com/valyala/fasthttp v1.14.0
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -1,7 +1,7 @@
|
||||||
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
|
github.com/andybalholm/brotli v1.0.0 h1:7UCwP93aiSfvWpapti8g88vVVGp2qqtGyePsSuDafo4=
|
||||||
github.com/andybalholm/brotli v1.0.0/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
|
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.4 h1:gcOb+WZDQ319bELqAp5ePDYwxostDDzWh4pY7S0KiMI=
|
||||||
github.com/gofiber/utils v0.0.3/go.mod h1:pacRFtghAE3UoknMOUiXh2Io/nLWSUHtQCi/3QASsOc=
|
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 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
|
||||||
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
|
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
|
||||||
github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss11wc=
|
github.com/klauspost/compress v1.10.4 h1:jFzIFaf586tquEB5EhzQG0HwGNSlgAJpG53G6Ss11wc=
|
||||||
|
|
23
router.go
23
router.go
|
@ -96,6 +96,19 @@ func (app *App) next(ctx *Ctx) bool {
|
||||||
func (app *App) handler(rctx *fasthttp.RequestCtx) {
|
func (app *App) handler(rctx *fasthttp.RequestCtx) {
|
||||||
// Acquire Ctx with fasthttp request from pool
|
// Acquire Ctx with fasthttp request from pool
|
||||||
ctx := app.AcquireCtx(rctx)
|
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
|
// Prettify path
|
||||||
ctx.prettifyPath()
|
ctx.prettifyPath()
|
||||||
// Find match in stack
|
// Find match in stack
|
||||||
|
@ -233,7 +246,8 @@ func (app *App) registerStatic(prefix, root string, config ...Static) *Route {
|
||||||
path = path[prefixLen:]
|
path = path[prefixLen:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return append(path, '/')
|
path = append([]byte("/"), path...)
|
||||||
|
return path
|
||||||
},
|
},
|
||||||
PathNotFound: func(ctx *fasthttp.RequestCtx) {
|
PathNotFound: func(ctx *fasthttp.RequestCtx) {
|
||||||
ctx.Response.SetStatusCode(404)
|
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.SetStatusCode(200)
|
||||||
c.Fasthttp.Response.SetBodyString("")
|
c.Fasthttp.Response.SetBodyString("")
|
||||||
// Next middleware
|
// Next middleware
|
||||||
match := c.app.next(c)
|
c.Next()
|
||||||
// If no other route is executed return 404 Not Found
|
|
||||||
if !match {
|
|
||||||
c.Fasthttp.Response.SetStatusCode(404)
|
|
||||||
c.Fasthttp.Response.SetBodyString("Not Found")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
route := &Route{
|
route := &Route{
|
||||||
use: true,
|
use: true,
|
||||||
|
|
Loading…
Reference in New Issue