Files
zip/gate_answers_test.go
zeekayandHanzo Dev 5f71d2496b The router is three nouns, and every one of them is concrete
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]>
2026-09-17 01:39:17 -07:00

53 lines
1.9 KiB
Go

package zip
import (
"net/http"
"net/http/httptest"
"testing"
)
// A gate that cannot refuse is not a gate. Middleware in Use position may write a
// response and return without Next — an auth check, a rate limiter, a cache hit, a
// maintenance refusal. This is asserted rather than described because it was
// DESCRIBED as forbidden for a while ("a Handler may never terminate a request"),
// and a consumer read that and routed a 503 refusal through the host's error handler
// instead of answering, which a host that flattens errors to 500 then swallowed.
//
// What stays refused is a handler that CLAIMS an address from Use position — see
// TestUse_RefusesTerminal. The two are different facts and only one is a rule.
func TestUse_MiddlewareMayAnswer(t *testing.T) {
app := New(Config{DisableStartupMessage: true})
app.Use(H(func(c *Ctx) error {
if c.Header("X-Token") == "" {
c.SetHeader("Retry-After", "30")
return c.JSON(http.StatusUnauthorized, map[string]any{"error": "no token"})
}
return c.Next()
}))
app.Raw("GET", "/thing", func(c *Ctx) error { return c.String(200, "served") })
if err := app.Build(); err != nil {
t.Fatalf("Build refused a gate that answers: %v", err)
}
refused, err := app.Test(httptest.NewRequest(http.MethodGet, "/thing", nil))
if err != nil {
t.Fatal(err)
}
if refused.StatusCode != http.StatusUnauthorized {
t.Errorf("no token: status = %d, want 401 — the gate could not short-circuit", refused.StatusCode)
}
if got := refused.Header.Get("Retry-After"); got != "30" {
t.Errorf("no token: Retry-After = %q, want the gate's own header", got)
}
req := httptest.NewRequest(http.MethodGet, "/thing", nil)
req.Header.Set("X-Token", "t")
allowed, err := app.Test(req)
if err != nil {
t.Fatal(err)
}
if allowed.StatusCode != http.StatusOK {
t.Errorf("with token: status = %d, want 200 — Next must still reach the route", allowed.StatusCode)
}
}