Files
opencode2api/admin_test.go
T
Jason Xu ea29a62eca fix: harden gateway routing and protocol fidelity
Preserve request parameters, tool-call completion reasons and retry session
affinity. Bound upstream header waits, stop expired-context scans and add
opt-in redacted request-body diagnostics.

Isolate Playground diagnostics from production pools, report stream failures
accurately, align readiness with discoverable models and honor Docker listen
configuration.

Split Go and WebUI modules by responsibility, add regression tests and CI,
standardize formatting and rewrite the English and Chinese READMEs.

Refs: #6, #18, #19, #20, #21, #22, #23, #24, #25
2026-09-15 00:43:35 +08:00

46 lines
1.8 KiB
Go

package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestEmbeddedWebAssets(t *testing.T) {
manager, _ := newHandlerTestRuntime(t, handlerTestConfig(), nil)
admin := NewAdminServer(manager, manager.monitor, manager.hub, manager.logger)
for _, test := range []struct {
path string
contentType string
contains string
}{
{path: "/", contentType: "text/html", contains: `<script src="/app.js" defer></script>`},
{path: "/app.js", contentType: "javascript", contains: "async function boot()"},
{path: "/styles.css", contentType: "text/css", contains: "#account-form"},
} {
t.Run(test.path, func(t *testing.T) {
recorder := httptest.NewRecorder()
admin.Handler().ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, test.path, nil))
if recorder.Code != 200 || !strings.Contains(recorder.Header().Get("Content-Type"), test.contentType) || !strings.Contains(recorder.Body.String(), test.contains) {
t.Fatalf("asset %s: status=%d content-type=%s", test.path, recorder.Code, recorder.Header().Get("Content-Type"))
}
if policy := recorder.Header().Get("Content-Security-Policy"); strings.Contains(policy, "unsafe-inline") || !strings.Contains(policy, "script-src 'self'") {
t.Fatalf("unexpected asset policy: %s", policy)
}
})
}
}
func TestManagementEndpointsRequireSession(t *testing.T) {
manager, _ := newHandlerTestRuntime(t, handlerTestConfig(), nil)
admin := NewAdminServer(manager, manager.monitor, manager.hub, manager.logger)
for _, path := range []string{"/api/config", "/api/monitor", "/api/debug/models", "/api/logs", "/api/logs/stream"} {
recorder := httptest.NewRecorder()
admin.Handler().ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, path, nil))
if recorder.Code != http.StatusUnauthorized {
t.Errorf("%s: unauthenticated status = %d", path, recorder.Code)
}
}
}