Skip to content

🤗 a not intuitive group middleware matching problem #1920

Open
@trim21

Description

@trim21

Question description
I'm using App.Group() with middleware but I found a middleware add in group A may match request to group B.

This may not be a bug because prefix /p indeed should match path /page, but it would be more intuitive if these middleware are only group-wide.

I'm guesing app.Group(prefix, middleware) is working just like app.Use(prefix, middleware)?

Code snippet Optional

you will see output "group '/p" and "group '/page" in this example:

package main

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"

	"github.com/gofiber/fiber/v2"
)

func main() {
	app := fiber.New()
	g1 := app.Group("/p", func(c *fiber.Ctx) error {
		fmt.Println("group '/p")
		return c.Next()
	})

	g1.Get("/1", func(c *fiber.Ctx) error {
		return c.SendString("/p/1")
	})

	g2 := app.Group("/page", func(c *fiber.Ctx) error {
		fmt.Println("group '/page")
		return c.Next()
	})

	g2.Get("/2", func(c *fiber.Ctx) error {
		return c.SendString("/page/1")
	})

	req := httptest.NewRequest(fiber.MethodGet, "/page/2", http.NoBody)
	res, err := app.Test(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()
	content, err := io.ReadAll(res.Body)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(content))
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions