diff --git a/app_test.go b/app_test.go index 046acad977..0598fd1ffc 100644 --- a/app_test.go +++ b/app_test.go @@ -1241,7 +1241,6 @@ func Test_App_Route(t *testing.T) { resp, err = app.Test(httptest.NewRequest(MethodGet, "/test/v1/v2/v3", nil)) require.NoError(t, err, "app.Test(req)") require.Equal(t, 200, resp.StatusCode, "Status code") - } func Test_App_Deep_Group(t *testing.T) { diff --git a/client_test.go b/client_test.go index 38a0a309e5..ee784b2140 100644 --- a/client_test.go +++ b/client_test.go @@ -63,7 +63,6 @@ func Test_Client_Unsupported_Protocol(t *testing.T) { require.Equal(t, 1, len(errs)) require.Equal(t, `unsupported protocol "ftp". http and https are supported`, errs[0].Error()) - } func Test_Client_Get(t *testing.T) { diff --git a/ctx_test.go b/ctx_test.go index 25197ec78b..fb03644535 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -559,12 +559,15 @@ func Test_Ctx_Format(t *testing.T) { require.Error(t, err) c.Request().Header.Set(HeaderAccept, MIMETextPlain) - c.Format(Map{}) + err = c.Format(Map{}) + require.NoError(t, err) require.Equal(t, "map[]", string(c.Response().Body())) type broken string c.Request().Header.Set(HeaderAccept, "broken/accept") - c.Format(broken("Hello, World!")) + require.NoError(t, err) + err = c.Format(broken("Hello, World!")) + require.NoError(t, err) require.Equal(t, `Hello, World!`, string(c.Response().Body())) } @@ -2565,7 +2568,6 @@ func Test_Ctx_RenderWithLocals(t *testing.T) { defer bytebufferpool.Put(buf) require.Equal(t, "

Hello, World!

", string(c.Response().Body())) - } func Test_Ctx_RenderWithBindVars(t *testing.T) { @@ -3073,13 +3075,16 @@ func Test_Ctx_SendStream(t *testing.T) { app := New() c := app.NewCtx(&fasthttp.RequestCtx{}) - c.SendStream(bytes.NewReader([]byte("Don't crash please"))) + err := c.SendStream(bytes.NewReader([]byte("Don't crash please"))) + require.NoError(t, err) require.Equal(t, "Don't crash please", string(c.Response().Body())) - c.SendStream(bytes.NewReader([]byte("Don't crash please")), len([]byte("Don't crash please"))) + err = c.SendStream(bytes.NewReader([]byte("Don't crash please")), len([]byte("Don't crash please"))) + require.NoError(t, err) require.Equal(t, "Don't crash please", string(c.Response().Body())) - c.SendStream(bufio.NewReader(bytes.NewReader([]byte("Hello bufio")))) + err = c.SendStream(bufio.NewReader(bytes.NewReader([]byte("Hello bufio")))) + require.NoError(t, err) require.Equal(t, "Hello bufio", string(c.Response().Body())) } @@ -3215,8 +3220,10 @@ func Test_Ctx_Write(t *testing.T) { app := New() c := app.NewCtx(&fasthttp.RequestCtx{}) - c.Write([]byte("Hello, ")) - c.Write([]byte("World!")) + _, err := c.Write([]byte("Hello, ")) + require.NoError(t, err) + _, err = c.Write([]byte("World!")) + require.NoError(t, err) require.Equal(t, "Hello, World!", string(c.Response().Body())) } @@ -3270,8 +3277,10 @@ func Test_Ctx_WriteString(t *testing.T) { app := New() c := app.NewCtx(&fasthttp.RequestCtx{}) - c.WriteString("Hello, ") - c.WriteString("World!") + _, err := c.WriteString("Hello, ") + require.NoError(t, err) + _, err = c.WriteString("World!") + require.NoError(t, err) require.Equal(t, "Hello, World!", string(c.Response().Body())) } diff --git a/helpers.go b/helpers.go index 7754abf9df..7945a6df0f 100644 --- a/helpers.go +++ b/helpers.go @@ -714,7 +714,7 @@ const ( ConstraintBool = "bool" ConstraintFloat = "float" ConstraintAlpha = "alpha" - ConstraintGuid = "guid" //nolint:revive,stylecheck // TODO: Rename to "ConstraintGUID" in v3 + ConstraintGUID = "guid" ConstraintMinLen = "minLen" ConstraintMaxLen = "maxLen" ConstraintLen = "len" diff --git a/helpers_test.go b/helpers_test.go index b5f607be0b..b48f2fdb81 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -41,7 +41,6 @@ func Test_Utils_UniqueRouteStack(t *testing.T) { route2, route3, })) - } func Test_Utils_getGroupPath(t *testing.T) { @@ -143,7 +142,6 @@ func Test_Utils_IsNoCache(t *testing.T) { ok := isNoCache(c.string) require.Equal(t, c.bool, ok, fmt.Sprintf("want %t, got isNoCache(%s)=%t", c.bool, c.string, ok)) - } } diff --git a/listen.go b/listen.go index d05659276a..34860d5e1a 100644 --- a/listen.go +++ b/listen.go @@ -145,7 +145,7 @@ func (app *App) Listen(addr string, config ...ListenConfig) error { if cfg.CertFile != "" && cfg.CertKeyFile != "" { cert, err := tls.LoadX509KeyPair(cfg.CertFile, cfg.CertKeyFile) if err != nil { - return fmt.Errorf("tls: cannot load TLS key pair from certFile=%q and keyFile=%q: %s", cfg.CertFile, cfg.CertKeyFile, err) + return fmt.Errorf("tls: cannot load TLS key pair from certFile=%q and keyFile=%q: %w", cfg.CertFile, cfg.CertKeyFile, err) } tlsHandler := &TLSHandler{} @@ -241,7 +241,7 @@ func (app *App) Listener(ln net.Listener, config ...ListenConfig) error { // Prefork is not supported for custom listeners if cfg.EnablePrefork { - fmt.Println("[Warning] Prefork isn't supported for custom listeners.") + log.Print("[Warning] Prefork isn't supported for custom listeners.") } return app.server.Serve(ln) diff --git a/listen_test.go b/listen_test.go index c677ddede3..e0dcd67238 100644 --- a/listen_test.go +++ b/listen_test.go @@ -168,7 +168,6 @@ func Test_Listen_MutualTLS(t *testing.T) { CertKeyFile: "./.github/testdata/ssl.key", CertClientFile: "./.github/testdata/ca-chain.cert.pem", })) - } // go test -run Test_Listen_MutualTLS_Prefork @@ -198,7 +197,6 @@ func Test_Listen_MutualTLS_Prefork(t *testing.T) { CertKeyFile: "./.github/testdata/ssl.key", CertClientFile: "./.github/testdata/ca-chain.cert.pem", })) - } // go test -run Test_Listener @@ -351,7 +349,6 @@ func Test_Listen_Master_Process_Show_Startup_Message(t *testing.T) { startupMessage(":3000", true, strings.Repeat(",11111,22222,33333,44444,55555,60000", 10), cfg) }) colors := Colors{} - fmt.Println(startupMessage) require.True(t, strings.Contains(startupMessage, "https://127.0.0.1:3000")) require.True(t, strings.Contains(startupMessage, "(bound on host 0.0.0.0 and port 3000)")) require.True(t, strings.Contains(startupMessage, "Child PIDs")) @@ -369,7 +366,6 @@ func Test_Listen_Master_Process_Show_Startup_MessageWithAppName(t *testing.T) { startupMessage := captureOutput(func() { app.startupMessage(":3000", true, strings.Repeat(",11111,22222,33333,44444,55555,60000", 10), cfg) }) - fmt.Println(startupMessage) require.Equal(t, "Test App v3.0.0", app.Config().AppName) require.True(t, strings.Contains(startupMessage, app.Config().AppName)) } @@ -386,7 +382,6 @@ func Test_Listen_Master_Process_Show_Startup_MessageWithAppNameNonAscii(t *testi startupMessage := captureOutput(func() { app.startupMessage(":3000", false, "", cfg) }) - fmt.Println(startupMessage) require.True(t, strings.Contains(startupMessage, "Serveur de vérification des données")) } @@ -402,7 +397,6 @@ func Test_Listen_Master_Process_Show_Startup_MessageWithDisabledPreforkAndCustom app.startupMessage("server.com:8081", true, strings.Repeat(",11111,22222,33333,44444,55555,60000", 5), cfg) }) colors := Colors{} - fmt.Println(startupMessage) require.True(t, strings.Contains(startupMessage, fmt.Sprintf("%sINFO%s", colors.Green, colors.Reset))) require.True(t, strings.Contains(startupMessage, fmt.Sprintf("%s%s%s", colors.Blue, appName, colors.Reset))) require.True(t, strings.Contains(startupMessage, fmt.Sprintf("%s%s%s", colors.Blue, "https://server.com:8081", colors.Reset))) @@ -416,7 +410,6 @@ func Test_Listen_Print_Route(t *testing.T) { printRoutesMessage := captureOutput(func() { app.printRoutesMessage() }) - fmt.Println(printRoutesMessage) require.True(t, strings.Contains(printRoutesMessage, MethodGet)) require.True(t, strings.Contains(printRoutesMessage, "/")) require.True(t, strings.Contains(printRoutesMessage, "emptyHandler")) diff --git a/middleware/adaptor/adopter_test.go b/middleware/adaptor/adopter_test.go index a8c7f4a243..7eed491813 100644 --- a/middleware/adaptor/adopter_test.go +++ b/middleware/adaptor/adopter_test.go @@ -14,6 +14,7 @@ import ( "testing" "github.com/gofiber/fiber/v3" + "github.com/stretchr/testify/require" "github.com/valyala/fasthttp" ) @@ -94,7 +95,8 @@ func Test_HTTPHandler(t *testing.T) { w.Header().Set("Header1", "value1") w.Header().Set("Header2", "value2") w.WriteHeader(http.StatusBadRequest) - fmt.Fprintf(w, "request body is %q", body) + _, err = fmt.Fprintf(w, "request body is %q", body) + require.NoError(t, err) } fiberH := HTTPHandlerFunc(http.HandlerFunc(nethttpH)) fiberH = setFiberContextValueMiddleware(fiberH, expectedContextKey, expectedContextValue) @@ -211,7 +213,7 @@ func Test_FiberAppDefaultPort(t *testing.T) { testFiberToHandlerFunc(t, true, fiber.New()) } -func testFiberToHandlerFunc(t *testing.T, checkDefaultPort bool, app ...*fiber.App) { +func testFiberToHandlerFunc(t *testing.T, checkDefaultPort bool, app ...*fiber.App) { //revive:disable-line:flag-parameter expectedMethod := fiber.MethodPost expectedRequestURI := "/foo/bar?baz=123" expectedBody := "body 123 foo bar baz" diff --git a/middleware/etag/etag.go b/middleware/etag/etag.go index 38fdc0165d..881e18c5e7 100644 --- a/middleware/etag/etag.go +++ b/middleware/etag/etag.go @@ -22,7 +22,7 @@ func New(config ...Config) fiber.Handler { crc32q := crc32.MakeTable(crcPol) // Return new handler - return func(c fiber.Ctx) (err error) { + return func(c fiber.Ctx) error { // Don't execute middleware if Next returns true if cfg.Next != nil && cfg.Next(c) { return c.Next() diff --git a/middleware/filesystem/utils.go b/middleware/filesystem/utils.go index 00247a91bf..b9d60bd6a0 100644 --- a/middleware/filesystem/utils.go +++ b/middleware/filesystem/utils.go @@ -70,8 +70,8 @@ func dirList(c fiber.Ctx, f fs.File) error { return nil } -func openFile(fs fs.FS, name string) (fs.File, error) { +func openFile(filesystem fs.FS, name string) (fs.File, error) { name = filepath.ToSlash(name) - return fs.Open(name) + return filesystem.Open(name) } diff --git a/middleware/helmet/helmet.go b/middleware/helmet/helmet.go index f966a73131..052f5d8da6 100644 --- a/middleware/helmet/helmet.go +++ b/middleware/helmet/helmet.go @@ -103,7 +103,6 @@ func New(config ...Config) fiber.Handler { } if cfg.PermissionPolicy != "" { c.Set(fiber.HeaderPermissionsPolicy, cfg.PermissionPolicy) - } return c.Next() } diff --git a/middleware/logger/logger.go b/middleware/logger/logger.go index bcbd047e74..5d0c85e09c 100644 --- a/middleware/logger/logger.go +++ b/middleware/logger/logger.go @@ -68,7 +68,7 @@ func New(config ...Config) fiber.Handler { } // Return new handler - return func(c fiber.Ctx) (err error) { + return func(c fiber.Ctx) error { // Don't execute middleware if Next returns true if cfg.Next != nil && cfg.Next(c) { return c.Next() diff --git a/middleware/redirect/redirect.go b/middleware/redirect/redirect.go index 8433ddcba4..68ff79b9b3 100644 --- a/middleware/redirect/redirect.go +++ b/middleware/redirect/redirect.go @@ -47,7 +47,7 @@ func New(config ...Config) fiber.Handler { cfg.rulesRegex = map[*regexp.Regexp]string{} // Initialize for k, v := range cfg.Rules { - k = strings.Replace(k, "*", "(.*)", -1) + k = strings.ReplaceAll(k, "*", "(.*)") k += "$" cfg.rulesRegex[regexp.MustCompile(k)] = v } diff --git a/middleware/redirect/redirect_test.go b/middleware/redirect/redirect_test.go index e47cc4b672..d2c00f016e 100644 --- a/middleware/redirect/redirect_test.go +++ b/middleware/redirect/redirect_test.go @@ -6,6 +6,7 @@ package redirect import ( + "context" "net/http" "testing" @@ -110,7 +111,7 @@ func Test_Redirect(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - req, err := http.NewRequest(fiber.MethodGet, tt.url, nil) + req, err := http.NewRequestWithContext(context.Background(), fiber.MethodGet, tt.url, nil) require.NoError(t, err) req.Header.Set("Location", "github.com/gofiber/redirect") resp, err := app.Test(req) diff --git a/mount_test.go b/mount_test.go index c374c350e6..ee0e3b5df7 100644 --- a/mount_test.go +++ b/mount_test.go @@ -282,7 +282,8 @@ func Test_Ctx_Render_Mount(t *testing.T) { t.Parallel() engine := &testTemplateEngine{} - engine.Load() + err := engine.Load() + require.NoError(t, err) sub := New(Config{ Views: engine, @@ -376,14 +377,14 @@ func Test_Ctx_Render_Mount_ParentOrSubHasViews(t *testing.T) { body, err = io.ReadAll(resp.Body) require.Equal(t, nil, err) require.Equal(t, "

I'm Bruh

", string(body)) - } func Test_Ctx_Render_MountGroup(t *testing.T) { t.Parallel() engine := &testTemplateEngine{} - engine.Load() + err := engine.Load() + require.NoError(t, err) micro := New(Config{ Views: engine, diff --git a/path.go b/path.go index c7a41dc994..9fdf2e9022 100644 --- a/path.go +++ b/path.go @@ -617,7 +617,7 @@ func getParamConstraintType(constraintPart string) TypeConstraint { return floatConstraint case ConstraintAlpha: return alphaConstraint - case ConstraintGuid: + case ConstraintGUID: return guidConstraint case ConstraintMinLen, ConstraintMinLenLower: return minLenConstraint diff --git a/path_test.go b/path_test.go index 26b11cffdb..3582f7ee2f 100644 --- a/path_test.go +++ b/path_test.go @@ -133,7 +133,6 @@ func Test_Path_parseRoute(t *testing.T) { params: []string{"*1", "*2"}, wildCardCount: 2, }, rp) - } // go test -race -run Test_Path_matchParams diff --git a/redirect.go b/redirect.go index aff9f516c7..57f0bdfd06 100644 --- a/redirect.go +++ b/redirect.go @@ -171,7 +171,6 @@ func (r *Redirect) OldInput(key string) string { } } return "" - } // Redirect to the URL derived from the specified path, with specified status. @@ -204,10 +203,10 @@ func (r *Redirect) Route(name string, config ...RedirectConfig) error { // flash messages for i, message := range r.messages { - _, _ = messageText.WriteString(message) + _, _ = messageText.WriteString(message) //nolint:errcheck // Always return nil // when there are more messages or oldInput -> add a comma if len(r.messages)-1 != i || (len(r.messages)-1 == i && len(r.oldInput) > 0) { - _, _ = messageText.WriteString(CookieDataSeparator) + _, _ = messageText.WriteString(CookieDataSeparator) //nolint:errcheck // Always return nil } } r.messages = r.messages[:0] @@ -215,9 +214,9 @@ func (r *Redirect) Route(name string, config ...RedirectConfig) error { // old input data i := 1 for k, v := range r.oldInput { - _, _ = messageText.WriteString(OldInputDataPrefix + k + CookieDataAssigner + v) + _, _ = messageText.WriteString(OldInputDataPrefix + k + CookieDataAssigner + v) //nolint:errcheck // Always return nil if len(r.oldInput) != i { - _, _ = messageText.WriteString(CookieDataSeparator) + _, _ = messageText.WriteString(CookieDataSeparator) //nolint:errcheck // Always return nil } i++ } @@ -236,10 +235,10 @@ func (r *Redirect) Route(name string, config ...RedirectConfig) error { i := 1 for k, v := range cfg.Queries { - _, _ = queryText.WriteString(k + "=" + v) + _, _ = queryText.WriteString(k + "=" + v) //nolint:errcheck // Always return nil if i != len(cfg.Queries) { - _, _ = queryText.WriteString("&") + _, _ = queryText.WriteString("&") //nolint:errcheck // Always return nil } i++ } diff --git a/redirect_test.go b/redirect_test.go index f603c6fc43..85ac1cd45b 100644 --- a/redirect_test.go +++ b/redirect_test.go @@ -22,11 +22,13 @@ func Test_Redirect_To(t *testing.T) { app := New() c := app.NewCtx(&fasthttp.RequestCtx{}) - c.Redirect().To("http://default.com") + err := c.Redirect().To("http://default.com") + require.NoError(t, err) require.Equal(t, 302, c.Response().StatusCode()) require.Equal(t, "http://default.com", string(c.Response().Header.Peek(HeaderLocation))) - c.Redirect().Status(301).To("http://example.com") + err = c.Redirect().Status(301).To("http://example.com") + require.NoError(t, err) require.Equal(t, 301, c.Response().StatusCode()) require.Equal(t, "http://example.com", string(c.Response().Header.Peek(HeaderLocation))) } @@ -40,11 +42,12 @@ func Test_Redirect_Route_WithParams(t *testing.T) { }).Name("user") c := app.NewCtx(&fasthttp.RequestCtx{}) - c.Redirect().Route("user", RedirectConfig{ + err := c.Redirect().Route("user", RedirectConfig{ Params: Map{ "name": "fiber", }, }) + require.NoError(t, err) require.Equal(t, 302, c.Response().StatusCode()) require.Equal(t, "/user/fiber", string(c.Response().Header.Peek(HeaderLocation))) } @@ -58,12 +61,13 @@ func Test_Redirect_Route_WithParams_WithQueries(t *testing.T) { }).Name("user") c := app.NewCtx(&fasthttp.RequestCtx{}) - c.Redirect().Route("user", RedirectConfig{ + err := c.Redirect().Route("user", RedirectConfig{ Params: Map{ "name": "fiber", }, Queries: map[string]string{"data[0][name]": "john", "data[0][age]": "10", "test": "doe"}, }) + require.NoError(t, err) require.Equal(t, 302, c.Response().StatusCode()) // analysis of query parameters with url parsing, since a map pass is always randomly ordered location, err := url.Parse(string(c.Response().Header.Peek(HeaderLocation))) @@ -81,11 +85,12 @@ func Test_Redirect_Route_WithOptionalParams(t *testing.T) { }).Name("user") c := app.NewCtx(&fasthttp.RequestCtx{}) - c.Redirect().Route("user", RedirectConfig{ + err := c.Redirect().Route("user", RedirectConfig{ Params: Map{ "name": "fiber", }, }) + require.NoError(t, err) require.Equal(t, 302, c.Response().StatusCode()) require.Equal(t, "/user/fiber", string(c.Response().Header.Peek(HeaderLocation))) } @@ -99,7 +104,8 @@ func Test_Redirect_Route_WithOptionalParamsWithoutValue(t *testing.T) { }).Name("user") c := app.NewCtx(&fasthttp.RequestCtx{}) - c.Redirect().Route("user") + err := c.Redirect().Route("user") + require.NoError(t, err) require.Equal(t, 302, c.Response().StatusCode()) require.Equal(t, "/user/", string(c.Response().Header.Peek(HeaderLocation))) } @@ -113,11 +119,12 @@ func Test_Redirect_Route_WithGreedyParameters(t *testing.T) { }).Name("user") c := app.NewCtx(&fasthttp.RequestCtx{}) - c.Redirect().Route("user", RedirectConfig{ + err := c.Redirect().Route("user", RedirectConfig{ Params: Map{ "+": "test/routes", }, }) + require.NoError(t, err) require.Equal(t, 302, c.Response().StatusCode()) require.Equal(t, "/user/test/routes", string(c.Response().Header.Peek(HeaderLocation))) } @@ -131,11 +138,12 @@ func Test_Redirect_Back(t *testing.T) { }).Name("home") c := app.NewCtx(&fasthttp.RequestCtx{}) - c.Redirect().Back("/") + err := c.Redirect().Back("/") + require.NoError(t, err) require.Equal(t, 302, c.Response().StatusCode()) require.Equal(t, "/", string(c.Response().Header.Peek(HeaderLocation))) - err := c.Redirect().Back() + err = c.Redirect().Back() require.Equal(t, 500, c.Response().StatusCode()) require.ErrorAs(t, ErrRedirectBackNoFallback, &err) } @@ -153,7 +161,8 @@ func Test_Redirect_Back_WithReferer(t *testing.T) { c := app.NewCtx(&fasthttp.RequestCtx{}) c.Request().Header.Set(HeaderReferer, "/back") - c.Redirect().Back("/") + err := c.Redirect().Back("/") + require.NoError(t, err) require.Equal(t, 302, c.Response().StatusCode()) require.Equal(t, "/back", c.Get(HeaderReferer)) require.Equal(t, "/back", string(c.Response().Header.Peek(HeaderLocation))) @@ -170,8 +179,8 @@ func Test_Redirect_Route_WithFlashMessages(t *testing.T) { c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) - c.Redirect().With("success", "1").With("message", "test").Route("user") - + err := c.Redirect().With("success", "1").With("message", "test").Route("user") + require.NoError(t, err) require.Equal(t, 302, c.Response().StatusCode()) require.Equal(t, "/user", string(c.Response().Header.Peek(HeaderLocation))) @@ -194,8 +203,8 @@ func Test_Redirect_Route_WithOldInput(t *testing.T) { c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) c.Request().URI().SetQueryString("id=1&name=tom") - c.Redirect().With("success", "1").With("message", "test").WithInput().Route("user") - + err := c.Redirect().With("success", "1").With("message", "test").WithInput().Route("user") + require.NoError(t, err) require.Equal(t, 302, c.Response().StatusCode()) require.Equal(t, "/user", string(c.Response().Header.Peek(HeaderLocation))) diff --git a/router.go b/router.go index 5bae26e770..e5f2ad9a2d 100644 --- a/router.go +++ b/router.go @@ -203,7 +203,7 @@ func (app *App) handler(rctx *fasthttp.RequestCtx) { // handle invalid http method directly if app.methodInt(c.Method()) == -1 { - _ = c.SendStatus(StatusNotImplemented) + _ = c.SendStatus(StatusNotImplemented) //nolint:errcheck // Always return nil return }