Files
zip/multistatus_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

89 lines
2.6 KiB
Go

package zip
import (
"context"
"strings"
"testing"
)
type createIn struct {
Name string `json:"name"`
}
type createOut struct {
ID string `json:"id"`
Existed bool `json:"existed"`
}
// The answer states its own status. No mutable slot, no side channel.
func (c *createOut) StatusCode() int {
if c.Existed {
return 200
}
return 201
}
// Gap 2: a route with two legitimate success codes is declarable, the handler
// selects per request, and BOTH reach the document.
func TestMultiStatus_BothCodesAreDeclaredAndSelectable(t *testing.T) {
app := quiet("svc")
app.Post("/v1/things", func(_ context.Context, in *createIn) (*createOut, error) {
return &createOut{ID: in.Name, Existed: in.Name == "old"}, nil
}, WithStatus(201, 200), WithOperationID("create"))
// Both codes are in the document — the visibility constraint.
spec := app.OpenAPISpec()
post := spec["paths"].(map[string]map[string]any)["/v1/things"]["post"].(map[string]any)
resp := post["responses"].(map[string]any)
for _, want := range []string{"200", "201"} {
if _, ok := resp[want]; !ok {
t.Errorf("document omits declared status %s: %v", want, keysOfSchemas(resp))
}
}
// And the handler selects per request.
if code, _ := postJSON(t, app, "/v1/things", `{"name":"new"}`); code != 201 {
t.Errorf("new thing answered %d, want 201", code)
}
if code, _ := postJSON(t, app, "/v1/things", `{"name":"old"}`); code != 200 {
t.Errorf("existing thing answered %d, want 200", code)
}
}
type sneakOut struct{}
func (sneakOut) StatusCode() int { return 202 }
// A code the op did not declare is refused rather than written: the document
// publishes the declared set, so anything else tells the caller one thing and
// sends another.
func TestMultiStatus_UndeclaredCodeIsRefused(t *testing.T) {
app := quiet("svc")
app.Post("/v1/sneak", func(_ context.Context, in *createIn) (*sneakOut, error) {
return &sneakOut{}, nil
}, WithStatus(200), WithOperationID("sneak"))
code, body := postJSON(t, app, "/v1/sneak", `{"name":"x"}`)
if code == 202 {
t.Fatal("an undeclared status reached the wire")
}
if code != 500 {
t.Errorf("undeclared status answered %d, want 500", code)
}
for _, want := range []string{"202", "WithStatus"} {
if !strings.Contains(body, want) {
t.Errorf("refusal does not name %q: %s", want, body)
}
}
}
func postJSON(t *testing.T, a *App, path, body string) (int, string) {
t.Helper()
req := postReq(path, "application/json", []byte(body))
resp, err := a.Fiber().Test(req)
if err != nil {
t.Fatalf("POST %s: %v", path, err)
}
defer func() { _ = resp.Body.Close() }()
return resp.StatusCode, readAll(t, resp.Body)
}