Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Aug 17, 2022
1 parent 8dd2e6a commit b637dbe
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 35 deletions.
31 changes: 0 additions & 31 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1296,34 +1296,3 @@ func (c *DefaultCtx) Bind() *Bind {
}
return c.bind
}

// setFlash is a method to get flash messages before removing them
func (c *DefaultCtx) setFlash() {
// parse flash messages
if c.Cookies("fiber_flash") != "" {
messages := strings.Split(c.Cookies("fiber_flash"), ",k:")
c.flashMessages = make(map[string]string, len(messages))

for _, msg := range messages {
msg = strings.Replace(msg, "k:", "", 1)
k, v := strings.Split(msg, ":")[0], strings.Split(msg, ":")[1]

c.flashMessages[k] = v
}
}

// parse old input data
if c.Cookies("fiber_flash_old_input") != "" {
messages := strings.Split(c.Cookies("fiber_flash_old_input"), ",k:")
c.oldInput = make(map[string]string, len(messages))

for _, msg := range messages {
msg = strings.Replace(msg, "k:", "", 1)
k, v := strings.Split(msg, ":")[0], strings.Split(msg, ":")[1]

c.oldInput[k] = v
}
}

c.ClearCookie("fiber_flash", "fiber_flash_old_input")
}
3 changes: 0 additions & 3 deletions ctx_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,6 @@ type Ctx interface {
// Replacement of: BodyParser, ParamsParser, GetReqHeaders, GetRespHeaders, AllParams, QueryParser, ReqHeaderParser
Bind() *Bind

// setFlash is a method to get flash messages before removing them
setFlash()

// SetReq resets fields of context that is relating to request.
setReq(fctx *fasthttp.RequestCtx)

Expand Down
33 changes: 33 additions & 0 deletions redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package fiber

import (
"fmt"
"strings"

"github.com/gofiber/fiber/v3/binder"
"github.com/gofiber/fiber/v3/utils"
Expand Down Expand Up @@ -192,3 +193,35 @@ func (r *Redirect) Back(fallback string) error {
}
return r.To(location)
}

// setFlash is a method to get flash messages before removing them
func (r *Redirect) setFlash() {
// parse flash messages
fmt.Print(r.c.Cookies("fiber_flash"))
if r.c.Cookies("fiber_flash") != "" {
messages := strings.Split(r.c.Cookies("fiber_flash"), ",k:")
r.c.flashMessages = make(map[string]string, len(messages))

for _, msg := range messages {
msg = strings.Replace(msg, "k:", "", 1)
k, v := strings.Split(msg, ":")[0], strings.Split(msg, ":")[1]

r.c.flashMessages[k] = v
}
}

// parse old input data
if r.c.Cookies("fiber_flash_old_input") != "" {
messages := strings.Split(r.c.Cookies("fiber_flash_old_input"), ",k:")
r.c.oldInput = make(map[string]string, len(messages))

for _, msg := range messages {
msg = strings.Replace(msg, "k:", "", 1)
k, v := strings.Split(msg, ":")[0], strings.Split(msg, ":")[1]

r.c.oldInput[k] = v
}
}

r.c.ClearCookie("fiber_flash", "fiber_flash_old_input")
}
76 changes: 76 additions & 0 deletions redirect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package fiber

import (
"net/url"
"strings"
"testing"

"github.com/gofiber/fiber/v3/utils"
Expand Down Expand Up @@ -203,3 +204,78 @@ func Benchmark_Redirect_Route_WithQueries(b *testing.B) {
utils.AssertEqual(b, "/user/fiber", location.Path)
utils.AssertEqual(b, url.Values{"a": []string{"a"}, "b": []string{"b"}}, location.Query())
}

// go test -run Test_Redirect_Route_WithFlashMessages
func Test_Redirect_Route_WithFlashMessages(t *testing.T) {
t.Parallel()

app := New()
app.Get("/user", func(c Ctx) error {
return c.SendString("user")
}).Name("user")

c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

c.Redirect().With("success", 1).With("message", "test").Route("user")

utils.AssertEqual(t, 302, c.Response().StatusCode())
utils.AssertEqual(t, "/user", string(c.Response().Header.Peek(HeaderLocation)))

utils.AssertEqual(t, "fiber_flash=k:success:1,k:message:test; path=/; SameSite=Lax", string(c.Response().Header.Peek(HeaderSetCookie)))

c.Redirect().setFlash()
utils.AssertEqual(t, "fiber_flash=; expires=Tue, 10 Nov 2009 23:00:00 GMT; fiber_flash_old_input=; expires=Tue, 10 Nov 2009 23:00:00 GMT", string(c.Response().Header.Peek(HeaderSetCookie)))
}

// go test -run Test_Redirect_Route_WithOldInput
func Test_Redirect_Route_WithOldInput(t *testing.T) {
t.Parallel()

app := New()
app.Get("/user", func(c Ctx) error {
return c.SendString("user")
}).Name("user")

c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

c.Request().URI().SetQueryString("id=1&name=tom")
c.Redirect().With("success", 1).With("message", "test").WithInput().Route("user")

utils.AssertEqual(t, 302, c.Response().StatusCode())
utils.AssertEqual(t, "/user", string(c.Response().Header.Peek(HeaderLocation)))

utils.AssertEqual(t, true, strings.Contains(string(c.Response().Header.Peek(HeaderSetCookie)), "fiber_flash=k:success:1,k:message:test; path=/; SameSite=Lax;"))
utils.AssertEqual(t, true, strings.Contains(string(c.Response().Header.Peek(HeaderSetCookie)), "fiber_flash_old_input=k:"))
utils.AssertEqual(t, true, strings.Contains(string(c.Response().Header.Peek(HeaderSetCookie)), "k:id:1"))
utils.AssertEqual(t, true, strings.Contains(string(c.Response().Header.Peek(HeaderSetCookie)), "k:name:tom"))

c.Redirect().setFlash()
utils.AssertEqual(t, "fiber_flash=; expires=Tue, 10 Nov 2009 23:00:00 GMT; fiber_flash_old_input=; expires=Tue, 10 Nov 2009 23:00:00 GMT", string(c.Response().Header.Peek(HeaderSetCookie)))
}

// go test -run Test_Redirect_setFlash
func Test_Redirect_setFlash(t *testing.T) {
t.Parallel()

app := New()
app.Get("/user", func(c Ctx) error {
return c.SendString("user")
}).Name("user")

c := app.NewCtx(&fasthttp.RequestCtx{}).(*DefaultCtx)

c.Request().Header.Set(HeaderCookie, "fiber_flash_old_input=k:name:tom,k:id:1")
c.Request().Header.Set(HeaderCookie, "fiber_flash=k:success:1,k:message:test")

c.Redirect().setFlash()

utils.AssertEqual(t, "fiber_flash=; expires=Tue, 10 Nov 2009 23:00:00 GMT; fiber_flash_old_input=; expires=Tue, 10 Nov 2009 23:00:00 GMT", string(c.Response().Header.Peek(HeaderSetCookie)))

utils.AssertEqual(t, "1", c.Redirect().Message("success"))
utils.AssertEqual(t, "test", c.Redirect().Message("message"))
utils.AssertEqual(t, map[string]string{"success": "1", "message": "test"}, c.Redirect().Messages())

utils.AssertEqual(t, "1", c.Redirect().Old("id"))
utils.AssertEqual(t, "tom", c.Redirect().Old("name"))
utils.AssertEqual(t, map[string]string{"id": "1", "name": "tom"}, c.Redirect().Olds())
}
2 changes: 1 addition & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (app *App) handler(rctx *fasthttp.RequestCtx) {
c.Reset(rctx)

// check flash messages
c.setFlash()
c.Redirect().setFlash()

// handle invalid http method directly
if methodInt(c.Method()) == -1 {
Expand Down

1 comment on commit b637dbe

@ReneWerner87
Copy link
Member

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: b637dbe Previous: 8dd2e6a Ratio
Benchmark_Router_Handler 2513 ns/op 128 B/op 3 allocs/op 1123 ns/op 0 B/op 0 allocs/op 2.24
Benchmark_Router_Handler_Strict_Case 2525 ns/op 128 B/op 3 allocs/op 1105 ns/op 0 B/op 0 allocs/op 2.29
Benchmark_Router_Handler_CaseSensitive 2620 ns/op 128 B/op 3 allocs/op 1116 ns/op 0 B/op 0 allocs/op 2.35
Benchmark_Router_Handler_Unescape 2521 ns/op 128 B/op 3 allocs/op 1204 ns/op 0 B/op 0 allocs/op 2.09
Benchmark_Router_Handler_StrictRouting 2475 ns/op 128 B/op 3 allocs/op 1102 ns/op 0 B/op 0 allocs/op 2.25
Benchmark_Etag 2417 ns/op 128 B/op 3 allocs/op 1031 ns/op 0 B/op 0 allocs/op 2.34
Benchmark_Middleware_Favicon 2291 ns/op 131 B/op 4 allocs/op 1009 ns/op 3 B/op 1 allocs/op 2.27
Benchmark_Limiter_Custom_Store 3160 ns/op 200 B/op 5 allocs/op 1565 ns/op 72 B/op 2 allocs/op 2.02
Benchmark_Limiter 3230 ns/op 200 B/op 5 allocs/op 1576 ns/op 72 B/op 2 allocs/op 2.05
Benchmark_Logger 2811 ns/op 128 B/op 3 allocs/op 1203 ns/op 0 B/op 0 allocs/op 2.34

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

Please sign in to comment.