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]>
67 lines
2.0 KiB
Go
67 lines
2.0 KiB
Go
package zip_test
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/zap-proto/zip"
|
|
)
|
|
|
|
// THE HOST A CHILD SEES IS THE HOST THE CALLER ASKED FOR — through a REAL
|
|
// mount, not a stubbed client. The unit test for forward() passes a request it
|
|
// built itself; this one goes through the server path, where fasthttp has
|
|
// already parsed the URI in order to route, which is the condition under which
|
|
// Request.Write re-derives the Host header from the URI.
|
|
func TestMountedChildSeesTheCallersHost(t *testing.T) {
|
|
seen := make(chan string, 4)
|
|
|
|
child := zip.New(zip.Config{AppName: "child", DisableStartupMessage: true})
|
|
child.Raw("GET", "/v1/child/who", func(c *zip.Ctx) error {
|
|
seen <- c.Host()
|
|
return c.String(200, "ok")
|
|
})
|
|
dir := sockDir(t)
|
|
sock := dir + "/child.sock"
|
|
go func() { _ = child.Listen(sock) }()
|
|
waitSock(t, sock)
|
|
|
|
host := zip.New(zip.Config{AppName: "host", DisableStartupMessage: true})
|
|
loaded, err := zip.Load(zip.Plugin{Name: "child", Addr: sock}, "/v1/child")
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
host.Use(loaded)
|
|
|
|
// First establish what the HOST sees: if the harness does not deliver a Host,
|
|
// the child cannot be blamed for not receiving one.
|
|
host.Raw("GET", "/echo-host", func(c *zip.Ctx) error { return c.String(200, "["+c.Host()+"]") })
|
|
er := httptest.NewRequest(http.MethodGet, "/echo-host", nil)
|
|
er.Host = "lux.id"
|
|
eresp, eerr := host.Test(er)
|
|
if eerr != nil {
|
|
t.Fatalf("echo: %v", eerr)
|
|
}
|
|
ebuf := make([]byte, 64)
|
|
en, _ := eresp.Body.Read(ebuf)
|
|
_ = eresp.Body.Close()
|
|
t.Logf("HOST saw: %s", string(ebuf[:en]))
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/v1/child/who", nil)
|
|
req.Host = "lux.id"
|
|
resp, err := host.Test(req)
|
|
if err != nil {
|
|
t.Fatalf("Test: %v", err)
|
|
}
|
|
_ = resp.Body.Close()
|
|
|
|
select {
|
|
case got := <-seen:
|
|
if got != "lux.id" {
|
|
t.Fatalf("child saw Host %q, want \"lux.id\" — a brand-resolving child cannot answer as the brand it was asked as", got)
|
|
}
|
|
default:
|
|
t.Fatal("the child never served the request")
|
|
}
|
|
}
|