mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
routers/ and every test in it is green. That is the first real check on the filter
work, and running it found two defects and one habit worth naming.
THE HEADER CEILING WAS UNSET, and it is the twin of the body one. fasthttp's
per-connection read buffer is 4 KiB by default and that buffer caps the TOTAL
request headers, so a request whose headers do not fit is refused at the socket
before any handler — with a message naming a buffer size. Every authenticated
request here carries a signed IAM bearer and the gateway's identity set beside it,
which clears 4 KiB together. zip states the ceiling and names the seam in its own
comment; ai set BodyLimit and never set this. Found because a test signed a real
token and the request would not parse.
A FILTER IS MIDDLEWARE, so it is tested through an app. It ends in Continue(), and
Continue means "the next handler" — which a detached context does not have, so
calling one on a TestCtx panics inside fiber's Next. zip's own doc says TestCtx is
for calling ONE handler and a chain belongs to an app. probe.through does that, and
it puts each filter in the position it actually occupies, ahead of a handler.
A FIBER CTX IS POOLED. The harness first captured the Ctx the handler was reached
with and read it after the request; fiber releases its Ctx at the end, so that read
returns a recycled object with nothing of the request on it — and the tenant
attribution tests failed reporting an empty project, which reads exactly like the
filter never ran. Capture the VALUE, never the Ctx.
Two more honest corrections to the tests themselves:
- A "*" mapping claims every verb, and the live table lists them one at a time
because the router registers a handler per method. The doc-table check now
asserts the PATH is served for a "*" row, which is what "*" says.
- The probe never returns a silent zero on a failed run. It did, and a status of 0
reads as "the filter answered nothing" — which sent three assertions looking for
a bug in the filter that was in the harness.
And the tests that had nothing left to test are gone rather than adapted: the
auto-signin and static filter tests went with the filters, and the session
nil-safety test is recast onto the property that survived — no credential resolves
to nobody, and bills nobody, because the filters ask that before any controller.
68 lines
2.6 KiB
Go
68 lines
2.6 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// 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 ai
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/ai/controllers"
|
|
"github.com/zap-proto/zip"
|
|
)
|
|
|
|
// TestVideosGenerationsRouteIsRegistered proves POST /v1/videos/generations is
|
|
// wired to the real VideosGenerations controller through the unified binary's
|
|
// /v1/* mount — it must NOT 404 (route missing) and must NOT 500 (session
|
|
// panic). Without a Bearer token the handler's own auth rejects the request
|
|
// (401), which still proves the route resolved and the handler ran. This is the
|
|
// registration half of the wiring: before it, the path 404'd (catalog-ware
|
|
// model, no endpoint).
|
|
func TestVideosGenerationsRouteIsRegistered(t *testing.T) {
|
|
|
|
app := zip.New(zip.Config{DisableStartupMessage: true, ReadBufferSize: 32 << 10})
|
|
routes(app)
|
|
|
|
// No Authorization header → the handler's bearerToken() rejects with 401.
|
|
// A 404 would mean the route never registered; a 500 a session panic.
|
|
req, _ := http.NewRequest(http.MethodPost, "http://example.com/v1/videos/generations",
|
|
strings.NewReader(`{"model":"zen3-video","prompt":"a red fox in snow"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := app.Fiber().Test(req)
|
|
if err != nil {
|
|
t.Fatalf("Test: %v", err)
|
|
}
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
t.Fatalf("POST /v1/videos/generations: 404 — route not registered. body=%q", body)
|
|
}
|
|
if resp.StatusCode == http.StatusInternalServerError {
|
|
t.Fatalf("POST /v1/videos/generations: 500 (session panic). body=%q", body)
|
|
}
|
|
if bs := string(body); strings.Contains(bs, "nil pointer") {
|
|
t.Fatalf("a nil dereference reached the body: %q", bs)
|
|
}
|
|
// The handler ran and rejected the unauthenticated request.
|
|
if resp.StatusCode != http.StatusUnauthorized {
|
|
t.Logf("note: status=%d body=%q (expected 401 from unauthenticated handler)", resp.StatusCode, body)
|
|
}
|
|
}
|
|
|
|
// ensure the controllers package is linked (VideosGenerations method exists).
|
|
var _ = (&controllers.ApiController{}).VideosGenerations
|