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]>
113 lines
3.5 KiB
Go
113 lines
3.5 KiB
Go
package zip_test
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/zap-proto/zip"
|
|
)
|
|
|
|
// TestTwoAppsNoStateBleed proves that two zip.App instances in the same
|
|
// process maintain disjoint route tables, disjoint middleware chains,
|
|
// and disjoint internal state. This is the structural invariant that
|
|
// makes horizontal scale safe: N replicas of the same binary running N
|
|
// independent App instances must never coordinate via globals.
|
|
//
|
|
// The contract this test enforces:
|
|
// - Routes registered on app A are NOT visible to app B
|
|
// - Middleware applied to app A is NOT applied to app B's requests
|
|
// - A panic recovered in app A does not affect app B
|
|
//
|
|
// If this test fails, there is a package-level mutable map somewhere
|
|
// in the zip module that needs to move into App.
|
|
func TestTwoAppsNoStateBleed(t *testing.T) {
|
|
a := zip.New(zip.Config{AppName: "a", DisableStartupMessage: true})
|
|
b := zip.New(zip.Config{AppName: "b", DisableStartupMessage: true})
|
|
|
|
// Route registered on A only.
|
|
a.Raw("GET", "/from-a", func(c *zip.Ctx) error {
|
|
return c.JSON(200, map[string]string{"from": "a"})
|
|
})
|
|
// Route registered on B only.
|
|
b.Raw("GET", "/from-b", func(c *zip.Ctx) error {
|
|
return c.JSON(200, map[string]string{"from": "b"})
|
|
})
|
|
|
|
// Middleware only on A — sets a marker header on every A request.
|
|
a.Use(zip.H(func(c *zip.Ctx) error {
|
|
c.SetHeader("X-App", "a")
|
|
return c.Continue()
|
|
}))
|
|
// Middleware only on B — sets a different marker on B.
|
|
b.Use(zip.H(func(c *zip.Ctx) error {
|
|
c.SetHeader("X-App", "b")
|
|
return c.Continue()
|
|
}))
|
|
|
|
// A's route is reachable on A.
|
|
reqA, _ := http.NewRequest("GET", "/from-a", nil)
|
|
resA, err := a.Fiber().Test(reqA)
|
|
if err != nil {
|
|
t.Fatalf("a.Test: %v", err)
|
|
}
|
|
if resA.StatusCode != 200 {
|
|
t.Fatalf("a /from-a: status=%d, want 200", resA.StatusCode)
|
|
}
|
|
body, _ := io.ReadAll(resA.Body)
|
|
if got := string(body); got != `{"from":"a"}` {
|
|
t.Fatalf("a /from-a body=%s", got)
|
|
}
|
|
|
|
// A's route is NOT reachable on B.
|
|
reqA2B, _ := http.NewRequest("GET", "/from-a", nil)
|
|
resA2B, err := b.Fiber().Test(reqA2B)
|
|
if err != nil {
|
|
t.Fatalf("b.Test: %v", err)
|
|
}
|
|
if resA2B.StatusCode != 404 {
|
|
t.Fatalf("b /from-a: status=%d, want 404 (route leaked from A)", resA2B.StatusCode)
|
|
}
|
|
|
|
// B's route is reachable on B.
|
|
reqB, _ := http.NewRequest("GET", "/from-b", nil)
|
|
resB, err := b.Fiber().Test(reqB)
|
|
if err != nil {
|
|
t.Fatalf("b.Test: %v", err)
|
|
}
|
|
if resB.StatusCode != 200 {
|
|
t.Fatalf("b /from-b: status=%d, want 200", resB.StatusCode)
|
|
}
|
|
|
|
// B's route is NOT reachable on A.
|
|
reqB2A, _ := http.NewRequest("GET", "/from-b", nil)
|
|
resB2A, err := a.Fiber().Test(reqB2A)
|
|
if err != nil {
|
|
t.Fatalf("a.Test: %v", err)
|
|
}
|
|
if resB2A.StatusCode != 404 {
|
|
t.Fatalf("a /from-b: status=%d, want 404 (route leaked from B)", resB2A.StatusCode)
|
|
}
|
|
}
|
|
|
|
// TestTwoAppsIndependentShutdown verifies that shutting down one App
|
|
// does not affect the other.
|
|
func TestTwoAppsIndependentShutdown(t *testing.T) {
|
|
a := zip.New(zip.Config{AppName: "a", DisableStartupMessage: true})
|
|
b := zip.New(zip.Config{AppName: "b", DisableStartupMessage: true})
|
|
b.Raw("GET", "/still-up", func(c *zip.Ctx) error { return c.JSON(200, map[string]bool{"ok": true}) })
|
|
|
|
if err := a.Shutdown(); err != nil {
|
|
t.Fatalf("a.Shutdown: %v", err)
|
|
}
|
|
|
|
req, _ := http.NewRequest("GET", "/still-up", nil)
|
|
resp, err := b.Fiber().Test(req)
|
|
if err != nil {
|
|
t.Fatalf("b after a.Shutdown: %v", err)
|
|
}
|
|
if resp.StatusCode != 200 {
|
|
t.Fatalf("b /still-up: status=%d, want 200", resp.StatusCode)
|
|
}
|
|
}
|