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]>
66 lines
2.1 KiB
Go
66 lines
2.1 KiB
Go
package zip
|
|
|
|
import (
|
|
"context"
|
|
|
|
fiber "github.com/zap-proto/fiber/v3"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// The footgun a consumer migration found, guarded.
|
|
//
|
|
// hanzoai/playground had s.Router.Fiber().Use(cors.New(...)). Fiber() returns
|
|
// the CURRENT generation's router, and a generation is a projection — so the
|
|
// next registration materialised a fresh one and the CORS middleware was
|
|
// silently discarded. No error, no panic, no CORS headers on any response.
|
|
func TestFiberEscape_ForeignRegistrationIsRefusedNotDiscarded(t *testing.T) {
|
|
app := quiet("host")
|
|
app.Raw("GET", "/live", func(c *Ctx) error { return c.String(200, "live") })
|
|
h, err := host2(app)
|
|
if err != nil {
|
|
t.Fatalf("host: %v", err)
|
|
}
|
|
|
|
// The mistake: registering on the value Fiber() handed out.
|
|
app.Fiber().Get("/smuggled", func(fc fiber.Ctx) error { return fc.SendString("smuggled") })
|
|
|
|
// The next composition must REFUSE rather than silently drop it.
|
|
defer func() {
|
|
r := recover()
|
|
if r == nil {
|
|
t.Fatal("a rebuild discarded a route registered on Fiber() without saying so")
|
|
}
|
|
msg, _ := r.(string)
|
|
for _, want := range []string{"App.Fiber()", "discard", "app.Use(mw)"} {
|
|
if !strings.Contains(msg, want) {
|
|
t.Errorf("refusal does not mention %q: %s", want, msg)
|
|
}
|
|
}
|
|
}()
|
|
later := quiet("later")
|
|
later.Raw("GET", "/later", func(c *Ctx) error { return nil })
|
|
_ = h.Include(later)
|
|
}
|
|
|
|
// The guard must not fire on zip's own work: control routes, plugin mounts and
|
|
// ordinary composition all rebuild without tripping it.
|
|
func TestFiberEscape_OrdinaryCompositionDoesNotTripTheGuard(t *testing.T) {
|
|
app := quiet("host")
|
|
app.Get("/v1/thing", func(ctx1 context.Context, in *invoiceIn) (*invoiceOut, error) { return &invoiceOut{}, nil }, WithOperationID("thing"))
|
|
h, err := host2(app)
|
|
if err != nil {
|
|
t.Fatalf("host: %v", err)
|
|
}
|
|
for i := 0; i < 3; i++ {
|
|
p := quiet("p" + itoa(i))
|
|
p.Raw("GET", "/p"+itoa(i), func(c *Ctx) error { return nil })
|
|
if err := h.Include(p); err != nil {
|
|
t.Fatalf("Include %d: %v", i, err)
|
|
}
|
|
}
|
|
if n := h.Generation(); n != 3 {
|
|
t.Errorf("generation = %d, want 3", n)
|
|
}
|
|
}
|