mirror of
https://github.com/zap-proto/zip.git
synced 2026-10-19 15:38:57 +00:00
App, Group, Operation. A group is a prefix and what holds under it, an operation
is one typed route declared there, and the verb methods are generic methods —
which Go 1.27 allows on a concrete type and still refuses on an interface. So In
and Out come from the handler and the caller writes no type arguments:
users := app.Group("/v1/users")
users.Post("/", api.Create)
mint := users.Group("/mint").With(middleware.Mint(gate))
mint.Post("/deposit", api.Deposit)
A Group is a CLOSED HANDLE: two unexported fields, and no method that hands back
the app, the parent or the fiber underneath. A subsystem given the group at its
own prefix cannot reach above it, so confinement is a property of the type
instead of a rule something has to police — which is what a host needs in order
to hand out a bounded surface and know what comes back.
That is bought by giving up the router interface, deliberately. Router and
OpTarget are gone, and with them the ability to implement a route table outside
this package: a decorator is no longer a router, a gate is middleware on a group,
and a bound is a path. The five package-level verb functions go the same way —
a registration is a method on the thing it registers into.
Raw carries what is not a typed operation: a stream, an upgrade, a probe, a
foreign subtree. It names its method, and it is the longer spelling because
opting out of the typed model is the exception and should read like one.
Co-authored-by: Hanzo Dev <[email protected]>
62 lines
2.1 KiB
Go
62 lines
2.1 KiB
Go
package zip_test
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/zap-proto/zip"
|
|
)
|
|
|
|
// TestRouteChainOrder pins the ONE registration order: middleware first, the
|
|
// terminal handler LAST, executed in exactly that order. A regression here
|
|
// inverts every auth gate in every app built on zip.
|
|
func TestRouteChainOrder(t *testing.T) {
|
|
app := zip.New(zip.Config{DisableStartupMessage: true})
|
|
var order []string
|
|
app.Raw("GET", "/x",
|
|
func(c *zip.Ctx) error { order = append(order, "mw1"); return c.Next() },
|
|
func(c *zip.Ctx) error { order = append(order, "mw2"); return c.Next() },
|
|
func(c *zip.Ctx) error { order = append(order, "handler"); return c.NoContent(204) },
|
|
)
|
|
if _, err := app.Fiber().Test(httptest.NewRequest(http.MethodGet, "/x", nil)); err != nil {
|
|
t.Fatalf("Test: %v", err)
|
|
}
|
|
if len(order) != 3 || order[0] != "mw1" || order[1] != "mw2" || order[2] != "handler" {
|
|
t.Fatalf("chain order = %v, want [mw1 mw2 handler]", order)
|
|
}
|
|
}
|
|
|
|
// TestRouteChainGateStops pins the abort contract: a middleware that renders
|
|
// WITHOUT calling Next stops the chain — the terminal never runs.
|
|
func TestRouteChainGateStops(t *testing.T) {
|
|
app := zip.New(zip.Config{DisableStartupMessage: true})
|
|
reached := false
|
|
app.Raw("GET", "/x",
|
|
func(c *zip.Ctx) error { return c.NoContent(401) }, // gate: no Next
|
|
func(c *zip.Ctx) error { reached = true; return c.NoContent(200) },
|
|
)
|
|
resp, err := app.Fiber().Test(httptest.NewRequest(http.MethodGet, "/x", nil))
|
|
if err != nil {
|
|
t.Fatalf("Test: %v", err)
|
|
}
|
|
if resp.StatusCode != 401 || reached {
|
|
t.Fatalf("status=%d reached=%v, want 401,false", resp.StatusCode, reached)
|
|
}
|
|
}
|
|
|
|
// TestEmptyLeafIsGroupRoot pins the gin/express idiom: Get("") on a group is
|
|
// the group root.
|
|
func TestEmptyLeafIsGroupRoot(t *testing.T) {
|
|
app := zip.New(zip.Config{DisableStartupMessage: true})
|
|
g := app.Group("/tm")
|
|
g.Raw("GET", "", func(c *zip.Ctx) error { return c.NoContent(204) })
|
|
resp, err := app.Fiber().Test(httptest.NewRequest(http.MethodGet, "/tm", nil))
|
|
if err != nil {
|
|
t.Fatalf("Test: %v", err)
|
|
}
|
|
if resp.StatusCode != 204 {
|
|
t.Fatalf("GET /tm = %d, want 204 (empty leaf = group root)", resp.StatusCode)
|
|
}
|
|
}
|