Skip to content

With RedirectFixedPath enabled, the request execution panics when a 404 is expected #2959

@kpacha

Description

@kpacha

Description

When a request to a path doesn't match any registered patterns and the RedirectFixedPath flag is set to true, the request execution panics (invalid node type). This is the line throwing the panic: https://github.com/gin-gonic/gin/blob/v1.7.7/tree.go#L851

If the panic is commented, everything seems to work as expected with the exception of the test TestTreeInvalidNodeType.

How to reproduce

Add this function to the gin_integration_test.go file

func TestRedirectFixedPath(t *testing.T) {
	router := Default()

	router.RedirectFixedPath = true

	router.GET("/aa/aa", func(c *Context) { c.String(http.StatusOK, "/aa/aa") })
	router.GET("/:bb/aa", func(c *Context) { c.String(http.StatusOK, "/:bb/aa") })

	ts := httptest.NewServer(router)
	defer ts.Close()

	testRequest(t, ts.URL+"/aa/aa", "", "/aa/aa")
	testRequest(t, ts.URL+"/bb/aa", "", "/:bb/aa")

	testRequest(t, ts.URL+"/aa", "404 Not Found") // this request makes the router panic
}

Expectations

The router should return a 404

Actual result

The router panics and the client (curl) returns

curl: (52) Empty reply from server

Environment

  • go version: 1.17.3
  • gin version (or commit ref): 1.7.7
  • operating system: linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/bugFound something you weren't expecting? Report it here!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions