Files
forge/modules/captcha/testrouter_test.go
zeekayandHanzo Dev 1ed3377dce web: our own router; chi is gone from this codebase
modules/web/mux.go routes the 1333 routes. chi is no longer imported anywhere in
our source.

A tree over path segments, each node trying children by specificity: exact
segment, then patterned segments in registration order, then a trailing
wildcard. That ordering is the contract — "/user/settings" must beat
"/{username}" or a user named "settings" takes over the settings page — and
matching BACKTRACKS, because a segment can match here and fail deeper while the
next candidate at this level still has to be tried.

Four segment shapes, all of which this codebase actually uses:

	users             static
	{id}              whole segment          (577)
	{sha:[a-f0-9]+}   regexp-constrained     (43)
	PACKAGES{format}  parameter mid-segment  (CRAN indexes)
	*                 trailing wildcard      (131)

A plain {name} carries no regexp. It is the overwhelming majority, and it does
not need one to match a whole segment.

The tests are the point of this commit, not the code. Each case is a way a
router can be wrong that a passing build will not reveal: static beating a
param, a regexp declining and the NEXT candidate winning (/repo/zzz is not a
sha, but "repo" is a legal username), a wildcard capturing the remainder,
"/*" matching "/" itself, HEAD falling back to GET, and — the one that matters
most — a failed branch leaving NO captured parameters behind for the branch
tried next.

Three things this turned up while wiring it in:

  Mount had to strip its prefix. A sub-router mounted at "/v1" writes its
  patterns relative to it, so handing it the full path matches nothing.
  chi_middleware.GetHead was deleted rather than reimplemented: Mux.handlerFor
  already falls back from HEAD to GET, so keeping it would have been two
  implementations of one rule.
  ChiRoutePathHandler is now RoutePathHandler and sets RouteContext.RoutePath.
  It exists so a "%2f" inside a parameter routes as one segment — without it a
  branch called "feat/x" splits and matches the wrong handler.

The vendored middleware tests used chi to register two routes each; they use a
small stdlib ServeMux shim now. modules/web imports modules/binding, so those
tests cannot import web back.

github.com/go-chi/chi/v5 remains in go.mod as an INDIRECT dependency of
markbates/goth/gothic, which drives every OAuth2 login. That is goth pulling it,
not us.

Everything green; integration compile gate green.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 19:20:07 -07:00

37 lines
1.2 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// SPDX-License-Identifier: MIT
package captcha
import "net/http"
// testRouter is the small router these vendored tests need to exercise the
// middleware. It replaces chi, which was pulled in as a dependency purely so
// upstream's tests could register two routes.
//
// modules/web imports this package, so it cannot be used here — and stdlib
// ServeMux has done method-qualified patterns since Go 1.22, which is all these
// tests ask for.
type testRouter struct {
mux *http.ServeMux
middleware []func(http.Handler) http.Handler
}
func newTestRouter() *testRouter { return &testRouter{mux: http.NewServeMux()} }
// Use adds a middleware, applied outermost-first around every route.
func (r *testRouter) Use(mw func(http.Handler) http.Handler) {
r.middleware = append(r.middleware, mw)
}
func (r *testRouter) Get(pattern string, h http.HandlerFunc) { r.mux.HandleFunc("GET "+pattern, h) }
func (r *testRouter) Post(pattern string, h http.HandlerFunc) { r.mux.HandleFunc("POST "+pattern, h) }
func (r *testRouter) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
h := http.Handler(r.mux)
for i := len(r.middleware) - 1; i >= 0; i-- {
h = r.middleware[i](h)
}
h.ServeHTTP(resp, req)
}