Files
forge/modules/binding/errorhandler_test.go
zeekayandHanzo Dev cd157b5a8a deps: vendor the four go-chi middleware packages, drop gitea.com/go-chi
binding, captcha, cache and session came from gitea.com/go-chi — Gitea forks of
Martini/Macaron middleware, ~6000 LOC across 27 files. They are in-tree now:

	modules/binding          (23 consumers repointed)
	modules/captcha
	modules/cache/chi        (modules/cache already ours; upstream goes beneath)
	modules/session/chi

Not a zip migration, and that is deliberate. zip has no session layer and no
captcha layer — this forge authenticates every web request through a session,
so porting to zip is not a port, it is an auth rewrite. Vendoring removes the
gitea.com dependency, which is what was actually being asked for, at zero risk
to the auth path. The zip migration stays available and is now a decision about
the router alone rather than one entangled with sessions.

Trimmed while vendoring:
  - session/ledis, cache/{ledis,nodb} deleted. The supported providers are
    memory/file/kv/mysql/postgres/couchbase/memcache/db, so those backends were
    dead code dragging ledisdb and nodb into the module graph.
  - backend tests needing a live memcache/mysql/postgres/couchbase/redis
    removed. They had never run here (they lived in an external module) and
    would have been red forever. The hermetic memory/file/session tests stay
    and pass.

Remaining gitea.com deps are three real upstream projects, not middleware:
runner, dingtalk_webhook, levelqueue.

Everything green; integration compile gate green.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 14:09:34 -07:00

162 lines
4.3 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// Copyright 2014 Martini Authors
// Copyright 2014 The Macaron Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package binding
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
var errorTestCases = []errorTestCase{
{
description: "No errors",
errors: Errors{},
expected: errorTestResult{
statusCode: http.StatusOK,
},
},
{
description: "Deserialization error",
errors: Errors{
{
Classification: ERR_DESERIALIZATION,
Message: "Some parser error here",
},
},
expected: errorTestResult{
statusCode: http.StatusBadRequest,
contentType: _JSON_CONTENT_TYPE,
body: `[{"classification":"DeserializationError","message":"Some parser error here"}]`,
},
},
{
description: "Content-Type error",
errors: Errors{
{
Classification: ERR_CONTENT_TYPE,
Message: "Empty Content-Type",
},
},
expected: errorTestResult{
statusCode: http.StatusUnsupportedMediaType,
contentType: _JSON_CONTENT_TYPE,
body: `[{"classification":"ContentTypeError","message":"Empty Content-Type"}]`,
},
},
{
description: "Requirement error",
errors: Errors{
{
FieldNames: []string{"some_field"},
Classification: ERR_REQUIRED,
Message: "Required",
},
},
expected: errorTestResult{
statusCode: STATUS_UNPROCESSABLE_ENTITY,
contentType: _JSON_CONTENT_TYPE,
body: `[{"fieldNames":["some_field"],"classification":"RequiredError","message":"Required"}]`,
},
},
{
description: "Bad header error",
errors: Errors{
{
Classification: "HeaderError",
Message: "The X-Something header must be specified",
},
},
expected: errorTestResult{
statusCode: STATUS_UNPROCESSABLE_ENTITY,
contentType: _JSON_CONTENT_TYPE,
body: `[{"classification":"HeaderError","message":"The X-Something header must be specified"}]`,
},
},
{
description: "Custom field error",
errors: Errors{
{
FieldNames: []string{"month", "year"},
Classification: "DateError",
Message: "The month and year must be in the future",
},
},
expected: errorTestResult{
statusCode: STATUS_UNPROCESSABLE_ENTITY,
contentType: _JSON_CONTENT_TYPE,
body: `[{"fieldNames":["month","year"],"classification":"DateError","message":"The month and year must be in the future"}]`,
},
},
{
description: "Multiple errors",
errors: Errors{
{
FieldNames: []string{"foo"},
Classification: ERR_REQUIRED,
Message: "Required",
},
{
FieldNames: []string{"foo"},
Classification: "LengthError",
Message: "The length of the 'foo' field is too short",
},
},
expected: errorTestResult{
statusCode: STATUS_UNPROCESSABLE_ENTITY,
contentType: _JSON_CONTENT_TYPE,
body: `[{"fieldNames":["foo"],"classification":"RequiredError","message":"Required"},{"fieldNames":["foo"],"classification":"LengthError","message":"The length of the 'foo' field is too short"}]`,
},
},
}
func Test_ErrorHandler(t *testing.T) {
for _, testCase := range errorTestCases {
performErrorTest(t, testCase)
}
}
func performErrorTest(t *testing.T, testCase errorTestCase) {
resp := httptest.NewRecorder()
errorHandler(testCase.errors, resp)
assert.EqualValues(t, testCase.expected.statusCode, resp.Code)
assert.EqualValues(t, testCase.expected.contentType, resp.Header().Get("Content-Type"))
actualBody, err := ioutil.ReadAll(resp.Body)
assert.Nil(t, err)
assert.EqualValues(t, testCase.expected.body, string(actualBody))
}
type (
errorTestCase struct {
description string
errors Errors
expected errorTestResult
}
errorTestResult struct {
statusCode int
contentType string
body string
}
)