mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-22 02:32:43 +00:00
* Add initial support for hooks.
* release ctx, mutex.
* Add unit tests.
* add comment lines.
* update
* update
* remove unnecessary code.
* fix race condition.
* fix gosec.
* skip error handling for onshutdown and onresponse.
* update
* separate hooks from app.go
* make hooks field private, hook struct public and Hooks() func.
* remove onreq and onres because of they can be done by middlewares.
* OnGroupName method.
* Update hooks.go
Co-authored-by: hi019 <65871571+hi019@users.noreply.github.com>
* handle errors for name and groupname
* fix tests.
* Update app.go
* use struct fields instead of map
* add multi-handler.
* add onGroup, make prefix field public on Group struct.
* Update hooks.go
* add newhooks method.
* ✨ feature: add initial support for hooks
* remove ctx from hooks.
Co-authored-by: hi019 <65871571+hi019@users.noreply.github.com>
Co-authored-by: wernerr <rene@gofiber.io>
179 lines
3.1 KiB
Go
179 lines
3.1 KiB
Go
package fiber
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2/internal/bytebufferpool"
|
|
"github.com/gofiber/fiber/v2/utils"
|
|
)
|
|
|
|
var testSimpleHandler = func(c *Ctx) error {
|
|
return c.SendString("simple")
|
|
}
|
|
|
|
func Test_Hook_OnRoute(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
app := New()
|
|
|
|
app.Hooks().OnRoute(func(r Route) error {
|
|
utils.AssertEqual(t, "", r.Name)
|
|
|
|
return nil
|
|
})
|
|
|
|
app.Get("/", testSimpleHandler).Name("x")
|
|
|
|
subApp := New()
|
|
subApp.Get("/test", testSimpleHandler)
|
|
|
|
app.Mount("/sub", subApp)
|
|
}
|
|
|
|
func Test_Hook_OnName(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
app := New()
|
|
|
|
buf := bytebufferpool.Get()
|
|
defer bytebufferpool.Put(buf)
|
|
|
|
app.Hooks().OnName(func(r Route) error {
|
|
buf.WriteString(r.Name)
|
|
|
|
return nil
|
|
})
|
|
|
|
app.Get("/", testSimpleHandler).Name("index")
|
|
|
|
subApp := New()
|
|
subApp.Get("/test", testSimpleHandler)
|
|
subApp.Get("/test2", testSimpleHandler)
|
|
|
|
app.Mount("/sub", subApp)
|
|
|
|
utils.AssertEqual(t, "index", buf.String())
|
|
}
|
|
|
|
func Test_Hook_OnName_Error(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
app := New()
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
utils.AssertEqual(t, "unknown error", fmt.Sprintf("%v", err))
|
|
}
|
|
}()
|
|
|
|
app.Hooks().OnName(func(r Route) error {
|
|
return errors.New("unknown error")
|
|
})
|
|
|
|
app.Get("/", testSimpleHandler).Name("index")
|
|
}
|
|
|
|
func Test_Hook_OnGroup(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
app := New()
|
|
|
|
buf := bytebufferpool.Get()
|
|
defer bytebufferpool.Put(buf)
|
|
|
|
app.Hooks().OnGroup(func(g Group) error {
|
|
buf.WriteString(g.Prefix)
|
|
|
|
return nil
|
|
})
|
|
|
|
grp := app.Group("/x").Name("x.")
|
|
grp.Group("/a")
|
|
|
|
utils.AssertEqual(t, "/x/x/a", buf.String())
|
|
}
|
|
|
|
func Test_Hook_OnGroupName(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
app := New()
|
|
|
|
buf := bytebufferpool.Get()
|
|
defer bytebufferpool.Put(buf)
|
|
|
|
app.Hooks().OnGroupName(func(g Group) error {
|
|
buf.WriteString(g.name)
|
|
|
|
return nil
|
|
})
|
|
|
|
grp := app.Group("/x").Name("x.")
|
|
grp.Get("/test", testSimpleHandler)
|
|
grp.Get("/test2", testSimpleHandler)
|
|
|
|
utils.AssertEqual(t, "x.", buf.String())
|
|
}
|
|
|
|
func Test_Hook_OnGroupName_Error(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
app := New()
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
utils.AssertEqual(t, "unknown error", fmt.Sprintf("%v", err))
|
|
}
|
|
}()
|
|
|
|
app.Hooks().OnGroupName(func(g Group) error {
|
|
return errors.New("unknown error")
|
|
})
|
|
|
|
grp := app.Group("/x").Name("x.")
|
|
grp.Get("/test", testSimpleHandler)
|
|
}
|
|
|
|
func Test_Hook_OnShutdown(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
app := New()
|
|
|
|
buf := bytebufferpool.Get()
|
|
defer bytebufferpool.Put(buf)
|
|
|
|
app.Hooks().OnShutdown(func() error {
|
|
buf.WriteString("shutdowning")
|
|
|
|
return nil
|
|
})
|
|
|
|
utils.AssertEqual(t, nil, app.Shutdown())
|
|
utils.AssertEqual(t, "shutdowning", buf.String())
|
|
}
|
|
|
|
func Test_Hook_OnListen(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
app := New(Config{
|
|
DisableStartupMessage: true,
|
|
})
|
|
|
|
buf := bytebufferpool.Get()
|
|
defer bytebufferpool.Put(buf)
|
|
|
|
app.Hooks().OnListen(func() error {
|
|
buf.WriteString("ready")
|
|
|
|
return nil
|
|
})
|
|
|
|
go func() {
|
|
time.Sleep(1000 * time.Millisecond)
|
|
utils.AssertEqual(t, nil, app.Shutdown())
|
|
}()
|
|
utils.AssertEqual(t, nil, app.Listen(":9000"))
|
|
|
|
utils.AssertEqual(t, "ready", buf.String())
|
|
}
|