Files
forge/modules/validation/binding_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

63 lines
1.5 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package validation
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/hanzoai/git/modules/binding"
"github.com/stretchr/testify/assert"
)
const (
testRoute = "/test"
)
type (
validationTestCase struct {
description string
data any
expectedErrors binding.Errors
}
TestForm struct {
BranchName string `form:"BranchName" binding:"GitRefName"`
URL string `form:"ValidUrl" binding:"ValidUrl"`
GlobPattern string `form:"GlobPattern" binding:"GlobPattern"`
RegexPattern string `form:"RegexPattern" binding:"RegexPattern"`
}
)
func performValidationTest(t *testing.T, testCase validationTestCase) {
httpRecorder := httptest.NewRecorder()
m := newTestRouter()
m.Post(testRoute, func(resp http.ResponseWriter, req *http.Request) {
actual := binding.Validate(req, testCase.data)
// see https://github.com/stretchr/testify/issues/435
if actual == nil {
actual = binding.Errors{}
}
assert.Equal(t, testCase.expectedErrors, actual)
})
req, err := http.NewRequest(http.MethodPost, testRoute, nil)
if err != nil {
panic(err)
}
req.Header.Add("Content-Type", "x-www-form-urlencoded")
m.ServeHTTP(httpRecorder, req)
switch httpRecorder.Code {
case http.StatusNotFound:
panic("Routing is messed up in test fixture (got 404): check methods and paths")
case http.StatusInternalServerError:
panic("Something bad happened on '" + testCase.description + "'")
}
}