Files
Ant-Browser/backend/internal/launchcode/server_auth_internal_test.go
T
Ant Browser Release Bot 3d264eb83d publish: 1.1.0 snapshot (a358335)
channel: master
version: 1.1.0
source-ref: master
published-at-utc: 2026-03-29T11:32:04Z
2026-03-29 19:32:05 +08:00

30 lines
806 B
Go

package launchcode
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestBuildHandlerRejectsNonLocalRequestBeforeAPIAuth(t *testing.T) {
srv := NewLaunchServer(NewLaunchCodeService(NewMemoryLaunchCodeDAO()), nil, nil, 0)
srv.SetAPIAuthConfig(APIAuthConfig{
Enabled: true,
APIKey: "secret-key",
Header: "X-Test-Api-Key",
})
req := httptest.NewRequest(http.MethodGet, "/api/health", nil)
req.RemoteAddr = "10.0.0.8:3456"
w := httptest.NewRecorder()
srv.buildHandler(true).ServeHTTP(w, req)
if w.Code != http.StatusForbidden {
t.Fatalf("非 localhost 请求应优先返回 403: got=%d body=%s", w.Code, w.Body.String())
}
if !strings.Contains(w.Body.String(), "forbidden: only localhost is allowed") {
t.Fatalf("错误信息不正确: %s", w.Body.String())
}
}