Skip to content

Commit

Permalink
🔥 Feature: Add ability to restart route handling (#1739)
Browse files Browse the repository at this point in the history
* 🔥 Feature: Add ability to restart route handling

* Change test names

Co-authored-by: RW <rene@gofiber.io>
  • Loading branch information
mtneug and ReneWerner87 committed Feb 3, 2022
1 parent af10fab commit 8853190
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ctx.go
Expand Up @@ -783,6 +783,14 @@ func (c *Ctx) Next() (err error) {
return err
}

// RestartRouting instead of going to the next handler. This may be usefull after
// changing the request path. Note that handlers might be executed again.
func (c *Ctx) RestartRouting() error {
c.indexRoute = -1
_, err := c.app.next(c)
return err
}

// OriginalURL contains the original request URL.
// Returned value is only valid within the handler. Do not store any references.
// Make copies or use the Immutable setting to use the value outside the Handler.
Expand Down
63 changes: 63 additions & 0 deletions ctx_test.go
Expand Up @@ -2108,6 +2108,69 @@ func Test_Ctx_RenderWithLocalsAndBinding(t *testing.T) {
utils.AssertEqual(t, "<h1>Hello, World!</h1>", string(c.Response().Body()))
}

// go test -run Test_Ctx_RestartRouting
func Test_Ctx_RestartRouting(t *testing.T) {
app := New()
calls := 0
app.Get("/", func(c *Ctx) error {
calls++
if calls < 3 {
return c.RestartRouting()
}
return nil
})
resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
utils.AssertEqual(t, 3, calls, "Number of calls")
}

// go test -run Test_Ctx_RestartRoutingWithChangedPath
func Test_Ctx_RestartRoutingWithChangedPath(t *testing.T) {
app := New()
executedOldHandler := false
executedNewHandler := false

app.Get("/old", func(c *Ctx) error {
c.Path("/new")
return c.RestartRouting()
})
app.Get("/old", func(c *Ctx) error {
executedOldHandler = true
return nil
})
app.Get("/new", func(c *Ctx) error {
executedNewHandler = true
return nil
})

resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/old", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
utils.AssertEqual(t, false, executedOldHandler, "Executed old handler")
utils.AssertEqual(t, true, executedNewHandler, "Executed new handler")
}

// go test -run Test_Ctx_RestartRoutingWithChangedPathAnd404
func Test_Ctx_RestartRoutingWithChangedPathAndCatchAll(t *testing.T) {
app := New()
app.Get("/new", func(c *Ctx) error {
return nil
})
app.Use(func(c *Ctx) error {
c.Path("/new")
// c.Next() would fail this test as a 404 is returned from the next handler
return c.RestartRouting()
})
app.Use(func(c *Ctx) error {
return ErrNotFound
})

resp, err := app.Test(httptest.NewRequest(MethodGet, "http://example.com/old", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
}

type testTemplateEngine struct {
templates *template.Template
}
Expand Down

1 comment on commit 8853190

@Fenny
Copy link
Member

@Fenny Fenny commented on 8853190 Feb 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 8853190 Previous: af10fab Ratio
Benchmark_StatusMessage/default 21 ns/op 0 B/op 0 allocs/op 10.4 ns/op 0 B/op 0 allocs/op 2.02

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.