test(api/v2): use huma.ErrorModel for problem+json assertions

This commit is contained in:
kolaente
2026-05-04 13:54:07 +02:00
parent 32fd191f59
commit 3d5b3d63c6
2 changed files with 3 additions and 15 deletions

View File

@@ -22,6 +22,7 @@ import (
"strings"
"testing"
"github.com/danielgtaylor/huma/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -43,7 +44,7 @@ func TestHuma_ErrorShapeIsRFC9457(t *testing.T) {
ct := rec.Header().Get("Content-Type")
assert.Contains(t, ct, "application/problem+json", "forbidden response must use RFC 9457 content type; got %q", ct)
var body humaErrorBody
var body huma.ErrorModel
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &body), "body: %s", rec.Body.String())
assert.Equal(t, http.StatusForbidden, body.Status)
assert.NotEmpty(t, body.Title, "title is required by RFC 9457")
@@ -56,7 +57,7 @@ func TestHuma_ErrorShapeIsRFC9457(t *testing.T) {
ct := rec.Header().Get("Content-Type")
assert.Contains(t, ct, "application/problem+json", "validation response must use RFC 9457 content type; got %q", ct)
var body humaErrorBody
var body huma.ErrorModel
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &body), "body: %s", rec.Body.String())
assert.Equal(t, http.StatusUnprocessableEntity, body.Status)
require.NotEmpty(t, body.Errors, "validation errors must include structured per-field details")

View File

@@ -41,19 +41,6 @@ func labelTokenFor(t *testing.T, u *user.User) string {
return tok
}
// humaErrorBody is the RFC 9457 problem+json shape Huma emits by default.
// Fields are a subset — we only assert what's load-bearing for the tests.
type humaErrorBody struct {
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail"`
Errors []struct {
Message string `json:"message"`
Location string `json:"location"`
} `json:"errors"`
}
// TestHumaLabel mirrors the v1 webtest shape (see project_test.go's TestProject)
// so the v2 contract can be read side-by-side with the v1 coverage. The goal
// is to prove v2 is behaviourally compatible with v1 modulo the documented