mirror of https://github.com/gofiber/fiber.git
Fix GetLocationFromRoute bug for optional params (#1922)
* Add Global Layout for view render * Add test case for Views Layout * Update ctx_test.go * Add App Name function to pass custom app name * Remove json tag for function * Change func to string * Add test for AppName * Add RedirectToRoute and RedirectBack with fallback if referer in header not found * replace errors.New with fmt.Errorf * simplified code * Add tests for different formats * Add method to get route location and add benchmarks * Add ToString function * Fix error * rearrange case for fmt.Stringer * Fix bug for error return * Lock latest route for app.Name(namee string) * decreasing timeout for client test with timeout * remove println and adjust condition to > 0 * Change name to get route url * Change name to get route url * Update ctx.go Co-authored-by: hi019 <65871571+hi019@users.noreply.github.com> * Fix bug on getting url for optional and greedy params * Fix greedy pattern * This PR will fix #1921 (comment). The optional and greedy params were not fetched correctly * This PR will fix #1921 (comment). The optional and greedy params were not fetched correctly * This PR will fix #1921 (comment). The optional and greedy params were not fetched correctly Co-authored-by: RW <rene@gofiber.io> Co-authored-by: hi019 <65871571+hi019@users.noreply.github.com>pull/1911/head
parent
68e922d733
commit
3bb4b7ed41
16
ctx.go
16
ctx.go
|
@ -1144,16 +1144,15 @@ func (c *Ctx) Bind(vars Map) error {
|
|||
func (c *Ctx) getLocationFromRoute(route Route, params Map) (string, error) {
|
||||
buf := bytebufferpool.Get()
|
||||
for _, segment := range route.routeParser.segs {
|
||||
if segment.IsParam {
|
||||
for key, val := range params {
|
||||
if key == segment.ParamName || segment.IsGreedy {
|
||||
_, err := buf.WriteString(utils.ToString(val))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for key, val := range params {
|
||||
if segment.IsParam && (key == segment.ParamName || (segment.IsGreedy && len(key) == 1 && isInCharset(key[0], greedyParameters))) {
|
||||
_, err := buf.WriteString(utils.ToString(val))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
}
|
||||
if !segment.IsParam {
|
||||
_, err := buf.WriteString(segment.Const)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -1161,6 +1160,7 @@ func (c *Ctx) getLocationFromRoute(route Route, params Map) (string, error) {
|
|||
}
|
||||
}
|
||||
location := buf.String()
|
||||
// release buffer
|
||||
bytebufferpool.Put(buf)
|
||||
return location, nil
|
||||
}
|
||||
|
|
35
ctx_test.go
35
ctx_test.go
|
@ -2753,6 +2753,41 @@ func Test_Ctx_Get_Location_From_Route_name(t *testing.T) {
|
|||
utils.AssertEqual(t, "/user/fiber", location)
|
||||
}
|
||||
|
||||
// go test -run Test_Ctx_Get_Location_From_Route_name_Optional_greedy
|
||||
func Test_Ctx_Get_Location_From_Route_name_Optional_greedy(t *testing.T) {
|
||||
app := New()
|
||||
c := app.AcquireCtx(&fasthttp.RequestCtx{})
|
||||
defer app.ReleaseCtx(c)
|
||||
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"))
|
||||
}).Name("SendSms")
|
||||
|
||||
location, err := c.GetRouteURL("SendSms", Map{
|
||||
"phone": "23456789",
|
||||
"*1": "sms",
|
||||
"*2": "test-msg",
|
||||
})
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "/23456789/sms/send/test-msg", location)
|
||||
}
|
||||
|
||||
// go test -run Test_Ctx_Get_Location_From_Route_name_Optional_greedy_one_param
|
||||
func Test_Ctx_Get_Location_From_Route_name_Optional_greedy_one_param(t *testing.T) {
|
||||
app := New()
|
||||
c := app.AcquireCtx(&fasthttp.RequestCtx{})
|
||||
defer app.ReleaseCtx(c)
|
||||
app.Get("/:phone/*/send", func(c *Ctx) error {
|
||||
return c.SendString("Phone: " + c.Params("phone") + "\nFirst Param: " + c.Params("*1"))
|
||||
}).Name("SendSms")
|
||||
|
||||
location, err := c.GetRouteURL("SendSms", Map{
|
||||
"phone": "23456789",
|
||||
"*": "sms",
|
||||
})
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "/23456789/sms/send", location)
|
||||
}
|
||||
|
||||
type errorTemplateEngine struct{}
|
||||
|
||||
func (t errorTemplateEngine) Render(w io.Writer, name string, bind interface{}, layout ...string) error {
|
||||
|
|
2
path.go
2
path.go
|
@ -53,6 +53,8 @@ const (
|
|||
var (
|
||||
// slash has a special role, unlike the other parameters it must not be interpreted as a parameter
|
||||
routeDelimiter = []byte{slashDelimiter, '-', '.'}
|
||||
// list of greedy parameters
|
||||
greedyParameters = []byte{wildcardParam, plusParam}
|
||||
// list of chars for the parameter recognising
|
||||
parameterStartChars = []byte{wildcardParam, plusParam, paramStarterChar}
|
||||
// list of chars of delimiters and the starting parameter name char
|
||||
|
|
Loading…
Reference in New Issue