Files
zeekayandHanzo Dev 3011b92ea4 routing: one router, and it is zip
gorilla/mux, otelmux, gorilla/handlers and rs/cors leave go.mod. Every route
the runtime serves is a zip route on a zip group: 233 from the apiserver tree,
134 from query-service, 367 total — the same 367 the mount publishes.

pkg/http/routing is the one registration surface. It exists because a
registration carries three facts zip has no slot for, and the tree this
replaces recovered all three PER REQUEST by asking the router what it had just
matched: the bound segments, the path template, and the route's declared
resources. mux.CurrentRoute(req).GetHandler() was type-asserted back to
handler.Handler in two separate packages to re-derive at request time what was
already known once at boot. Stating them at registration deletes both lookups.

The middleware chain is composed at the LEAF rather than registered ambient,
because Audit keys on the route template and Resource resolves the route's
declared resources — ambient middleware runs before the router has matched, so
both would read an empty route. Per-leaf composition reproduces the order the
old tree ran in exactly: match, then middleware, then handler.

Duplicate registration now panics at boot; gorilla let the first one win.
The Sentry ingest constraint becomes fiber's own guid — a UUID parse instead of
a hand-written character class. Paths keep their public brace spelling in the
source and in the table; the respelling happens in one function.

The standalone server hands its listener to the app, so streams and the
websocket upgrade run native fasthttp.

Counted, not asserted: routes_test.go in the apiserver pins 233 against the
real AddToRouter, routing_test.go pins the table against the router itself.
This repo has already shipped 83 typed ops that reached no router while the
suite stayed green — an uncalled func is legal Go, so arithmetic is the test.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-01 19:30:40 -07:00

83 lines
3.0 KiB
Go

package implspanmapper
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/hanzoai/o11y/pkg/http/routing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/hanzoai/o11y/pkg/errors"
"github.com/hanzoai/o11y/pkg/types/spantypes"
"github.com/hanzoai/o11y/pkg/valuer"
)
// The two registered templates, verbatim from the apiserver. The names matter as
// much as the shapes: the group id is carried as {groupId} on BOTH, so a helper
// reading "id" would answer "" on every route and the mappers of one group would
// be served as the mappers of none.
const (
groupRoute = "/v1/o11y/span_mapper_groups/{groupId}"
mapperRoute = "/v1/o11y/span_mapper_groups/{groupId}/span_mappers/{mapperId}"
)
// served drives one request through a REAL router registered at template and
// hands back the request the handler saw, so the helpers below are exercised
// against what the ROUTER matched rather than against injected vars.
func served(t *testing.T, template, target string) *http.Request {
t.Helper()
var got *http.Request
router := routing.Serve(http.MethodGet, template, http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) { got = req }))
rec := httptest.NewRecorder()
router.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, target, http.NoBody))
require.Equal(t, http.StatusOK, rec.Code, "the route did not match — the test is not exercising the read")
require.NotNil(t, got)
return got
}
func TestPathIDsAreTheSegmentsTheRouterMatched(t *testing.T) {
group, mapper := valuer.GenerateUUID(), valuer.GenerateUUID()
req := served(t, mapperRoute, "/v1/o11y/span_mapper_groups/"+group.String()+"/span_mappers/"+mapper.String())
gotGroup, err := groupIDFromPath(req)
require.NoError(t, err)
assert.Equal(t, group, gotGroup)
gotMapper, err := mapperIDFromPath(req)
require.NoError(t, err)
assert.Equal(t, mapper, gotMapper)
}
// The group routes carry the same segment name one level up, and the group
// handlers read it with the same helper.
func TestGroupIDIsReadOnTheGroupRoute(t *testing.T) {
group := valuer.GenerateUUID()
got, err := groupIDFromPath(served(t, groupRoute, "/v1/o11y/span_mapper_groups/"+group.String()))
require.NoError(t, err)
assert.Equal(t, group, got)
}
// A segment that is not a uuid is refused with the mapping resource's own code,
// not answered with the zero uuid — an empty or malformed id must never reach the
// module as a lookup for whatever the zero value matches.
func TestPathIDsRefuseANonUUIDSegment(t *testing.T) {
req := served(t, mapperRoute, "/v1/o11y/span_mapper_groups/not-a-uuid/span_mappers/also-not-a-uuid")
group, err := groupIDFromPath(req)
require.Error(t, err)
assert.True(t, errors.Asc(err, spantypes.ErrCodeMappingInvalidInput), "want the mapping invalid-input code, got %v", err)
assert.Equal(t, valuer.UUID{}, group)
mapper, err := mapperIDFromPath(req)
require.Error(t, err)
assert.True(t, errors.Asc(err, spantypes.ErrCodeMappingInvalidInput), "want the mapping invalid-input code, got %v", err)
assert.Equal(t, valuer.UUID{}, mapper)
}