1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-22 15:03:57 +00:00

fix pprof redirect path (#1330)

* 📒:fix redirect path

* ️:fix test case

* 📚:use constant instead of number

* 🔥:TrimRight instead of TrimSuffix
This commit is contained in:
bestgopher 2021-05-19 18:47:07 +08:00 committed by GitHub
parent 67adaeadec
commit 2f05108351
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View File

@ -34,7 +34,7 @@ func New() fiber.Handler {
}
// Switch to original path without stripped slashes
switch path {
case "/debug/pprof/":
case "/debug/pprof":
pprofIndex(c.Context())
case "/debug/pprof/cmdline":
pprofCmdline(c.Context())
@ -58,7 +58,13 @@ func New() fiber.Handler {
pprofThreadcreate(c.Context())
default:
// pprof index only works with trailing slash
return c.Redirect("/debug/pprof/", 302)
if strings.HasSuffix(path, "/") {
path = strings.TrimRight(path, "/")
} else {
path = "/debug/pprof"
}
return c.Redirect(path, fiber.StatusFound)
}
return nil
}

View File

@ -2,11 +2,12 @@ package pprof
import (
"bytes"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"io/ioutil"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
)
func Test_Non_Pprof_Path(t *testing.T) {
@ -36,7 +37,7 @@ func Test_Pprof_Index(t *testing.T) {
return c.SendString("escaped")
})
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/debug/pprof/", nil))
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/debug/pprof", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 200, resp.StatusCode)
utils.AssertEqual(t, fiber.MIMETextHTMLCharsetUTF8, resp.Header.Get(fiber.HeaderContentType))