Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQUEST] - Add RedirectToRoute and RedirectBack #1750

Merged
merged 30 commits into from Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ae0cc9a
Merge pull request #4 from gofiber/master
sujit-baniya May 6, 2021
744fbe2
Merge branch 'gofiber:master' into master
sujit-baniya Jun 2, 2021
75b5c18
Merge branch 'gofiber:master' into master
sujit-baniya Jun 14, 2021
5a81a51
Add Global Layout for view render
sujit-baniya Jun 14, 2021
2437d8f
Add test case for Views Layout
sujit-baniya Jun 14, 2021
6f4f14f
Update ctx_test.go
ReneWerner87 Jun 14, 2021
3812595
Merge branch 'gofiber:master' into master
sujit-baniya Jun 23, 2021
27a01e7
Merge branch 'gofiber:master' into master
sujit-baniya Jun 27, 2021
2f05a64
Merge branch 'gofiber:master' into master
sujit-baniya Jul 1, 2021
ad8b4f9
Add App Name function to pass custom app name
sujit-baniya Jul 1, 2021
5b6e866
Remove json tag for function
sujit-baniya Jul 1, 2021
3ee7557
Change func to string
sujit-baniya Jul 1, 2021
1c6e102
Add test for AppName
sujit-baniya Jul 5, 2021
5dddb64
Merge branch 'master' into master
ReneWerner87 Jul 16, 2021
86ef9ce
Merge branch 'gofiber:master' into master
sujit-baniya Aug 21, 2021
3a367be
Merge branch 'gofiber:master' into master
sujit-baniya Feb 4, 2022
a5f6791
Add RedirectToRoute and RedirectBack with fallback if referer in head…
sujit-baniya Feb 4, 2022
82873c8
replace errors.New with fmt.Errorf
sujit-baniya Feb 4, 2022
8e8d92e
simplified code
sujit-baniya Feb 4, 2022
0215512
Add tests for different formats
sujit-baniya Feb 7, 2022
9013dc5
Add method to get route location and add benchmarks
sujit-baniya Feb 7, 2022
e716d23
Add ToString function
sujit-baniya Feb 7, 2022
d694752
Fix error
sujit-baniya Feb 8, 2022
05dd7ba
rearrange case for fmt.Stringer
sujit-baniya Feb 8, 2022
a9bc1f6
Fix bug for error return
sujit-baniya Feb 8, 2022
9c6a15d
Merge branch 'gofiber:master' into master
sujit-baniya Feb 8, 2022
3075c02
Lock latest route for app.Name(namee string)
sujit-baniya Feb 8, 2022
81455f0
Merge remote-tracking branch 'origin/master'
sujit-baniya Feb 8, 2022
245148e
decreasing timeout for client test with timeout
sujit-baniya Feb 8, 2022
4591b66
remove println and adjust condition to > 0
sujit-baniya Feb 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 35 additions & 1 deletion ctx.go
Expand Up @@ -1044,7 +1044,7 @@ func (c *Ctx) Range(size int) (rangeData Range, err error) {

// Redirect to the URL derived from the specified path, with specified status.
// If status is not specified, status defaults to 302 Found.
func (c *Ctx) Redirect(location string, status ...int) error {
func (c *Ctx) redirect(location string, status ...int) error {
c.setCanonical(HeaderLocation, location)
if len(status) > 0 {
c.Status(status[0])
Expand All @@ -1054,6 +1054,40 @@ func (c *Ctx) Redirect(location string, status ...int) error {
return nil
}

// Redirect to the URL derived from the specified path, with specified status.
// If status is not specified, status defaults to 302 Found.
func (c *Ctx) Redirect(location string, status ...int) error {
ReneWerner87 marked this conversation as resolved.
Show resolved Hide resolved
return c.redirect(location, status...)
}

// RedirectToRoute to the Route registered in the app with appropriate parameters
// If status is not specified, status defaults to 302 Found.
func (c *Ctx) RedirectToRoute(routeName string, params Map, status ...int) error {
route := c.App().GetRoute(routeName)
location := ""
for _, segment := range route.routeParser.segs {
location = fmt.Sprintf("%s%s", location, segment.Const)
if segment.IsParam {
if val, ok := params[segment.ParamName]; ok {
location = fmt.Sprintf("%s%s", location, val)
ReneWerner87 marked this conversation as resolved.
Show resolved Hide resolved
} else {
return fmt.Errorf("redirection failed. No value for param: `%s`", segment.ParamName)
}
}
}
return c.redirect(location, status...)
}

// RedirectBack to the URL to referer
// If status is not specified, status defaults to 302 Found.
func (c *Ctx) RedirectBack(fallback string, status ...int) error {
location := c.Get(HeaderReferer)
if location == "" {
location = fallback
}
return c.redirect(location, status...)
}

// Render a template with data and sends a text/html response.
// We support the following engines: html, amber, handlebars, mustache, pug
func (c *Ctx) Render(name string, bind interface{}, layouts ...string) error {
Expand Down
31 changes: 31 additions & 0 deletions ctx_test.go
Expand Up @@ -2026,6 +2026,37 @@ func Test_Ctx_Redirect(t *testing.T) {
utils.AssertEqual(t, "http://example.com", string(c.Response().Header.Peek(HeaderLocation)))
}

// go test -run Test_Ctx_RedirectToRoute
func Test_Ctx_RedirectToRoute(t *testing.T) {
t.Parallel()
app := New()
app.Get("/user/:name", func(c *Ctx) error {
return c.JSON(c.Params("name"))
}).Name("user")
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)

c.RedirectToRoute("user", Map{
"name": "fiber",
})
utils.AssertEqual(t, 302, c.Response().StatusCode())
utils.AssertEqual(t, "/user/fiber", string(c.Response().Header.Peek(HeaderLocation)))
ReneWerner87 marked this conversation as resolved.
Show resolved Hide resolved
}

// go test -run Test_Ctx_RedirectBack
func Test_Ctx_RedirectBack(t *testing.T) {
ReneWerner87 marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()
app := New()
app.Get("/", func(c *Ctx) error {
return c.JSON("Home")
}).Name("home")
c := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(c)
c.RedirectBack("/")
utils.AssertEqual(t, 302, c.Response().StatusCode())
utils.AssertEqual(t, "/", string(c.Response().Header.Peek(HeaderLocation)))
}

// go test -run Test_Ctx_Render
func Test_Ctx_Render(t *testing.T) {
t.Parallel()
Expand Down