From 5a0167a460ef2385ecade84683bd0007a28b97c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= Date: Tue, 27 Feb 2024 12:46:15 +0100 Subject: [PATCH] fixed the fasthttp ctx race condition problem --- app.go | 6 +- app_test.go | 9 +- bind_test.go | 74 +++--- ctx_interface.go | 12 +- ctx_test.go | 408 ++++++++++++++--------------- go.mod | 2 - go.sum | 4 - middleware/adaptor/adaptor.go | 2 +- middleware/adaptor/adaptor_test.go | 2 +- middleware/csrf/csrf_test.go | 24 +- middleware/session/session_test.go | 32 +-- middleware/session/store_test.go | 10 +- redirect_test.go | 38 +-- router.go | 4 +- router_test.go | 5 +- 15 files changed, 311 insertions(+), 321 deletions(-) diff --git a/app.go b/app.go index f681ae2ce2..f51186fe7e 100644 --- a/app.go +++ b/app.go @@ -496,7 +496,7 @@ func New(config ...Config) *App { // Create Ctx pool app.pool = sync.Pool{ New: func() any { - return app.NewCtx(&fasthttp.RequestCtx{}) + return app.newCtx() }, } @@ -1071,9 +1071,7 @@ func (app *App) ErrorHandler(ctx Ctx, err error) error { // errors before calling the application's error handler method. func (app *App) serverErrorHandler(fctx *fasthttp.RequestCtx, err error) { // Acquire Ctx with fasthttp request from pool - c := app.AcquireCtx() - c.Reset(fctx) - + c := app.AcquireCtx(fctx) defer app.ReleaseCtx(c) var ( diff --git a/app_test.go b/app_test.go index 81d7650e44..46d05bd0d1 100644 --- a/app_test.go +++ b/app_test.go @@ -314,7 +314,7 @@ func Test_App_serverErrorHandler_Internal_Error(t *testing.T) { t.Parallel() app := New() msg := "test err" - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed app.serverErrorHandler(c.fasthttp, errors.New(msg)) require.Equal(t, string(c.fasthttp.Response.Body()), msg) @@ -324,7 +324,7 @@ func Test_App_serverErrorHandler_Internal_Error(t *testing.T) { func Test_App_serverErrorHandler_Network_Error(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed app.serverErrorHandler(c.fasthttp, &net.DNSError{ Err: "test error", @@ -1400,8 +1400,7 @@ func Test_App_Next_Method(t *testing.T) { func Benchmark_AcquireCtx(b *testing.B) { app := New() for n := 0; n < b.N; n++ { - c := app.AcquireCtx() - c.Reset(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) app.ReleaseCtx(c) } @@ -1765,7 +1764,7 @@ func Test_App_SetTLSHandler(t *testing.T) { app := New() app.SetTLSHandler(tlsHandler) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) require.Equal(t, "example.golang", c.ClientHelloInfo().ServerName) diff --git a/bind_test.go b/bind_test.go index d8b3efe65a..f20255bc53 100644 --- a/bind_test.go +++ b/bind_test.go @@ -23,7 +23,7 @@ const helloWorld = "hello world" func Test_Bind_Query(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Query struct { ID int @@ -98,7 +98,7 @@ func Test_Bind_Query_Map(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetBody([]byte(``)) c.Request().Header.SetContentType("") @@ -156,7 +156,7 @@ func Test_Bind_Query_WithSetParserDecoder(t *testing.T) { }) app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type NonRFCTimeInput struct { Date NonRFCTime `query:"date"` @@ -188,7 +188,7 @@ func Test_Bind_Query_WithSetParserDecoder(t *testing.T) { func Test_Bind_Query_Schema(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Query1 struct { Name string `query:"name,required"` @@ -289,7 +289,7 @@ func Test_Bind_Query_Schema(t *testing.T) { func Test_Bind_Header(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Header struct { ID int @@ -362,7 +362,7 @@ func Test_Bind_Header_Map(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetBody([]byte(``)) c.Request().Header.SetContentType("") @@ -410,7 +410,7 @@ func Test_Bind_Header_WithSetParserDecoder(t *testing.T) { }) app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type NonRFCTimeInput struct { Date NonRFCTime `req:"date"` @@ -445,7 +445,7 @@ func Test_Bind_Header_WithSetParserDecoder(t *testing.T) { func Test_Bind_Header_Schema(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Header1 struct { Name string `header:"Name,required"` @@ -530,7 +530,7 @@ func Test_Bind_Header_Schema(t *testing.T) { func Test_Bind_RespHeader(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Header struct { ID int @@ -603,7 +603,7 @@ func Test_Bind_RespHeader_Map(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetBody([]byte(``)) c.Request().Header.SetContentType("") @@ -632,7 +632,7 @@ func Benchmark_Bind_Query(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Query struct { ID int @@ -656,7 +656,7 @@ func Benchmark_Bind_Query_Map(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetBody([]byte(``)) c.Request().Header.SetContentType("") @@ -675,7 +675,7 @@ func Benchmark_Bind_Query_WithParseParam(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Person struct { Name string `query:"name"` @@ -705,7 +705,7 @@ func Benchmark_Bind_Query_Comma(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Query struct { ID int @@ -730,7 +730,7 @@ func Benchmark_Bind_Header(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type ReqHeader struct { ID int @@ -757,7 +757,7 @@ func Benchmark_Bind_Header(b *testing.B) { func Benchmark_Bind_Header_Map(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetBody([]byte(``)) c.Request().Header.SetContentType("") @@ -780,7 +780,7 @@ func Benchmark_Bind_RespHeader(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type ReqHeader struct { ID int @@ -807,7 +807,7 @@ func Benchmark_Bind_RespHeader(b *testing.B) { func Benchmark_Bind_RespHeader_Map(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetBody([]byte(``)) c.Request().Header.SetContentType("") @@ -829,7 +829,7 @@ func Benchmark_Bind_RespHeader_Map(b *testing.B) { func Test_Bind_Body(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Demo struct { Name string `json:"name" xml:"name" form:"name" query:"name"` @@ -926,7 +926,7 @@ func Test_Bind_Body_WithSetParserDecoder(t *testing.T) { }) app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Demo struct { Date CustomTime `form:"date"` @@ -958,7 +958,7 @@ func Benchmark_Bind_Body_JSON(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Demo struct { Name string `json:"name"` @@ -984,7 +984,7 @@ func Benchmark_Bind_Body_XML(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Demo struct { Name string `xml:"name"` @@ -1010,7 +1010,7 @@ func Benchmark_Bind_Body_Form(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Demo struct { Name string `form:"name"` @@ -1036,7 +1036,7 @@ func Benchmark_Bind_Body_MultipartForm(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Demo struct { Name string `form:"name"` @@ -1063,7 +1063,7 @@ func Benchmark_Bind_Body_Form_Map(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) body := []byte("name=john") c.Request().SetBody(body) @@ -1131,7 +1131,7 @@ func Benchmark_Bind_URI(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.route = &Route{ Params: []string{ @@ -1168,7 +1168,7 @@ func Benchmark_Bind_URI_Map(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.route = &Route{ Params: []string{ @@ -1200,7 +1200,7 @@ func Test_Bind_Cookie(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Cookie struct { ID int @@ -1273,7 +1273,7 @@ func Test_Bind_Cookie_Map(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetBody([]byte(``)) c.Request().Header.SetContentType("") @@ -1321,7 +1321,7 @@ func Test_Bind_Cookie_WithSetParserDecoder(t *testing.T) { }) app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type NonRFCTimeInput struct { Date NonRFCTime `cerez:"date"` @@ -1357,7 +1357,7 @@ func Test_Bind_Cookie_Schema(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Cookie1 struct { Name string `cookie:"Name,required"` @@ -1443,7 +1443,7 @@ func Benchmark_Bind_Cookie(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Cookie struct { ID int @@ -1472,7 +1472,7 @@ func Benchmark_Bind_Cookie_Map(b *testing.B) { var err error app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetBody([]byte(``)) c.Request().Header.SetContentType("") @@ -1509,7 +1509,7 @@ func (*customBinder) Parse(c Ctx, out any) error { // go test -run Test_Bind_CustomBinder func Test_Bind_CustomBinder(t *testing.T) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) // Register binder customBinder := &customBinder{} @@ -1533,7 +1533,7 @@ func Test_Bind_CustomBinder(t *testing.T) { // go test -run Test_Bind_Must func Test_Bind_Must(t *testing.T) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type RequiredQuery struct { Name string `query:"name,required"` @@ -1573,7 +1573,7 @@ type simpleQuery struct { // go test -run Test_Bind_StructValidator func Test_Bind_StructValidator(t *testing.T) { app := New(Config{StructValidator: &structValidator{}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) rq := new(simpleQuery) c.Request().URI().SetQueryString("name=efe") @@ -1588,7 +1588,7 @@ func Test_Bind_StructValidator(t *testing.T) { func Test_Bind_RepeatParserWithSameStruct(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) type Request struct { diff --git a/ctx_interface.go b/ctx_interface.go index 3eef6a6938..a608604a53 100644 --- a/ctx_interface.go +++ b/ctx_interface.go @@ -427,7 +427,7 @@ func NewDefaultCtx(app *App) *DefaultCtx { } } -func (app *App) NewCtx(fctx *fasthttp.RequestCtx) Ctx { +func (app *App) newCtx() Ctx { var c Ctx if app.newCtxFunc != nil { @@ -436,18 +436,18 @@ func (app *App) NewCtx(fctx *fasthttp.RequestCtx) Ctx { c = NewDefaultCtx(app) } - // Set request - c.setReq(fctx) - return c } // AcquireCtx retrieves a new Ctx from the pool. -func (app *App) AcquireCtx() Ctx { +func (app *App) AcquireCtx(fctx *fasthttp.RequestCtx) Ctx { ctx, ok := app.pool.Get().(Ctx) + if !ok { panic(errors.New("failed to type-assert to Ctx")) } + ctx.Reset(fctx) + return ctx } @@ -470,7 +470,7 @@ func (c *DefaultCtx) Reset(fctx *fasthttp.RequestCtx) { c.pathOriginal = c.app.getString(fctx.URI().PathOriginal()) // Attach *fasthttp.RequestCtx to ctx - c.fasthttp = fctx + c.setReq(fctx) // reset base uri c.baseURI = "" diff --git a/ctx_test.go b/ctx_test.go index 48c893efb4..bef906ea3c 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -40,7 +40,7 @@ const epsilon = 0.001 func Test_Ctx_Accepts(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderAccept, "text/html,application/xhtml+xml,application/xml;q=0.9") require.Equal(t, "", c.Accepts("")) @@ -70,7 +70,7 @@ func Test_Ctx_Accepts(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Accepts -benchmem -count=4 func Benchmark_Ctx_Accepts(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) acceptHeader := "text/html,application/xhtml+xml,application/xml;q=0.9" c.Request().Header.Set("Accept", acceptHeader) @@ -129,7 +129,7 @@ func Test_Ctx_CustomCtx(t *testing.T) { func Test_Ctx_Accepts_EmptyAccept(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.Equal(t, ".forwarded", c.Accepts(".forwarded")) } @@ -138,7 +138,7 @@ func Test_Ctx_Accepts_EmptyAccept(t *testing.T) { func Test_Ctx_Accepts_Wildcard(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderAccept, "*/*;q=0.9") require.Equal(t, "html", c.Accepts("html")) @@ -152,7 +152,7 @@ func Test_Ctx_Accepts_Wildcard(t *testing.T) { func Test_Ctx_AcceptsCharsets(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderAcceptCharset, "utf-8, iso-8859-1;q=0.5") require.Equal(t, "utf-8", c.AcceptsCharsets("utf-8")) @@ -161,7 +161,7 @@ func Test_Ctx_AcceptsCharsets(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_AcceptsCharsets -benchmem -count=4 func Benchmark_Ctx_AcceptsCharsets(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().Header.Set("Accept-Charset", "utf-8, iso-8859-1;q=0.5") var res string @@ -177,7 +177,7 @@ func Benchmark_Ctx_AcceptsCharsets(b *testing.B) { func Test_Ctx_AcceptsEncodings(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderAcceptEncoding, "deflate, gzip;q=1.0, *;q=0.5") require.Equal(t, "gzip", c.AcceptsEncodings("gzip")) @@ -187,7 +187,7 @@ func Test_Ctx_AcceptsEncodings(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_AcceptsEncodings -benchmem -count=4 func Benchmark_Ctx_AcceptsEncodings(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().Header.Set(HeaderAcceptEncoding, "deflate, gzip;q=1.0, *;q=0.5") var res string @@ -203,7 +203,7 @@ func Benchmark_Ctx_AcceptsEncodings(b *testing.B) { func Test_Ctx_AcceptsLanguages(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderAcceptLanguage, "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5") require.Equal(t, "fr", c.AcceptsLanguages("fr")) @@ -212,7 +212,7 @@ func Test_Ctx_AcceptsLanguages(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_AcceptsLanguages -benchmem -count=4 func Benchmark_Ctx_AcceptsLanguages(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().Header.Set(HeaderAcceptLanguage, "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5") var res string @@ -229,7 +229,7 @@ func Test_Ctx_App(t *testing.T) { t.Parallel() app := New() app.config.BodyLimit = 1000 - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.Equal(t, 1000, c.App().config.BodyLimit) } @@ -238,7 +238,7 @@ func Test_Ctx_App(t *testing.T) { func Test_Ctx_Append(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Append("X-Test", "Hello") c.Append("X-Test", "World") @@ -273,7 +273,7 @@ func Test_Ctx_Append(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Append -benchmem -count=4 func Benchmark_Ctx_Append(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed b.ReportAllocs() b.ResetTimer() @@ -289,7 +289,7 @@ func Benchmark_Ctx_Append(b *testing.B) { func Test_Ctx_Attachment(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) // empty c.Attachment() @@ -306,7 +306,7 @@ func Test_Ctx_Attachment(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Attachment -benchmem -count=4 func Benchmark_Ctx_Attachment(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed b.ReportAllocs() b.ResetTimer() @@ -321,7 +321,7 @@ func Benchmark_Ctx_Attachment(b *testing.B) { func Test_Ctx_BaseURL(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com/test") require.Equal(t, "http://google.com", c.BaseURL()) @@ -332,7 +332,7 @@ func Test_Ctx_BaseURL(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_BaseURL -benchmem func Benchmark_Ctx_BaseURL(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().SetHost("google.com:1337") c.Request().URI().SetPath("/haha/oke/lol") @@ -349,7 +349,7 @@ func Benchmark_Ctx_BaseURL(b *testing.B) { func Test_Ctx_Body(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().SetBody([]byte("john=doe")) require.Equal(t, []byte("john=doe"), c.Body()) @@ -360,7 +360,7 @@ func Benchmark_Ctx_Body(b *testing.B) { const input = "john=doe" app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().SetBody([]byte(input)) b.ReportAllocs() @@ -377,7 +377,7 @@ func Test_Ctx_Body_Immutable(t *testing.T) { t.Parallel() app := New() app.config.Immutable = true - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().SetBody([]byte("john=doe")) require.Equal(t, []byte("john=doe"), c.Body()) @@ -389,7 +389,7 @@ func Benchmark_Ctx_Body_Immutable(b *testing.B) { app := New() app.config.Immutable = true - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().SetBody([]byte(input)) b.ReportAllocs() @@ -442,7 +442,7 @@ func Test_Ctx_Body_With_Compression(t *testing.T) { t.Run(tCase.name, func(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().Header.Set("Content-Encoding", tCase.contentEncoding) if strings.Contains(tCase.contentEncoding, "gzip") { @@ -578,7 +578,7 @@ func Benchmark_Ctx_Body_With_Compression(b *testing.B) { b.Run(ct.contentEncoding, func(b *testing.B) { app := New() const input = "john=doe" - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set("Content-Encoding", ct.contentEncoding) compressedBody, err := ct.compressWriter([]byte(input)) @@ -635,7 +635,7 @@ func Test_Ctx_Body_With_Compression_Immutable(t *testing.T) { t.Parallel() app := New() app.config.Immutable = true - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().Header.Set("Content-Encoding", tCase.contentEncoding) if strings.Contains(tCase.contentEncoding, "gzip") { @@ -772,7 +772,7 @@ func Benchmark_Ctx_Body_With_Compression_Immutable(b *testing.B) { app := New() app.config.Immutable = true const input = "john=doe" - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set("Content-Encoding", ct.contentEncoding) compressedBody, err := ct.compressWriter([]byte(input)) @@ -792,7 +792,7 @@ func Benchmark_Ctx_Body_With_Compression_Immutable(b *testing.B) { func Test_Ctx_Context(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.Equal(t, "*fasthttp.RequestCtx", fmt.Sprintf("%T", c.Context())) } @@ -801,7 +801,7 @@ func Test_Ctx_Context(t *testing.T) { func Test_Ctx_UserContext(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) t.Run("Nil_Context", func(t *testing.T) { t.Parallel() @@ -821,7 +821,7 @@ func Test_Ctx_UserContext(t *testing.T) { func Test_Ctx_SetUserContext(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) testKey := struct{}{} testValue := "Test Value" @@ -872,7 +872,7 @@ func Test_Ctx_UserContext_Multiple_Requests(t *testing.T) { func Test_Ctx_Cookie(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) expire := time.Now().Add(24 * time.Hour) var dst []byte @@ -924,7 +924,7 @@ func Test_Ctx_Cookie(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Cookie -benchmem -count=4 func Benchmark_Ctx_Cookie(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed b.ReportAllocs() b.ResetTimer() @@ -941,7 +941,7 @@ func Benchmark_Ctx_Cookie(b *testing.B) { func Test_Ctx_Cookies(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set("Cookie", "john=doe") require.Equal(t, "doe", c.Cookies("john")) @@ -952,7 +952,7 @@ func Test_Ctx_Cookies(t *testing.T) { func Test_Ctx_Format(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) // set `accepted` to whatever media type was chosen by Format var accepted string @@ -1001,7 +1001,7 @@ func Test_Ctx_Format(t *testing.T) { func Benchmark_Ctx_Format(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderAccept, "application/json,text/plain; format=flowed; q=0.9") fail := func(_ Ctx) error { @@ -1068,7 +1068,7 @@ func Benchmark_Ctx_Format(b *testing.B) { func Test_Ctx_AutoFormat(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderAccept, MIMETextPlain) err := c.AutoFormat([]byte("Hello, World!")) @@ -1114,7 +1114,7 @@ func Test_Ctx_AutoFormat(t *testing.T) { func Test_Ctx_AutoFormat_Struct(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type Message struct { Recipients []string @@ -1147,7 +1147,7 @@ func Test_Ctx_AutoFormat_Struct(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_AutoFormat -benchmem -count=4 func Benchmark_Ctx_AutoFormat(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set("Accept", "text/plain") b.ReportAllocs() @@ -1164,7 +1164,7 @@ func Benchmark_Ctx_AutoFormat(b *testing.B) { // go test -v -run=^$ -bench=Benchmark_Ctx_AutoFormat_HTML -benchmem -count=4 func Benchmark_Ctx_AutoFormat_HTML(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set("Accept", "text/html") b.ReportAllocs() @@ -1181,7 +1181,7 @@ func Benchmark_Ctx_AutoFormat_HTML(b *testing.B) { // go test -v -run=^$ -bench=Benchmark_Ctx_AutoFormat_JSON -benchmem -count=4 func Benchmark_Ctx_AutoFormat_JSON(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set("Accept", "application/json") b.ReportAllocs() @@ -1198,7 +1198,7 @@ func Benchmark_Ctx_AutoFormat_JSON(b *testing.B) { // go test -v -run=^$ -bench=Benchmark_Ctx_AutoFormat_XML -benchmem -count=4 func Benchmark_Ctx_AutoFormat_XML(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set("Accept", "application/xml") b.ReportAllocs() @@ -1282,7 +1282,7 @@ func Test_Ctx_FormValue(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Fresh_StaleEtag -benchmem -count=4 func Benchmark_Ctx_Fresh_StaleEtag(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) for n := 0; n < b.N; n++ { c.Request().Header.Set(HeaderIfNoneMatch, "a, b, c, d") @@ -1299,7 +1299,7 @@ func Benchmark_Ctx_Fresh_StaleEtag(b *testing.B) { func Test_Ctx_Fresh(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.False(t, c.Fresh()) @@ -1344,7 +1344,7 @@ func Test_Ctx_Fresh(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Fresh_WithNoCache -benchmem -count=4 func Benchmark_Ctx_Fresh_WithNoCache(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderIfNoneMatch, "*") c.Request().Header.Set(HeaderCacheControl, "no-cache") @@ -1369,7 +1369,7 @@ func Test_Ctx_Parsers(t *testing.T) { withValues := func(t *testing.T, actionFn func(c Ctx, testStruct *TestStruct) error) { t.Helper() - c := app.AcquireCtx() + c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) testStruct := new(TestStruct) @@ -1453,7 +1453,7 @@ func Test_Ctx_Parsers(t *testing.T) { func Test_Ctx_Get(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderAcceptCharset, "utf-8, iso-8859-1;q=0.5") c.Request().Header.Set(HeaderReferer, "Monster") @@ -1466,7 +1466,7 @@ func Test_Ctx_Get(t *testing.T) { func Test_Ctx_Host(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com/test") require.Equal(t, "google.com", c.Host()) @@ -1478,7 +1478,7 @@ func Test_Ctx_Host_UntrustedProxy(t *testing.T) { // Don't trust any proxy { app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com/test") c.Request().Header.Set(HeaderXForwardedHost, "google1.com") require.Equal(t, "google.com", c.Host()) @@ -1487,7 +1487,7 @@ func Test_Ctx_Host_UntrustedProxy(t *testing.T) { // Trust to specific proxy list { app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"0.8.0.0", "0.8.0.1"}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com/test") c.Request().Header.Set(HeaderXForwardedHost, "google1.com") require.Equal(t, "google.com", c.Host()) @@ -1500,7 +1500,7 @@ func Test_Ctx_Host_TrustedProxy(t *testing.T) { t.Parallel() { app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"0.0.0.0", "0.8.0.1"}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com/test") c.Request().Header.Set(HeaderXForwardedHost, "google1.com") require.Equal(t, "google1.com", c.Host()) @@ -1513,7 +1513,7 @@ func Test_Ctx_Host_TrustedProxyRange(t *testing.T) { t.Parallel() app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"0.0.0.0/30"}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com/test") c.Request().Header.Set(HeaderXForwardedHost, "google1.com") require.Equal(t, "google1.com", c.Host()) @@ -1525,7 +1525,7 @@ func Test_Ctx_Host_UntrustedProxyRange(t *testing.T) { t.Parallel() app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"1.0.0.0/30"}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com/test") c.Request().Header.Set(HeaderXForwardedHost, "google1.com") require.Equal(t, "google.com", c.Host()) @@ -1535,7 +1535,7 @@ func Test_Ctx_Host_UntrustedProxyRange(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Host -benchmem -count=4 func Benchmark_Ctx_Host(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com/test") var host string b.ReportAllocs() @@ -1552,7 +1552,7 @@ func Test_Ctx_IsProxyTrusted(t *testing.T) { { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) require.True(t, c.IsProxyTrusted()) } @@ -1560,7 +1560,7 @@ func Test_Ctx_IsProxyTrusted(t *testing.T) { app := New(Config{ EnableTrustedProxyCheck: false, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.True(t, c.IsProxyTrusted()) } @@ -1568,7 +1568,7 @@ func Test_Ctx_IsProxyTrusted(t *testing.T) { app := New(Config{ EnableTrustedProxyCheck: true, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.False(t, c.IsProxyTrusted()) } { @@ -1577,7 +1577,7 @@ func Test_Ctx_IsProxyTrusted(t *testing.T) { TrustedProxies: []string{}, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.False(t, c.IsProxyTrusted()) } { @@ -1588,7 +1588,7 @@ func Test_Ctx_IsProxyTrusted(t *testing.T) { "127.0.0.1", }, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.False(t, c.IsProxyTrusted()) } { @@ -1599,7 +1599,7 @@ func Test_Ctx_IsProxyTrusted(t *testing.T) { "127.0.0.1/8", }, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.False(t, c.IsProxyTrusted()) } { @@ -1610,7 +1610,7 @@ func Test_Ctx_IsProxyTrusted(t *testing.T) { "0.0.0.0", }, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.True(t, c.IsProxyTrusted()) } { @@ -1621,7 +1621,7 @@ func Test_Ctx_IsProxyTrusted(t *testing.T) { "0.0.0.1/31", }, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.True(t, c.IsProxyTrusted()) } { @@ -1632,7 +1632,7 @@ func Test_Ctx_IsProxyTrusted(t *testing.T) { "0.0.0.1/31junk", }, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.False(t, c.IsProxyTrusted()) } } @@ -1641,7 +1641,7 @@ func Test_Ctx_IsProxyTrusted(t *testing.T) { func Test_Ctx_Hostname(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com/test") require.Equal(t, "google.com", c.Hostname()) @@ -1653,7 +1653,7 @@ func Test_Ctx_Hostname(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Hostname -benchmem -count=4 func Benchmark_Ctx_Hostname(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com:8080/test") var hostname string b.ReportAllocs() @@ -1664,7 +1664,7 @@ func Benchmark_Ctx_Hostname(b *testing.B) { // Trust to specific proxy list { app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"0.8.0.0", "0.8.0.1"}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com/test") c.Request().Header.Set(HeaderXForwardedHost, "google1.com") require.Equal(b, "google.com", hostname) @@ -1677,7 +1677,7 @@ func Test_Ctx_Hostname_TrustedProxy(t *testing.T) { t.Parallel() { app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"0.0.0.0", "0.8.0.1"}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com/test") c.Request().Header.Set(HeaderXForwardedHost, "google1.com") require.Equal(t, "google1.com", c.Hostname()) @@ -1690,7 +1690,7 @@ func Test_Ctx_Hostname_TrustedProxy_Multiple(t *testing.T) { t.Parallel() { app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"0.0.0.0", "0.8.0.1"}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com/test") c.Request().Header.Set(HeaderXForwardedHost, "google1.com, google2.com") require.Equal(t, "google1.com", c.Hostname()) @@ -1703,7 +1703,7 @@ func Test_Ctx_Hostname_TrustedProxyRange(t *testing.T) { t.Parallel() app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"0.0.0.0/30"}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com/test") c.Request().Header.Set(HeaderXForwardedHost, "google1.com") require.Equal(t, "google1.com", c.Hostname()) @@ -1715,7 +1715,7 @@ func Test_Ctx_Hostname_UntrustedProxyRange(t *testing.T) { t.Parallel() app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"1.0.0.0/30"}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://google.com/test") c.Request().Header.Set(HeaderXForwardedHost, "google1.com") require.Equal(t, "google.com", c.Hostname()) @@ -1726,7 +1726,7 @@ func Test_Ctx_Hostname_UntrustedProxyRange(t *testing.T) { func Test_Ctx_Port(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.Equal(t, "0", c.Port()) } @@ -1754,7 +1754,7 @@ func Test_Ctx_IP(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) // default behavior will return the remote IP from the stack require.Equal(t, "0.0.0.0", c.IP()) @@ -1773,7 +1773,7 @@ func Test_Ctx_IP_ProxyHeader(t *testing.T) { for _, proxyHeaderName := range proxyHeaderNames { app := New(Config{ProxyHeader: proxyHeaderName}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(proxyHeaderName, "0.0.0.1") require.Equal(t, "0.0.0.1", c.IP()) @@ -1805,7 +1805,7 @@ func Test_Ctx_IP_ProxyHeader_With_IP_Validation(t *testing.T) { for _, proxyHeaderName := range proxyHeaderNames { app := New(Config{EnableIPValidation: true, ProxyHeader: proxyHeaderName}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) // when proxy header & validation is enabled and the value is a valid IP, we return it c.Request().Header.Set(proxyHeaderName, "0.0.0.1") @@ -1833,7 +1833,7 @@ func Test_Ctx_IP_ProxyHeader_With_IP_Validation(t *testing.T) { func Test_Ctx_IP_UntrustedProxy(t *testing.T) { t.Parallel() app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"0.8.0.1"}, ProxyHeader: HeaderXForwardedFor}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXForwardedFor, "0.0.0.1") require.Equal(t, "0.0.0.0", c.IP()) } @@ -1842,7 +1842,7 @@ func Test_Ctx_IP_UntrustedProxy(t *testing.T) { func Test_Ctx_IP_TrustedProxy(t *testing.T) { t.Parallel() app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"0.0.0.0"}, ProxyHeader: HeaderXForwardedFor}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXForwardedFor, "0.0.0.1") require.Equal(t, "0.0.0.1", c.IP()) } @@ -1851,7 +1851,7 @@ func Test_Ctx_IP_TrustedProxy(t *testing.T) { func Test_Ctx_IPs(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) // normal happy path test case c.Request().Header.Set(HeaderXForwardedFor, "127.0.0.1, 127.0.0.2, 127.0.0.3") @@ -1887,7 +1887,7 @@ func Test_Ctx_IPs(t *testing.T) { func Test_Ctx_IPs_With_IP_Validation(t *testing.T) { t.Parallel() app := New(Config{EnableIPValidation: true}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) // normal happy path test case c.Request().Header.Set(HeaderXForwardedFor, "127.0.0.1, 127.0.0.2, 127.0.0.3") @@ -1923,7 +1923,7 @@ func Test_Ctx_IPs_With_IP_Validation(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_IPs -benchmem -count=4 func Benchmark_Ctx_IPs(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXForwardedFor, "127.0.0.1, invalid, 127.0.0.1") var res []string b.ReportAllocs() @@ -1936,7 +1936,7 @@ func Benchmark_Ctx_IPs(b *testing.B) { func Benchmark_Ctx_IPs_v6(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) c.Request().Header.Set(HeaderXForwardedFor, "f037:825e:eadb:1b7b:1667:6f0a:5356:f604, invalid, 2345:0425:2CA1::0567:5673:23b5") var res []string @@ -1950,7 +1950,7 @@ func Benchmark_Ctx_IPs_v6(b *testing.B) { func Benchmark_Ctx_IPs_With_IP_Validation(b *testing.B) { app := New(Config{EnableIPValidation: true}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXForwardedFor, "127.0.0.1, invalid, 127.0.0.1") var res []string b.ReportAllocs() @@ -1963,7 +1963,7 @@ func Benchmark_Ctx_IPs_With_IP_Validation(b *testing.B) { func Benchmark_Ctx_IPs_v6_With_IP_Validation(b *testing.B) { app := New(Config{EnableIPValidation: true}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) c.Request().Header.Set(HeaderXForwardedFor, "2345:0425:2CA1:0000:0000:0567:5673:23b5, invalid, 2345:0425:2CA1::0567:5673:23b5") var res []string @@ -1977,7 +1977,7 @@ func Benchmark_Ctx_IPs_v6_With_IP_Validation(b *testing.B) { func Benchmark_Ctx_IP_With_ProxyHeader(b *testing.B) { app := New(Config{ProxyHeader: HeaderXForwardedFor}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXForwardedFor, "127.0.0.1") var res string b.ReportAllocs() @@ -1990,7 +1990,7 @@ func Benchmark_Ctx_IP_With_ProxyHeader(b *testing.B) { func Benchmark_Ctx_IP_With_ProxyHeader_and_IP_Validation(b *testing.B) { app := New(Config{ProxyHeader: HeaderXForwardedFor, EnableIPValidation: true}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXForwardedFor, "127.0.0.1") var res string b.ReportAllocs() @@ -2003,7 +2003,7 @@ func Benchmark_Ctx_IP_With_ProxyHeader_and_IP_Validation(b *testing.B) { func Benchmark_Ctx_IP(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request() var res string b.ReportAllocs() @@ -2018,7 +2018,7 @@ func Benchmark_Ctx_IP(b *testing.B) { func Test_Ctx_Is(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderContentType, MIMETextHTML+"; boundary=something") require.True(t, c.Is(".html")) @@ -2052,7 +2052,7 @@ func Test_Ctx_Is(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Is -benchmem -count=4 func Benchmark_Ctx_Is(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderContentType, MIMEApplicationJSON) var res bool @@ -2133,7 +2133,7 @@ func Test_Ctx_Method(t *testing.T) { fctx := &fasthttp.RequestCtx{} fctx.Request.Header.SetMethod(MethodGet) app := New() - c := app.NewCtx(fctx) + c := app.AcquireCtx(fctx) require.Equal(t, MethodGet, c.Method()) c.Method(MethodPost) @@ -2289,7 +2289,7 @@ func Benchmark_Ctx_MultipartForm(b *testing.B) { func Test_Ctx_OriginalURL(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.SetRequestURI("http://google.com/test?search=demo") require.Equal(t, "http://google.com/test?search=demo", c.OriginalURL()) @@ -2368,7 +2368,7 @@ func Test_Ctx_Params_Case_Sensitive(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Params -benchmem -count=4 func Benchmark_Ctx_Params(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.route = &Route{ Params: []string{ @@ -2419,7 +2419,7 @@ func Test_Ctx_Protocol(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.Equal(t, "HTTP/1.1", c.Protocol()) @@ -2431,7 +2431,7 @@ func Test_Ctx_Protocol(t *testing.T) { func Benchmark_Ctx_Protocol(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) var res string b.ReportAllocs() @@ -2450,7 +2450,7 @@ func Test_Ctx_Scheme(t *testing.T) { freq := &fasthttp.RequestCtx{} freq.Request.Header.Set("X-Forwarded", "invalid") - c := app.NewCtx(freq) + c := app.AcquireCtx(freq) c.Request().Header.Set(HeaderXForwardedProto, schemeHTTPS) require.Equal(t, schemeHTTPS, c.Scheme()) @@ -2482,7 +2482,7 @@ func Test_Ctx_Scheme(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Scheme -benchmem -count=4 func Benchmark_Ctx_Scheme(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) var res string b.ReportAllocs() @@ -2497,7 +2497,7 @@ func Benchmark_Ctx_Scheme(b *testing.B) { func Test_Ctx_Scheme_TrustedProxy(t *testing.T) { t.Parallel() app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"0.0.0.0"}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXForwardedProto, schemeHTTPS) require.Equal(t, schemeHTTPS, c.Scheme()) @@ -2522,7 +2522,7 @@ func Test_Ctx_Scheme_TrustedProxy(t *testing.T) { func Test_Ctx_Scheme_TrustedProxyRange(t *testing.T) { t.Parallel() app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"0.0.0.0/30"}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXForwardedProto, schemeHTTPS) require.Equal(t, schemeHTTPS, c.Scheme()) @@ -2547,7 +2547,7 @@ func Test_Ctx_Scheme_TrustedProxyRange(t *testing.T) { func Test_Ctx_Scheme_UntrustedProxyRange(t *testing.T) { t.Parallel() app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"1.1.1.1/30"}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXForwardedProto, schemeHTTPS) require.Equal(t, schemeHTTP, c.Scheme()) @@ -2572,7 +2572,7 @@ func Test_Ctx_Scheme_UntrustedProxyRange(t *testing.T) { func Test_Ctx_Scheme_UnTrustedProxy(t *testing.T) { t.Parallel() app := New(Config{EnableTrustedProxyCheck: true, TrustedProxies: []string{"0.8.0.1"}}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXForwardedProto, schemeHTTPS) require.Equal(t, schemeHTTP, c.Scheme()) @@ -2597,7 +2597,7 @@ func Test_Ctx_Scheme_UnTrustedProxy(t *testing.T) { func Test_Ctx_Query(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("search=john&age=20") require.Equal(t, "john", c.Query("search")) @@ -2613,7 +2613,7 @@ func Test_Ctx_Query(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Query -benchmem -count=4 func Benchmark_Ctx_Query(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("search=john&age=8") var res string b.ReportAllocs() @@ -2628,7 +2628,7 @@ func Benchmark_Ctx_Query(b *testing.B) { func Test_Ctx_QuerySignedInt(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("search=john&age=8") // int @@ -2675,7 +2675,7 @@ func Test_Ctx_QuerySignedInt(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_QuerySignedInt -benchmem -count=4 func Benchmark_Ctx_QuerySignedInt(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("search=john&age=8") var res int b.ReportAllocs() @@ -2690,7 +2690,7 @@ func Benchmark_Ctx_QuerySignedInt(b *testing.B) { func Test_Ctx_QueryBoundarySignedInt(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) var q string // int @@ -2735,7 +2735,7 @@ func Test_Ctx_QueryBoundarySignedInt(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_QueryBoundarySignedInt -benchmem -count=4 func Benchmark_Ctx_QueryBoundarySignedInt(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("search=john&age=8") var res int b.ReportAllocs() @@ -2750,7 +2750,7 @@ func Benchmark_Ctx_QueryBoundarySignedInt(b *testing.B) { func Test_Ctx_QueryUnsignedInt(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("search=john&age=8") // uint @@ -2797,7 +2797,7 @@ func Test_Ctx_QueryUnsignedInt(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_QueryUnsignedInt -benchmem -count=4 func Benchmark_Ctx_QueryUnsignedInt(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("search=john&age=8") var res uint b.ReportAllocs() @@ -2812,7 +2812,7 @@ func Benchmark_Ctx_QueryUnsignedInt(b *testing.B) { func Test_Ctx_QueryBoundaryUnsignedInt(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) var q string // uint @@ -2857,7 +2857,7 @@ func Test_Ctx_QueryBoundaryUnsignedInt(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_QueryBoundaryUnsignedInt -benchmem -count=4 func Benchmark_Ctx_QueryBoundaryUnsignedInt(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("search=john&age=8") var res uint b.ReportAllocs() @@ -2872,7 +2872,7 @@ func Benchmark_Ctx_QueryBoundaryUnsignedInt(b *testing.B) { func Test_Ctx_QueryFloat(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("name=alex&amount=32.23&id=") @@ -2896,7 +2896,7 @@ func Test_Ctx_QueryFloat(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_QueryFloat -benchmem -count=4 func Benchmark_Ctx_QueryFloat(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("search=john&age=8") var res float32 b.ReportAllocs() @@ -2911,7 +2911,7 @@ func Benchmark_Ctx_QueryFloat(b *testing.B) { func Test_Ctx_QueryBool(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("name=alex&want_pizza=false&id=") @@ -2926,7 +2926,7 @@ func Test_Ctx_QueryBool(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_QueryBool -benchmem -count=4 func Benchmark_Ctx_QueryBool(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("search=john&age=8") var res bool b.ReportAllocs() @@ -2941,7 +2941,7 @@ func Benchmark_Ctx_QueryBool(b *testing.B) { func Test_Ctx_QueryString(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("name=alex&amount=32.23&id=") @@ -2956,7 +2956,7 @@ func Test_Ctx_QueryString(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_QueryString -benchmem -count=4 func Benchmark_Ctx_QueryString(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("search=john&age=8") var res string b.ReportAllocs() @@ -2971,7 +2971,7 @@ func Benchmark_Ctx_QueryString(b *testing.B) { func Test_Ctx_QueryBytes(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("name=alex&amount=32.23&id=") @@ -2986,7 +2986,7 @@ func Test_Ctx_QueryBytes(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_QueryBytes -benchmem -count=4 func Benchmark_Ctx_QueryBytes(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("search=john&age=8") var res []byte b.ReportAllocs() @@ -3001,7 +3001,7 @@ func Benchmark_Ctx_QueryBytes(b *testing.B) { func Test_Ctx_QueryWithoutGenericDataType(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("name=alex&amount=32.23&isAgent=true&id=32") @@ -3040,7 +3040,7 @@ func Test_Ctx_QueryWithoutGenericDataType(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_QueryWithoutGenericDataType -benchmem -count=4 func Benchmark_Ctx_QueryWithoutGenericDataType(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetQueryString("search=john&age=8") var res int b.ReportAllocs() @@ -3055,7 +3055,7 @@ func Benchmark_Ctx_QueryWithoutGenericDataType(b *testing.B) { func Test_Ctx_Range(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) testRange := func(header string, ranges ...RangeSet) { c.Request().Header.Set(HeaderRange, header) @@ -3088,7 +3088,7 @@ func Test_Ctx_Range(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Range -benchmem -count=4 func Benchmark_Ctx_Range(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) testCases := []struct { @@ -3132,7 +3132,7 @@ func Test_Ctx_Route(t *testing.T) { require.NoError(t, err, "app.Test(req)") require.Equal(t, StatusOK, resp.StatusCode, "Status code") - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.Equal(t, "/", c.Route().Path) require.Equal(t, MethodGet, c.Route().Method) @@ -3245,7 +3245,7 @@ func Test_Ctx_SaveFileToStorage(t *testing.T) { func Test_Ctx_Secure(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) // TODO Add TLS conn require.False(t, c.Secure()) @@ -3255,7 +3255,7 @@ func Test_Ctx_Secure(t *testing.T) { func Test_Ctx_Stale(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.True(t, c.Stale()) } @@ -3264,7 +3264,7 @@ func Test_Ctx_Stale(t *testing.T) { func Test_Ctx_Subdomains(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().URI().SetHost("john.doe.is.awesome.google.com") require.Equal(t, []string{"john", "doe"}, c.Subdomains(4)) @@ -3276,7 +3276,7 @@ func Test_Ctx_Subdomains(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Subdomains -benchmem -count=4 func Benchmark_Ctx_Subdomains(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetRequestURI("http://john.doe.google.com") var res []string @@ -3292,7 +3292,7 @@ func Benchmark_Ctx_Subdomains(b *testing.B) { func Test_Ctx_ClearCookie(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderCookie, "john=doe") c.ClearCookie("john") @@ -3309,7 +3309,7 @@ func Test_Ctx_ClearCookie(t *testing.T) { func Test_Ctx_Download(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.NoError(t, c.Download("ctx.go", "Awesome File!")) @@ -3346,7 +3346,7 @@ func Test_Ctx_SendFile(t *testing.T) { require.NoError(t, err) // simple test case - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err = c.SendFile("ctx.go") // check expectation require.NoError(t, err) @@ -3355,7 +3355,7 @@ func Test_Ctx_SendFile(t *testing.T) { app.ReleaseCtx(c) // test with custom error code - c = app.NewCtx(&fasthttp.RequestCtx{}) + c = app.AcquireCtx(&fasthttp.RequestCtx{}) err = c.Status(StatusInternalServerError).SendFile("ctx.go") // check expectation require.NoError(t, err) @@ -3364,7 +3364,7 @@ func Test_Ctx_SendFile(t *testing.T) { app.ReleaseCtx(c) // test not modified - c = app.NewCtx(&fasthttp.RequestCtx{}) + c = app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderIfModifiedSince, fI.ModTime().Format(time.RFC1123)) err = c.SendFile("ctx.go") // check expectation @@ -3458,7 +3458,7 @@ func Test_Ctx_SendFile_RestoreOriginalURL(t *testing.T) { func Test_Ctx_JSON(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.Error(t, c.JSON(complex(1, 1))) @@ -3499,7 +3499,7 @@ func Test_Ctx_JSON(t *testing.T) { return []byte(`["custom","json"]`), nil }, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.JSON(Map{ // map has no order "Name": "Grame", @@ -3514,7 +3514,7 @@ func Test_Ctx_JSON(t *testing.T) { // go test -run=^$ -bench=Benchmark_Ctx_JSON -benchmem -count=4 func Benchmark_Ctx_JSON(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type SomeStruct struct { Name string @@ -3538,7 +3538,7 @@ func Benchmark_Ctx_JSON(b *testing.B) { func Benchmark_Ctx_JSON_Ctype(b *testing.B) { app := New() // TODO: Check extra allocs because of the interface stuff - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed type SomeStruct struct { Name string Age uint8 @@ -3562,7 +3562,7 @@ func Benchmark_Ctx_JSON_Ctype(b *testing.B) { func Test_Ctx_JSONP(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.Error(t, c.JSONP(complex(1, 1))) @@ -3590,7 +3590,7 @@ func Test_Ctx_JSONP(t *testing.T) { return []byte(`["custom","json"]`), nil }, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.JSONP(Map{ // map has no order "Name": "Grame", @@ -3605,7 +3605,7 @@ func Test_Ctx_JSONP(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_JSONP -benchmem -count=4 func Benchmark_Ctx_JSONP(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed type SomeStruct struct { Name string @@ -3630,7 +3630,7 @@ func Benchmark_Ctx_JSONP(b *testing.B) { func Test_Ctx_XML(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed require.Error(t, c.JSON(complex(1, 1))) @@ -3667,7 +3667,7 @@ func Test_Ctx_XML(t *testing.T) { return []byte(`xml`), nil }, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) type xmlResult struct { XMLName xml.Name `xml:"Users"` @@ -3689,7 +3689,7 @@ func Test_Ctx_XML(t *testing.T) { // go test -run=^$ -bench=Benchmark_Ctx_XML -benchmem -count=4 func Benchmark_Ctx_XML(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed type SomeStruct struct { Name string `xml:"Name"` Age uint8 `xml:"Age"` @@ -3713,7 +3713,7 @@ func Benchmark_Ctx_XML(b *testing.B) { func Test_Ctx_Links(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Links() require.Equal(t, "", string(c.Response().Header.Peek(HeaderLink))) @@ -3728,7 +3728,7 @@ func Test_Ctx_Links(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Links -benchmem -count=4 func Benchmark_Ctx_Links(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed b.ReportAllocs() b.ResetTimer() @@ -3744,7 +3744,7 @@ func Benchmark_Ctx_Links(b *testing.B) { func Test_Ctx_Location(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Location("http://example.com") require.Equal(t, "http://example.com", string(c.Response().Header.Peek(HeaderLocation))) @@ -3786,7 +3786,7 @@ func Test_Ctx_Next_Error(t *testing.T) { func Test_Ctx_Render(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.Render("./.github/testdata/index.tmpl", Map{ "Title": "Hello, World!", @@ -3807,7 +3807,7 @@ func Test_Ctx_RenderWithoutLocals(t *testing.T) { app := New(Config{ PassLocalsToViews: false, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Locals("Title", "Hello, World!") @@ -3824,7 +3824,7 @@ func Test_Ctx_RenderWithLocals(t *testing.T) { t.Run("EmptyBind", func(t *testing.T) { t.Parallel() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Locals("Title", "Hello, World!") err := c.Render("./.github/testdata/index.tmpl", Map{}) @@ -3835,7 +3835,7 @@ func Test_Ctx_RenderWithLocals(t *testing.T) { t.Run("NilBind", func(t *testing.T) { t.Parallel() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Locals("Title", "Hello, World!") err := c.Render("./.github/testdata/index.tmpl", nil) @@ -3849,7 +3849,7 @@ func Test_Ctx_RenderWithBindVars(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.BindVars(Map{ "Title": "Hello, World!", @@ -3869,7 +3869,7 @@ func Test_Ctx_RenderWithBindVars(t *testing.T) { func Test_Ctx_RenderWithOverwrittenBind(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.BindVars(Map{ "Title": "Hello, World!", @@ -3894,7 +3894,7 @@ func Test_Ctx_RenderWithBindVarsLocals(t *testing.T) { PassLocalsToViews: true, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.BindVars(Map{ "Title": "Hello, World!", @@ -3920,7 +3920,7 @@ func Test_Ctx_RenderWithLocalsAndBinding(t *testing.T) { PassLocalsToViews: true, Views: engine, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Locals("Title", "This is a test.") @@ -3940,7 +3940,7 @@ func Benchmark_Ctx_RenderWithLocalsAndBindVars(b *testing.B) { PassLocalsToViews: true, Views: engine, }) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err = c.BindVars(Map{ "Title": "Hello, World!", @@ -3967,7 +3967,7 @@ func Benchmark_Ctx_RenderLocals(b *testing.B) { PassLocalsToViews: true, }) app.config.Views = engine - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Locals("Title", "Hello, World!") @@ -3988,7 +3988,7 @@ func Benchmark_Ctx_RenderBindVars(b *testing.B) { require.NoError(b, err) app := New() app.config.Views = engine - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err = c.BindVars(Map{ "Title": "Hello, World!", @@ -4107,7 +4107,7 @@ func Test_Ctx_Render_Engine(t *testing.T) { require.NoError(t, engine.Load()) app := New() app.config.Views = engine - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.Render("index.tmpl", Map{ "Title": "Hello, World!", @@ -4123,7 +4123,7 @@ func Test_Ctx_Render_Engine_With_View_Layout(t *testing.T) { require.NoError(t, engine.Load()) app := New(Config{ViewsLayout: "main.tmpl"}) app.config.Views = engine - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.Render("index.tmpl", Map{ "Title": "Hello, World!", @@ -4139,7 +4139,7 @@ func Benchmark_Ctx_Render_Engine(b *testing.B) { require.NoError(b, err) app := New() app.config.Views = engine - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) b.ReportAllocs() b.ResetTimer() @@ -4155,7 +4155,7 @@ func Benchmark_Ctx_Render_Engine(b *testing.B) { // go test -v -run=^$ -bench=Benchmark_Ctx_Get_Location_From_Route -benchmem -count=4 func Benchmark_Ctx_Get_Location_From_Route(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed app.Get("/user/:name", func(c Ctx) error { return c.SendString(c.Params("name")) @@ -4178,7 +4178,7 @@ func Test_Ctx_Get_Location_From_Route_name(t *testing.T) { t.Run("case insensitive", func(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) app.Get("/user/:name", func(c Ctx) error { return c.SendString(c.Params("name")) }).Name("User") @@ -4195,7 +4195,7 @@ func Test_Ctx_Get_Location_From_Route_name(t *testing.T) { t.Run("case sensitive", func(t *testing.T) { t.Parallel() app := New(Config{CaseSensitive: true}) - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) app.Get("/user/:name", func(c Ctx) error { return c.SendString(c.Params("name")) @@ -4215,7 +4215,7 @@ func Test_Ctx_Get_Location_From_Route_name(t *testing.T) { func Test_Ctx_Get_Location_From_Route_name_Optional_greedy(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) app.Get("/:phone/*/send/*", func(c Ctx) error { return c.SendString("Phone: " + c.Params("phone") + "\nFirst Param: " + c.Params("*1") + "\nSecond Param: " + c.Params("*2")) @@ -4234,7 +4234,7 @@ func Test_Ctx_Get_Location_From_Route_name_Optional_greedy(t *testing.T) { func Test_Ctx_Get_Location_From_Route_name_Optional_greedy_one_param(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) app.Get("/:phone/*/send", func(c Ctx) error { return c.SendString("Phone: " + c.Params("phone") + "\nFirst Param: " + c.Params("*1")) @@ -4261,7 +4261,7 @@ func Test_Ctx_Render_Engine_Error(t *testing.T) { t.Parallel() app := New() app.config.Views = errorTemplateEngine{} - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.Render("index.tmpl", nil) require.Error(t, err) @@ -4285,7 +4285,7 @@ func Test_Ctx_Render_Go_Template(t *testing.T) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err = c.Render(file.Name(), nil) require.NoError(t, err) @@ -4296,7 +4296,7 @@ func Test_Ctx_Render_Go_Template(t *testing.T) { func Test_Ctx_Send(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.NoError(t, c.Send([]byte("Hello, World"))) require.NoError(t, c.Send([]byte("Don't crash please"))) @@ -4307,7 +4307,7 @@ func Test_Ctx_Send(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Send -benchmem -count=4 func Benchmark_Ctx_Send(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) byt := []byte("Hello, World!") b.ReportAllocs() @@ -4325,7 +4325,7 @@ func Benchmark_Ctx_Send(b *testing.B) { func Test_Ctx_SendStatus(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.SendStatus(415) require.NoError(t, err) @@ -4337,7 +4337,7 @@ func Test_Ctx_SendStatus(t *testing.T) { func Test_Ctx_SendString(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.SendString("Don't crash please") require.NoError(t, err) @@ -4348,7 +4348,7 @@ func Test_Ctx_SendString(t *testing.T) { func Test_Ctx_SendStream(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.SendStream(bytes.NewReader([]byte("Don't crash please"))) require.NoError(t, err) @@ -4367,7 +4367,7 @@ func Test_Ctx_SendStream(t *testing.T) { func Test_Ctx_Set(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Set("X-1", "1") c.Set("X-2", "2") @@ -4382,7 +4382,7 @@ func Test_Ctx_Set(t *testing.T) { func Test_Ctx_Set_Splitter(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Set("Location", "foo\r\nSet-Cookie:%20SESSIONID=MaliciousValue\r\n") h := string(c.Response().Header.Peek("Location")) @@ -4396,7 +4396,7 @@ func Test_Ctx_Set_Splitter(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Set -benchmem -count=4 func Benchmark_Ctx_Set(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) val := "1431-15132-3423" b.ReportAllocs() @@ -4410,7 +4410,7 @@ func Benchmark_Ctx_Set(b *testing.B) { func Test_Ctx_Status(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Status(400) require.Equal(t, 400, c.Response().StatusCode()) @@ -4424,7 +4424,7 @@ func Test_Ctx_Status(t *testing.T) { func Test_Ctx_Type(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Type(".json") require.Equal(t, "application/json", string(c.Response().Header.Peek("Content-Type"))) @@ -4442,7 +4442,7 @@ func Test_Ctx_Type(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Type -benchmem -count=4 func Benchmark_Ctx_Type(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) b.ReportAllocs() b.ResetTimer() @@ -4455,7 +4455,7 @@ func Benchmark_Ctx_Type(b *testing.B) { // go test -v -run=^$ -bench=Benchmark_Ctx_Type_Charset -benchmem -count=4 func Benchmark_Ctx_Type_Charset(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed b.ReportAllocs() b.ResetTimer() @@ -4469,7 +4469,7 @@ func Benchmark_Ctx_Type_Charset(b *testing.B) { func Test_Ctx_Vary(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Vary("Origin") c.Vary("User-Agent") @@ -4480,7 +4480,7 @@ func Test_Ctx_Vary(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Vary -benchmem -count=4 func Benchmark_Ctx_Vary(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed b.ReportAllocs() b.ResetTimer() @@ -4493,7 +4493,7 @@ func Benchmark_Ctx_Vary(b *testing.B) { func Test_Ctx_Write(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) _, err := c.Write([]byte("Hello, ")) require.NoError(t, err) @@ -4505,7 +4505,7 @@ func Test_Ctx_Write(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Write -benchmem -count=4 func Benchmark_Ctx_Write(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) byt := []byte("Hello, World!") b.ReportAllocs() @@ -4522,7 +4522,7 @@ func Benchmark_Ctx_Write(b *testing.B) { func Test_Ctx_Writef(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) world := "World!" _, err := c.Writef("Hello, %s", world) @@ -4533,7 +4533,7 @@ func Test_Ctx_Writef(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Writef -benchmem -count=4 func Benchmark_Ctx_Writef(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed world := "World!" b.ReportAllocs() @@ -4550,7 +4550,7 @@ func Benchmark_Ctx_Writef(b *testing.B) { func Test_Ctx_WriteString(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) _, err := c.WriteString("Hello, ") require.NoError(t, err) @@ -4563,7 +4563,7 @@ func Test_Ctx_WriteString(t *testing.T) { func Test_Ctx_XHR(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXRequestedWith, "XMLHttpRequest") require.True(t, c.XHR()) @@ -4572,7 +4572,7 @@ func Test_Ctx_XHR(t *testing.T) { // go test -run=^$ -bench=Benchmark_Ctx_XHR -benchmem -count=4 func Benchmark_Ctx_XHR(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXRequestedWith, "XMLHttpRequest") var equal bool @@ -4587,7 +4587,7 @@ func Benchmark_Ctx_XHR(b *testing.B) { // go test -v -run=^$ -bench=Benchmark_Ctx_SendString_B -benchmem -count=4 func Benchmark_Ctx_SendString_B(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) body := "Hello, world!" b.ReportAllocs() @@ -4605,7 +4605,7 @@ func Benchmark_Ctx_SendString_B(b *testing.B) { func Test_Ctx_Queries(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().SetBody([]byte(``)) c.Request().Header.SetContentType("") @@ -4653,7 +4653,7 @@ func Test_Ctx_Queries(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Ctx_Queries -benchmem -count=4 func Benchmark_Ctx_Queries(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) b.ReportAllocs() b.ResetTimer() @@ -4722,7 +4722,7 @@ func Benchmark_Ctx_BodyStreamWriter(b *testing.B) { func Test_Ctx_String(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) require.Equal(t, "#0000000000000000 - 0.0.0.0:0 <-> 0.0.0.0:0 - GET http:///", c.String()) } @@ -4731,7 +4731,7 @@ func Test_Ctx_String(t *testing.T) { func Benchmark_Ctx_String(b *testing.B) { var str string app := New() - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) b.ReportAllocs() b.ResetTimer() @@ -4832,7 +4832,7 @@ func Test_Ctx_IsFromLocal_X_Forwarded(t *testing.T) { // Test unset X-Forwarded-For header. { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) // fasthttp returns "0.0.0.0" as IP as there is no remote address. require.Equal(t, "0.0.0.0", c.IP()) require.False(t, c.IsFromLocal()) @@ -4840,7 +4840,7 @@ func Test_Ctx_IsFromLocal_X_Forwarded(t *testing.T) { // Test when setting X-Forwarded-For header to localhost "127.0.0.1" { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXForwardedFor, "127.0.0.1") defer app.ReleaseCtx(c) require.False(t, c.IsFromLocal()) @@ -4848,7 +4848,7 @@ func Test_Ctx_IsFromLocal_X_Forwarded(t *testing.T) { // Test when setting X-Forwarded-For header to localhost "::1" { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXForwardedFor, "::1") defer app.ReleaseCtx(c) require.False(t, c.IsFromLocal()) @@ -4856,7 +4856,7 @@ func Test_Ctx_IsFromLocal_X_Forwarded(t *testing.T) { // Test when setting X-Forwarded-For to full localhost IPv6 address "0:0:0:0:0:0:0:1" { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXForwardedFor, "0:0:0:0:0:0:0:1") defer app.ReleaseCtx(c) require.False(t, c.IsFromLocal()) @@ -4864,7 +4864,7 @@ func Test_Ctx_IsFromLocal_X_Forwarded(t *testing.T) { // Test for a random IP address. { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderXForwardedFor, "93.46.8.90") require.False(t, c.IsFromLocal()) @@ -4889,7 +4889,7 @@ func Test_Ctx_IsFromLocal_RemoteAddr(t *testing.T) { app := New() fastCtx := &fasthttp.RequestCtx{} fastCtx.SetRemoteAddr(localIPv4) - c := app.NewCtx(fastCtx) + c := app.AcquireCtx(fastCtx) require.Equal(t, "127.0.0.1", c.IP()) require.True(t, c.IsFromLocal()) @@ -4899,7 +4899,7 @@ func Test_Ctx_IsFromLocal_RemoteAddr(t *testing.T) { app := New() fastCtx := &fasthttp.RequestCtx{} fastCtx.SetRemoteAddr(localIPv6) - c := app.NewCtx(fastCtx) + c := app.AcquireCtx(fastCtx) require.Equal(t, "::1", c.IP()) require.True(t, c.IsFromLocal()) } @@ -4908,7 +4908,7 @@ func Test_Ctx_IsFromLocal_RemoteAddr(t *testing.T) { app := New() fastCtx := &fasthttp.RequestCtx{} fastCtx.SetRemoteAddr(localIPv6long) - c := app.NewCtx(fastCtx) + c := app.AcquireCtx(fastCtx) // fasthttp should return "::1" for "0:0:0:0:0:0:0:1". // otherwise IsFromLocal() will break. require.Equal(t, "::1", c.IP()) @@ -4919,7 +4919,7 @@ func Test_Ctx_IsFromLocal_RemoteAddr(t *testing.T) { app := New() fastCtx := &fasthttp.RequestCtx{} fastCtx.SetRemoteAddr(zeroIPv4) - c := app.NewCtx(fastCtx) + c := app.AcquireCtx(fastCtx) require.Equal(t, "0.0.0.0", c.IP()) require.False(t, c.IsFromLocal()) } @@ -4928,7 +4928,7 @@ func Test_Ctx_IsFromLocal_RemoteAddr(t *testing.T) { app := New() fastCtx := &fasthttp.RequestCtx{} fastCtx.SetRemoteAddr(someIPv4) - c := app.NewCtx(fastCtx) + c := app.AcquireCtx(fastCtx) require.Equal(t, "93.46.8.90", c.IP()) require.False(t, c.IsFromLocal()) } @@ -4937,7 +4937,7 @@ func Test_Ctx_IsFromLocal_RemoteAddr(t *testing.T) { app := New() fastCtx := &fasthttp.RequestCtx{} fastCtx.SetRemoteAddr(someIPv6) - c := app.NewCtx(fastCtx) + c := app.AcquireCtx(fastCtx) require.Equal(t, "2001:db8:85a3::8a2e:370:7334", c.IP()) require.False(t, c.IsFromLocal()) } @@ -4946,7 +4946,7 @@ func Test_Ctx_IsFromLocal_RemoteAddr(t *testing.T) { // go test -run Test_Ctx_extractIPsFromHeader -v func Test_Ctx_extractIPsFromHeader(t *testing.T) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set("x-forwarded-for", "1.1.1.1,8.8.8.8 , /n, \n,1.1, a.c, 6.,6., , a,,42.118.81.169,10.0.137.108") ips := c.IPs() res := ips[len(ips)-2] @@ -4957,7 +4957,7 @@ func Test_Ctx_extractIPsFromHeader(t *testing.T) { func Test_Ctx_extractIPsFromHeader_EnableValidateIp(t *testing.T) { app := New() app.config.EnableIPValidation = true - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set("x-forwarded-for", "1.1.1.1,8.8.8.8 , /n, \n,1.1, a.c, 6.,6., , a,,42.118.81.169,10.0.137.108") ips := c.IPs() res := ips[len(ips)-2] @@ -4968,7 +4968,7 @@ func Test_Ctx_extractIPsFromHeader_EnableValidateIp(t *testing.T) { func Test_Ctx_GetRespHeaders(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Set("test", "Hello, World 👋!") c.Set("foo", "bar") @@ -4986,7 +4986,7 @@ func Test_Ctx_GetRespHeaders(t *testing.T) { func Benchmark_Ctx_GetRespHeaders(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Response().Header.Set("test", "Hello, World 👋!") c.Response().Header.Set("foo", "bar") @@ -5011,7 +5011,7 @@ func Benchmark_Ctx_GetRespHeaders(b *testing.B) { func Test_Ctx_GetReqHeaders(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set("test", "Hello, World 👋!") c.Request().Header.Set("foo", "bar") @@ -5029,7 +5029,7 @@ func Test_Ctx_GetReqHeaders(t *testing.T) { func Benchmark_Ctx_GetReqHeaders(b *testing.B) { app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set("test", "Hello, World 👋!") c.Request().Header.Set("foo", "bar") diff --git a/go.mod b/go.mod index 8411e1d122..2471590fc0 100644 --- a/go.mod +++ b/go.mod @@ -18,10 +18,8 @@ require ( github.com/andybalholm/brotli v1.1.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/klauspost/compress v1.17.6 // indirect - github.com/mattn/go-runewidth v0.0.15 // indirect github.com/philhofer/fwd v1.1.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/rivo/uniseg v0.2.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect golang.org/x/sys v0.17.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 82eafabc24..bf4bb8a486 100644 --- a/go.sum +++ b/go.sum @@ -15,14 +15,10 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= -github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw= github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/tinylib/msgp v1.1.8 h1:FCXC1xanKO4I8plpHGH2P7koL/RzZs12l/+r7vakfm0= diff --git a/middleware/adaptor/adaptor.go b/middleware/adaptor/adaptor.go index bb06218e22..82059cf158 100644 --- a/middleware/adaptor/adaptor.go +++ b/middleware/adaptor/adaptor.go @@ -160,7 +160,7 @@ func handlerFunc(app *fiber.App, h ...fiber.Handler) http.HandlerFunc { fctx.Init(req, remoteAddr, nil) if len(h) > 0 { // New fiber Ctx - ctx := app.NewCtx(&fctx) + ctx := app.AcquireCtx(&fctx) // Execute fiber Ctx err := h[0](ctx) if err != nil { diff --git a/middleware/adaptor/adaptor_test.go b/middleware/adaptor/adaptor_test.go index 7a6fa95cea..2982fb4042 100644 --- a/middleware/adaptor/adaptor_test.go +++ b/middleware/adaptor/adaptor_test.go @@ -87,7 +87,7 @@ func Test_HTTPHandler(t *testing.T) { fctx.Init(&req, remoteAddr, nil) app := fiber.New() - ctx := app.NewCtx(&fctx) + ctx := app.AcquireCtx(&fctx) defer app.ReleaseCtx(ctx) err = fiberH(ctx) diff --git a/middleware/csrf/csrf_test.go b/middleware/csrf/csrf_test.go index 83504bb2f8..99cbf85e48 100644 --- a/middleware/csrf/csrf_test.go +++ b/middleware/csrf/csrf_test.go @@ -79,16 +79,16 @@ func Test_CSRF_WithSession(t *testing.T) { // fiber context ctx := &fasthttp.RequestCtx{} - defer app.ReleaseCtx(app.NewCtx(ctx)) + defer app.ReleaseCtx(app.AcquireCtx(ctx)) // get session - sess, err := store.Get(app.NewCtx(ctx)) + sess, err := store.Get(app.AcquireCtx(ctx)) require.NoError(t, err) require.True(t, sess.Fresh()) // the session string is no longer be 123 newSessionIDString := sess.ID() - app.NewCtx(ctx).Request().Header.SetCookie("_session", newSessionIDString) + app.AcquireCtx(ctx).Request().Header.SetCookie("_session", newSessionIDString) // middleware config config := Config{ @@ -212,16 +212,16 @@ func Test_CSRF_ExpiredToken_WithSession(t *testing.T) { // fiber context ctx := &fasthttp.RequestCtx{} - defer app.ReleaseCtx(app.NewCtx(ctx)) + defer app.ReleaseCtx(app.AcquireCtx(ctx)) // get session - sess, err := store.Get(app.NewCtx(ctx)) + sess, err := store.Get(app.AcquireCtx(ctx)) require.NoError(t, err) require.True(t, sess.Fresh()) // get session id newSessionIDString := sess.ID() - app.NewCtx(ctx).Request().Header.SetCookie("_session", newSessionIDString) + app.AcquireCtx(ctx).Request().Header.SetCookie("_session", newSessionIDString) // middleware config config := Config{ @@ -714,9 +714,9 @@ func Test_CSRF_DeleteToken(t *testing.T) { ctx.Request.Header.SetMethod(fiber.MethodPost) ctx.Request.Header.Set(HeaderName, token) ctx.Request.Header.SetCookie(ConfigDefault.CookieName, token) - handler := HandlerFromContext(app.NewCtx(ctx)) + handler := HandlerFromContext(app.AcquireCtx(ctx)) if handler != nil { - if err := handler.DeleteToken(app.NewCtx(ctx)); err != nil { + if err := handler.DeleteToken(app.AcquireCtx(ctx)); err != nil { t.Fatal(err) } } @@ -746,13 +746,13 @@ func Test_CSRF_DeleteToken_WithSession(t *testing.T) { ctx := &fasthttp.RequestCtx{} // get session - sess, err := store.Get(app.NewCtx(ctx)) + sess, err := store.Get(app.AcquireCtx(ctx)) require.NoError(t, err) require.True(t, sess.Fresh()) // the session string is no longer be 123 newSessionIDString := sess.ID() - app.NewCtx(ctx).Request().Header.SetCookie("_session", newSessionIDString) + app.AcquireCtx(ctx).Request().Header.SetCookie("_session", newSessionIDString) // middleware config config := Config{ @@ -781,9 +781,9 @@ func Test_CSRF_DeleteToken_WithSession(t *testing.T) { ctx.Request.Header.SetMethod(fiber.MethodPost) ctx.Request.Header.Set(HeaderName, token) ctx.Request.Header.SetCookie(ConfigDefault.CookieName, token) - handler := HandlerFromContext(app.NewCtx(ctx)) + handler := HandlerFromContext(app.AcquireCtx(ctx)) if handler != nil { - if err := handler.DeleteToken(app.NewCtx(ctx)); err != nil { + if err := handler.DeleteToken(app.AcquireCtx(ctx)); err != nil { t.Fatal(err) } } diff --git a/middleware/session/session_test.go b/middleware/session/session_test.go index 94ddd8b798..f9166cabe4 100644 --- a/middleware/session/session_test.go +++ b/middleware/session/session_test.go @@ -21,7 +21,7 @@ func Test_Session(t *testing.T) { app := fiber.New() // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // set session ctx.Request().Header.SetCookie(store.sessionName, "123") @@ -69,7 +69,7 @@ func Test_Session(t *testing.T) { require.NoError(t, err) // requesting entirely new context to prevent falsy tests - ctx = app.NewCtx(&fasthttp.RequestCtx{}) + ctx = app.AcquireCtx(&fasthttp.RequestCtx{}) sess, err = store.Get(ctx) require.NoError(t, err) @@ -80,7 +80,7 @@ func Test_Session(t *testing.T) { // when we use the original session for the second time // the session be should be same if the session is not expired - ctx = app.NewCtx(&fasthttp.RequestCtx{}) + ctx = app.AcquireCtx(&fasthttp.RequestCtx{}) // request the server with the old session ctx.Request().Header.SetCookie(store.sessionName, id) @@ -101,7 +101,7 @@ func Test_Session_Types(t *testing.T) { app := fiber.New() // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // set cookie ctx.Request().Header.SetCookie(store.sessionName, "123") @@ -265,7 +265,7 @@ func Test_Session_Store_Reset(t *testing.T) { // fiber instance app := fiber.New() // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // get session sess, err := store.Get(ctx) @@ -298,7 +298,7 @@ func Test_Session_Save(t *testing.T) { // fiber instance app := fiber.New() // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // get session sess, err := store.Get(ctx) @@ -320,7 +320,7 @@ func Test_Session_Save(t *testing.T) { // fiber instance app := fiber.New() // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // get session sess, err := store.Get(ctx) @@ -347,7 +347,7 @@ func Test_Session_Save_Expiration(t *testing.T) { // fiber instance app := fiber.New() // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // get session sess, err := store.Get(ctx) @@ -389,7 +389,7 @@ func Test_Session_Destroy(t *testing.T) { // fiber instance app := fiber.New() // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // get session sess, err := store.Get(ctx) @@ -410,7 +410,7 @@ func Test_Session_Destroy(t *testing.T) { // fiber instance app := fiber.New() // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // get session sess, err := store.Get(ctx) @@ -449,7 +449,7 @@ func Test_Session_Cookie(t *testing.T) { // fiber instance app := fiber.New() // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // get session sess, err := store.Get(ctx) @@ -467,7 +467,7 @@ func Test_Session_Cookie_In_Response(t *testing.T) { app := fiber.New() // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // get session sess, err := store.Get(ctx) @@ -492,7 +492,7 @@ func Test_Session_Deletes_Single_Key(t *testing.T) { store := New() app := fiber.New() - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) sess, err := store.Get(ctx) require.NoError(t, err) @@ -522,7 +522,7 @@ func Test_Session_Reset(t *testing.T) { store := New() // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) t.Run("reset session data and id, and set fresh to be true", func(t *testing.T) { t.Parallel() @@ -590,7 +590,7 @@ func Test_Session_Regenerate(t *testing.T) { // a random session uuid originalSessionUUIDString := "" // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // now the session is in the storage freshSession, err := store.Get(ctx) @@ -622,7 +622,7 @@ func Test_Session_Regenerate(t *testing.T) { // go test -v -run=^$ -bench=Benchmark_Session -benchmem -count=4 func Benchmark_Session(b *testing.B) { app, store := fiber.New(), New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) defer app.ReleaseCtx(c) c.Request().Header.SetCookie(store.sessionName, "12356789") diff --git a/middleware/session/store_test.go b/middleware/session/store_test.go index bd7160fa2d..130f13feba 100644 --- a/middleware/session/store_test.go +++ b/middleware/session/store_test.go @@ -22,7 +22,7 @@ func TestStore_getSessionID(t *testing.T) { // session store store := New() // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // set cookie ctx.Request().Header.SetCookie(store.sessionName, expectedID) @@ -37,7 +37,7 @@ func TestStore_getSessionID(t *testing.T) { KeyLookup: "header:session_id", }) // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // set header ctx.Request().Header.Set(store.sessionName, expectedID) @@ -52,7 +52,7 @@ func TestStore_getSessionID(t *testing.T) { KeyLookup: "query:session_id", }) // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // set url parameter ctx.Request().SetRequestURI(fmt.Sprintf("/path?%s=%s", store.sessionName, expectedID)) @@ -73,7 +73,7 @@ func TestStore_Get(t *testing.T) { // session store store := New() // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // set cookie ctx.Request().Header.SetCookie(store.sessionName, unexpectedID) @@ -94,7 +94,7 @@ func TestStore_DeleteSession(t *testing.T) { store := New() // fiber context - ctx := app.NewCtx(&fasthttp.RequestCtx{}) + ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) // Create a new session session, err := store.Get(ctx) diff --git a/redirect_test.go b/redirect_test.go index 65f437f190..d49f526771 100644 --- a/redirect_test.go +++ b/redirect_test.go @@ -21,7 +21,7 @@ import ( func Test_Redirect_To(t *testing.T) { t.Parallel() app := New() - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.Redirect().To("http://default.com") require.NoError(t, err) @@ -41,7 +41,7 @@ func Test_Redirect_Route_WithParams(t *testing.T) { app.Get("/user/:name", func(c Ctx) error { return c.JSON(c.Params("name")) }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.Redirect().Route("user", RedirectConfig{ Params: Map{ @@ -60,7 +60,7 @@ func Test_Redirect_Route_WithParams_WithQueries(t *testing.T) { app.Get("/user/:name", func(c Ctx) error { return c.JSON(c.Params("name")) }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.Redirect().Route("user", RedirectConfig{ Params: Map{ @@ -84,7 +84,7 @@ func Test_Redirect_Route_WithOptionalParams(t *testing.T) { app.Get("/user/:name?", func(c Ctx) error { return c.JSON(c.Params("name")) }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.Redirect().Route("user", RedirectConfig{ Params: Map{ @@ -103,7 +103,7 @@ func Test_Redirect_Route_WithOptionalParamsWithoutValue(t *testing.T) { app.Get("/user/:name?", func(c Ctx) error { return c.JSON(c.Params("name")) }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.Redirect().Route("user") require.NoError(t, err) @@ -118,7 +118,7 @@ func Test_Redirect_Route_WithGreedyParameters(t *testing.T) { app.Get("/user/+", func(c Ctx) error { return c.JSON(c.Params("+")) }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.Redirect().Route("user", RedirectConfig{ Params: Map{ @@ -137,7 +137,7 @@ func Test_Redirect_Back(t *testing.T) { app.Get("/", func(c Ctx) error { return c.JSON("Home") }).Name("home") - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) err := c.Redirect().Back("/") require.NoError(t, err) @@ -159,7 +159,7 @@ func Test_Redirect_Back_WithReferer(t *testing.T) { app.Get("/back", func(c Ctx) error { return c.JSON("Back") }).Name("back") - c := app.NewCtx(&fasthttp.RequestCtx{}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderReferer, "/back") err := c.Redirect().Back("/") @@ -178,7 +178,7 @@ func Test_Redirect_Route_WithFlashMessages(t *testing.T) { return c.SendString("user") }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed err := c.Redirect().With("success", "1").With("message", "test").Route("user") require.NoError(t, err) @@ -201,7 +201,7 @@ func Test_Redirect_Route_WithOldInput(t *testing.T) { return c.SendString("user") }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().URI().SetQueryString("id=1&name=tom") err := c.Redirect().With("success", "1").With("message", "test").WithInput().Route("user") @@ -229,7 +229,7 @@ func Test_Redirect_setFlash(t *testing.T) { return c.SendString("user") }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().Header.Set(HeaderCookie, "fiber_flash=success:1,message:test,old_input_data_name:tom,old_input_data_id:1") @@ -336,7 +336,7 @@ func Benchmark_Redirect_Route(b *testing.B) { return c.JSON(c.Params("name")) }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed b.ReportAllocs() b.ResetTimer() @@ -363,7 +363,7 @@ func Benchmark_Redirect_Route_WithQueries(b *testing.B) { return c.JSON(c.Params("name")) }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed b.ReportAllocs() b.ResetTimer() @@ -395,7 +395,7 @@ func Benchmark_Redirect_Route_WithFlashMessages(b *testing.B) { return c.SendString("user") }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed b.ReportAllocs() b.ResetTimer() @@ -424,7 +424,7 @@ func Benchmark_Redirect_setFlash(b *testing.B) { return c.SendString("user") }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().Header.Set(HeaderCookie, "fiber_flash=success:1,message:test,old_input_data_name:tom,old_input_data_id:1") @@ -453,7 +453,7 @@ func Benchmark_Redirect_Messages(b *testing.B) { return c.SendString("user") }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().Header.Set(HeaderCookie, "fiber_flash=success:1,message:test,old_input_data_name:tom,old_input_data_id:1") c.Redirect().setFlash() @@ -478,7 +478,7 @@ func Benchmark_Redirect_OldInputs(b *testing.B) { return c.SendString("user") }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().Header.Set(HeaderCookie, "fiber_flash=success:1,message:test,old_input_data_name:tom,old_input_data_id:1") c.Redirect().setFlash() @@ -503,7 +503,7 @@ func Benchmark_Redirect_Message(b *testing.B) { return c.SendString("user") }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().Header.Set(HeaderCookie, "fiber_flash=success:1,message:test,old_input_data_name:tom,old_input_data_id:1") c.Redirect().setFlash() @@ -528,7 +528,7 @@ func Benchmark_Redirect_OldInput(b *testing.B) { return c.SendString("user") }).Name("user") - c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed c.Request().Header.Set(HeaderCookie, "fiber_flash=success:1,message:test,old_input_data_name:tom,old_input_data_id:1") c.Redirect().setFlash() diff --git a/router.go b/router.go index 42d5a1760b..f77b240a66 100644 --- a/router.go +++ b/router.go @@ -208,12 +208,12 @@ func (app *App) requestHandler(rctx *fasthttp.RequestCtx) { var c CustomCtx var ok bool if app.newCtxFunc != nil { - c, ok = app.AcquireCtx().(CustomCtx) + c, ok = app.AcquireCtx(rctx).(CustomCtx) if !ok { panic(errors.New("failed to type-assert to CustomCtx")) } } else { - c, ok = app.AcquireCtx().(*DefaultCtx) + c, ok = app.AcquireCtx(rctx).(*DefaultCtx) if !ok { panic(errors.New("failed to type-assert to *DefaultCtx")) } diff --git a/router_test.go b/router_test.go index 743a58edac..c25a317e32 100644 --- a/router_test.go +++ b/router_test.go @@ -651,7 +651,7 @@ func Benchmark_Router_Next(b *testing.B) { var res bool var err error - c := app.NewCtx(request).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed + c := app.AcquireCtx(request).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed b.ResetTimer() for n := 0; n < b.N; n++ { @@ -832,8 +832,7 @@ func Benchmark_Router_Github_API(b *testing.B) { for n := 0; n < b.N; n++ { c.URI().SetPath(routesFixture.TestRoutes[i].Path) - ctx := app.AcquireCtx().(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed - ctx.Reset(c) + ctx := app.AcquireCtx(c).(*DefaultCtx) //nolint:errcheck, forcetypeassert // not needed match, err = app.next(ctx) app.ReleaseCtx(ctx)