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

63 lines
1.9 KiB
Go

package zip
import (
"context"
"encoding/json"
"testing"
)
type tenantIn struct {
Tenant string `json:"tenant" header:"X-Tenant"`
Name string `json:"name"`
}
type tenantOut struct {
Saw string `json:"saw"`
}
// Gap 1: a typed op reads a request header WITHOUT a middleware parking it in a
// context slot — and because it declares the header, every projection sees it.
func TestHeaderParam_DeclaredHeaderBindsAndIsPublished(t *testing.T) {
app := quiet("svc")
app.Post("/v1/things", func(_ context.Context, in *tenantIn) (*tenantOut, error) {
return &tenantOut{Saw: in.Tenant}, nil
}, WithOperationID("things"))
// It binds off the wire, with no middleware installed anywhere.
req := postReq("/v1/things", "application/json", []byte(`{"name":"x"}`))
req.Header.Set("X-Tenant", "acme")
resp, err := app.Fiber().Test(req)
if err != nil {
t.Fatalf("POST: %v", err)
}
defer func() { _ = resp.Body.Close() }()
if body := readAll(t, resp.Body); body != `{"saw":"acme"}` {
t.Errorf("header did not reach the typed op: %s", body)
}
// And the DOCUMENT names it — the test a replacement for Bridge has to pass.
spec := app.OpenAPISpec()
post := spec["paths"].(map[string]map[string]any)["/v1/things"]["post"].(map[string]any)
raw, _ := json.Marshal(post["parameters"])
var params []map[string]any
_ = json.Unmarshal(raw, &params)
var found bool
for _, p := range params {
if p["name"] == "X-Tenant" && p["in"] == "header" {
found = true
}
}
if !found {
t.Errorf("the document does not declare the header parameter: %s", raw)
}
}
// Over a transport with no HTTP request the answer is honest — nothing — rather
// than a nil panic. Same shape CallerOf uses.
func TestHeaderParam_HonestOnATransportWithNoRequest(t *testing.T) {
var in tenantIn
bindHeaders(&in, nil) // the CLI's answer: there is no request to read
if in.Tenant != "" {
t.Errorf("bindHeaders invented a value with no request: %q", in.Tenant)
}
}