package api import ( "context" "encoding/json" "errors" "io" "net/http" "net/http/httptest" "os" "path/filepath" "reflect" "strings" "sync" "sync/atomic" "testing" "time" gin "github.com/gin-gonic/gin" managementHandlers "github.com/router-for-me/CLIProxyAPI/v7/internal/api/handlers/management" claudemodels "github.com/router-for-me/CLIProxyAPI/v7/internal/client/claude/models" codexmodels "github.com/router-for-me/CLIProxyAPI/v7/internal/client/codex/models" proxyconfig "github.com/router-for-me/CLIProxyAPI/v7/internal/config" "github.com/router-for-me/CLIProxyAPI/v7/internal/home" internallogging "github.com/router-for-me/CLIProxyAPI/v7/internal/logging" "github.com/router-for-me/CLIProxyAPI/v7/internal/pluginhost" "github.com/router-for-me/CLIProxyAPI/v7/internal/redisqueue" "github.com/router-for-me/CLIProxyAPI/v7/internal/registry" "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor" runtimehelps "github.com/router-for-me/CLIProxyAPI/v7/internal/runtime/executor/helps" sdkaccess "github.com/router-for-me/CLIProxyAPI/v7/sdk/access" "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executionregistry" coreexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" coreusage "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/usage" sdkconfig "github.com/router-for-me/CLIProxyAPI/v7/sdk/config" "github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi" log "github.com/sirupsen/logrus" logtest "github.com/sirupsen/logrus/hooks/test" "gopkg.in/yaml.v3" ) type codexSearchCaptureExecutor struct { request *http.Request body []byte authIDs []string prepareErr error httpErr error responseBody io.ReadCloser statuses []int refreshCalls int httpCalls int beforeReturn func() } func (e *codexSearchCaptureExecutor) Identifier() string { return "codex" } func (e *codexSearchCaptureExecutor) Execute(context.Context, *auth.Auth, coreexecutor.Request, coreexecutor.Options) (coreexecutor.Response, error) { return coreexecutor.Response{}, nil } func (e *codexSearchCaptureExecutor) ExecuteStream(context.Context, *auth.Auth, coreexecutor.Request, coreexecutor.Options) (*coreexecutor.StreamResult, error) { return nil, nil } func (e *codexSearchCaptureExecutor) Refresh(_ context.Context, a *auth.Auth) (*auth.Auth, error) { e.refreshCalls++ updated := a.Clone() if updated.Metadata == nil { updated.Metadata = make(map[string]any) } updated.Metadata["access_token"] = "refreshed-home-search-token" return updated, nil } func (e *codexSearchCaptureExecutor) CountTokens(context.Context, *auth.Auth, coreexecutor.Request, coreexecutor.Options) (coreexecutor.Response, error) { return coreexecutor.Response{}, nil } func (e *codexSearchCaptureExecutor) PrepareRequest(req *http.Request, a *auth.Auth) error { if e.prepareErr != nil { return e.prepareErr } token, _ := a.Metadata["access_token"].(string) if strings.TrimSpace(token) == "" && a.Attributes != nil { token = a.Attributes[auth.AttributeAPIKey] } if strings.TrimSpace(token) != "" { req.Header.Set("Authorization", "Bearer "+token) } return nil } type codexSearchGinContextSelector struct { ginContext *gin.Context } func (s *codexSearchGinContextSelector) Pick(ctx context.Context, _ string, _ string, _ coreexecutor.Options, auths []*auth.Auth) (*auth.Auth, error) { s.ginContext, _ = ctx.Value("gin").(*gin.Context) if len(auths) == 0 { return nil, nil } return auths[0], nil } type codexSearchAPIKeyFirstSelector struct{} type codexSearchModelRouter struct { response pluginapi.ModelRouteResponse handled bool requests []pluginapi.ModelRouteRequest } func (r *codexSearchModelRouter) RouteModel(_ context.Context, req pluginapi.ModelRouteRequest) (pluginapi.ModelRouteResponse, bool) { r.requests = append(r.requests, req) return r.response, r.handled } func (s *codexSearchAPIKeyFirstSelector) Pick(_ context.Context, _ string, _ string, _ coreexecutor.Options, auths []*auth.Auth) (*auth.Auth, error) { for _, candidate := range auths { if candidate.AuthKind() == auth.AuthKindAPIKey { return candidate, nil } } if len(auths) == 0 { return nil, nil } return auths[0], nil } func (e *codexSearchCaptureExecutor) HttpRequest(_ context.Context, selected *auth.Auth, req *http.Request) (*http.Response, error) { if e.httpErr != nil { return nil, e.httpErr } e.request = req.Clone(req.Context()) e.authIDs = append(e.authIDs, selected.ID) e.httpCalls++ body, err := io.ReadAll(req.Body) if err != nil { return nil, err } e.body = body responseBody := e.responseBody if responseBody == nil { responseBody = io.NopCloser(strings.NewReader(`{"results":[{"url":"https://example.com"}]}`)) } statusCode := http.StatusOK if e.httpCalls <= len(e.statuses) && e.statuses[e.httpCalls-1] > 0 { statusCode = e.statuses[e.httpCalls-1] } if e.beforeReturn != nil { e.beforeReturn() } return &http.Response{ StatusCode: statusCode, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: responseBody, }, nil } type codexSearchHomeDispatcher struct { authID string calls atomic.Int32 policy atomic.Value } type homeUnauthorizedUsageCapture struct { authID string records chan coreusage.Record } func (p *homeUnauthorizedUsageCapture) HandleUsage(_ context.Context, record coreusage.Record) { if p == nil || record.ExecutorType != "home-result" || record.AuthID != p.authID { return } select { case p.records <- record: default: } } func (p *homeUnauthorizedUsageCapture) wait(t *testing.T) coreusage.Record { t.Helper() select { case record := <-p.records: return record case <-time.After(time.Second): t.Fatal("timed out waiting for Home unauthorized usage record") return coreusage.Record{} } } type noopHomeUnauthorizedUsagePlugin struct{} func (noopHomeUnauthorizedUsagePlugin) HandleUsage(context.Context, coreusage.Record) {} func registerHomeUnauthorizedUsageCapture(t *testing.T, name, authID string) *homeUnauthorizedUsageCapture { t.Helper() capture := &homeUnauthorizedUsageCapture{authID: authID, records: make(chan coreusage.Record, 1)} coreusage.RegisterNamedPlugin(name, capture) t.Cleanup(func() { coreusage.RegisterNamedPlugin(name, noopHomeUnauthorizedUsagePlugin{}) }) return capture } func (*codexSearchHomeDispatcher) HeartbeatOK() bool { return true } func (d *codexSearchHomeDispatcher) RPopAuth(_ context.Context, model string, _ string, _ http.Header, _ int) ([]byte, error) { d.calls.Add(1) authID := d.authID if authID == "" { authID = "home-codex-search" } return json.Marshal(map[string]any{ "model": model, "auth_index": authID, "auth": map[string]any{ "id": authID, "provider": "codex", "status": "active", "metadata": map[string]any{"access_token": "home-search-token"}, }, "concurrency": map[string]any{ "accounted": true, "credential_id": authID, "model": model, }, }) } func (d *codexSearchHomeDispatcher) RPopAuthWithPolicy(ctx context.Context, model string, sessionID string, headers http.Header, count int, policy string) ([]byte, error) { d.policy.Store(policy) return d.RPopAuth(ctx, model, sessionID, headers, count) } func (*codexSearchHomeDispatcher) AbortAmbiguousDispatch() {} type codexSearchBusyHomeDispatcher struct{} func (*codexSearchBusyHomeDispatcher) HeartbeatOK() bool { return true } func (*codexSearchBusyHomeDispatcher) RPopAuth(context.Context, string, string, http.Header, int) ([]byte, error) { return []byte(`{"error":{"type":"credential_concurrency_exceeded","message":"busy","retry_after_ms":750}}`), nil } func (d *codexSearchBusyHomeDispatcher) RPopAuthWithPolicy(ctx context.Context, model string, sessionID string, headers http.Header, count int, _ string) ([]byte, error) { return d.RPopAuth(ctx, model, sessionID, headers, count) } func (*codexSearchBusyHomeDispatcher) AbortAmbiguousDispatch() {} type trackedSearchResponseBody struct { io.Reader closed atomic.Bool } func (b *trackedSearchResponseBody) Close() error { b.closed.Store(true) return nil } type errorSearchResponseBody struct { payload []byte read atomic.Bool closed atomic.Bool } func (b *errorSearchResponseBody) Read(p []byte) (int, error) { if !b.read.CompareAndSwap(false, true) { return 0, io.EOF } return copy(p, b.payload), io.ErrUnexpectedEOF } func (b *errorSearchResponseBody) Close() error { b.closed.Store(true) return nil } type drainAwareSearchResponseBody struct { started chan struct{} closed chan struct{} startOnce sync.Once closeOnce sync.Once } func newDrainAwareSearchResponseBody() *drainAwareSearchResponseBody { return &drainAwareSearchResponseBody{started: make(chan struct{}), closed: make(chan struct{})} } func (b *drainAwareSearchResponseBody) Read([]byte) (int, error) { b.startOnce.Do(func() { close(b.started) }) <-b.closed return 0, io.EOF } func (b *drainAwareSearchResponseBody) Close() error { b.closeOnce.Do(func() { close(b.closed) }) return nil } func TestAuditHomeBusyNormalAndStream429Headers(t *testing.T) { for _, stream := range []bool{false, true} { t.Run(map[bool]string{false: "normal", true: "stream"}[stream], func(t *testing.T) { server := newTestServer(t) server.handlers.AuthManager.SetConfig(&proxyconfig.Config{Home: proxyconfig.HomeConfig{Enabled: true}}) server.handlers.AuthManager.PublishHomeDispatch(&codexSearchBusyHomeDispatcher{}, executionregistry.New(), 1) body := `{"model":"gpt-5-codex","input":[]}` if stream { body = `{"model":"gpt-5-codex","input":[],"stream":true}` } rr := httptest.NewRecorder() req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(body)) req.Header.Set("Authorization", "Bearer test-key") server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusTooManyRequests { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusTooManyRequests, rr.Body.String()) } if got := rr.Header().Get("Retry-After"); got != "1" { t.Fatalf("Retry-After = %q, want 1", got) } }) } } func TestAuditHomeCodexSearchBusyReturnsTrustedRetryAfter(t *testing.T) { server := newTestServer(t) server.handlers.AuthManager.SetConfig(&proxyconfig.Config{Home: proxyconfig.HomeConfig{Enabled: true}}) server.handlers.AuthManager.PublishHomeDispatch(&codexSearchBusyHomeDispatcher{}, executionregistry.New(), 1) rr := httptest.NewRecorder() req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(`{"model":"gpt-5-codex","query":"test"}`)) req.Header.Set("Authorization", "Bearer test-key") server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusTooManyRequests { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusTooManyRequests, rr.Body.String()) } if got := rr.Header().Get("Retry-After"); got != "1" { t.Fatalf("Retry-After = %q, want 1", got) } if !strings.Contains(rr.Body.String(), "busy") { t.Fatalf("body = %q, want busy error", rr.Body.String()) } } func TestAuditHomeCodexSearchBodyCloseBeforeRelease(t *testing.T) { server := newTestServer(t) dispatcher := &codexSearchHomeDispatcher{} registry := executionregistry.New() body := newDrainAwareSearchResponseBody() var releaseAfterBodyClose atomic.Bool var releaseCount atomic.Int32 registry.SetReleaseSink(func(group executionregistry.ReleaseGroup, _ int64) { if group != (executionregistry.ReleaseGroup{CredentialID: "home-codex-search", Model: "gpt-5-codex"}) { t.Errorf("release group = %#v", group) } select { case <-body.closed: releaseAfterBodyClose.Store(true) default: } releaseCount.Add(1) }) server.handlers.AuthManager.SetConfig(&proxyconfig.Config{Home: proxyconfig.HomeConfig{Enabled: true}}) server.handlers.AuthManager.PublishHomeDispatch(dispatcher, registry, 1) executor := &codexSearchCaptureExecutor{responseBody: body} server.handlers.AuthManager.RegisterExecutor(executor) rr := httptest.NewRecorder() req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(`{"id":"home-search-drain","model":"gpt-5-codex","query":"test"}`)) req.Header.Set("Authorization", "Bearer test-key") handlerDone := make(chan struct{}) go func() { server.engine.ServeHTTP(rr, req) close(handlerDone) }() select { case <-body.started: case <-time.After(time.Second): t.Fatal("search handler did not start reading the response body") } if errDrain := registry.Drain(context.Background()); errDrain != nil { t.Fatalf("Drain() error = %v", errDrain) } if got := releaseCount.Load(); got != 1 { t.Fatalf("accounted releases = %d, want 1", got) } if !releaseAfterBodyClose.Load() { t.Fatal("accounted Home selection released before the search response body closed") } select { case <-handlerDone: case <-time.After(time.Second): t.Fatal("search handler remained blocked after Home drain") } if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) } } func TestHomeCodexAlphaSearchForwardsUnauthorizedResponseWithoutRefresh(t *testing.T) { const upstreamError = `{"error":{"message":"access token expired"}}` server := newTestServer(t) server.cfg.RequestLog = true dispatcher := &codexSearchHomeDispatcher{} server.handlers.AuthManager.SetConfig(&proxyconfig.Config{Home: proxyconfig.HomeConfig{Enabled: true}}) server.handlers.AuthManager.PublishHomeDispatch(dispatcher, executionregistry.New(), 1) executor := &codexSearchCaptureExecutor{ statuses: []int{http.StatusUnauthorized}, responseBody: io.NopCloser(strings.NewReader(upstreamError)), } server.handlers.AuthManager.RegisterExecutor(executor) req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(`{"id":"home-search-refresh","model":"gpt-5-codex","query":"test"}`)) req.Header.Set("Authorization", "Bearer test-key") rr := httptest.NewRecorder() c, _ := gin.CreateTestContext(rr) c.Request = req server.codexAlphaSearch(c) if rr.Code != http.StatusUnauthorized { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusUnauthorized, rr.Body.String()) } if got := rr.Body.String(); got != upstreamError { t.Fatalf("body = %q, want original upstream error %q", got, upstreamError) } if executor.refreshCalls != 0 || executor.httpCalls != 1 { t.Fatalf("refresh/http calls = %d/%d, want 0/1", executor.refreshCalls, executor.httpCalls) } if got := executor.request.Header.Get("Authorization"); got != "Bearer home-search-token" { t.Fatalf("Authorization = %q, want original Home token", got) } if got := dispatcher.calls.Load(); got != 1 { t.Fatalf("Home RPOP calls = %d, want 1", got) } rawAPIResponse, okResponse := c.Get("API_RESPONSE") if !okResponse { t.Fatal("API_RESPONSE was not captured") } apiResponse, _ := rawAPIResponse.([]byte) if !strings.Contains(string(apiResponse), "Status: 401") || !strings.Contains(string(apiResponse), upstreamError) { t.Fatalf("API_RESPONSE = %q, want original upstream 401", apiResponse) } } func TestHomeCodexAlphaSearchReportsUnauthorizedBeforeEarlyReturn(t *testing.T) { const upstreamError = `{"error":{"message":"access token expired"}}` tests := []struct { name string responseBody func() io.ReadCloser beforeReturn func(*executionregistry.Registry) wantStatus int wantFailBody string }{ { name: "response bind failure", responseBody: func() io.ReadCloser { return &trackedSearchResponseBody{Reader: strings.NewReader(upstreamError)} }, beforeReturn: func(registry *executionregistry.Registry) { _ = registry.Close() }, wantStatus: http.StatusServiceUnavailable, wantFailBody: "upstream unauthorized", }, { name: "response read failure", responseBody: func() io.ReadCloser { return &errorSearchResponseBody{payload: []byte(upstreamError)} }, wantStatus: http.StatusBadGateway, wantFailBody: upstreamError, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { server := newTestServer(t) registry := executionregistry.New() server.handlers.AuthManager.SetConfig(&proxyconfig.Config{Home: proxyconfig.HomeConfig{Enabled: true}}) testAuthID := "home-codex-search-" + strings.ReplaceAll(test.name, " ", "-") server.handlers.AuthManager.PublishHomeDispatch(&codexSearchHomeDispatcher{authID: testAuthID}, registry, 1) executor := &codexSearchCaptureExecutor{ statuses: []int{http.StatusUnauthorized}, responseBody: test.responseBody(), } if test.beforeReturn != nil { executor.beforeReturn = func() { test.beforeReturn(registry) } } server.handlers.AuthManager.RegisterExecutor(executor) usageCapture := registerHomeUnauthorizedUsageCapture(t, t.Name(), testAuthID) recorder := httptest.NewRecorder() request := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(`{"model":"gpt-5-codex","query":"test"}`)) request.Header.Set("Authorization", "Bearer test-key") server.engine.ServeHTTP(recorder, request) if recorder.Code != test.wantStatus { t.Fatalf("status = %d, want %d; body=%s", recorder.Code, test.wantStatus, recorder.Body.String()) } record := usageCapture.wait(t) if record.Fail.StatusCode != http.StatusUnauthorized || record.Fail.Body != test.wantFailBody { t.Fatalf("Home unauthorized failure = status %d body %q, want status 401 body %q", record.Fail.StatusCode, record.Fail.Body, test.wantFailBody) } }) } } func TestHomeCodexAlphaSearchRequestLogPreservesBodyReturnedWithReadError(t *testing.T) { const upstreamError = `{"error":{"message":"access token expired"}}` server := newTestServer(t) server.cfg.RequestLog = true server.handlers.AuthManager.SetConfig(&proxyconfig.Config{Home: proxyconfig.HomeConfig{Enabled: true}}) server.handlers.AuthManager.PublishHomeDispatch(&codexSearchHomeDispatcher{}, executionregistry.New(), 1) server.handlers.AuthManager.RegisterExecutor(&codexSearchCaptureExecutor{ statuses: []int{http.StatusUnauthorized}, responseBody: &errorSearchResponseBody{payload: []byte(upstreamError)}, }) recorder := httptest.NewRecorder() c, _ := gin.CreateTestContext(recorder) c.Request = httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(`{"model":"gpt-5-codex","query":"test"}`)) server.codexAlphaSearch(c) if recorder.Code != http.StatusBadGateway { t.Fatalf("status = %d, want %d; body=%s", recorder.Code, http.StatusBadGateway, recorder.Body.String()) } rawAPIResponse, okResponse := c.Get("API_RESPONSE") apiResponse, _ := rawAPIResponse.([]byte) if !okResponse || !strings.Contains(string(apiResponse), upstreamError) || !strings.Contains(string(apiResponse), io.ErrUnexpectedEOF.Error()) { t.Fatalf("API_RESPONSE = %q, want upstream body and read error", apiResponse) } } func TestHomeCodexAlphaSearchEndsSelectionAcrossDirectHTTPPaths(t *testing.T) { tests := []struct { name string configure func(*codexSearchCaptureExecutor, *trackedSearchResponseBody) wantStatus int wantClosed bool }{ { name: "request build failure", configure: func(executor *codexSearchCaptureExecutor, _ *trackedSearchResponseBody) { executor.prepareErr = errors.New("request preparation failed") }, wantStatus: http.StatusBadGateway, }, { name: "HTTP error", configure: func(executor *codexSearchCaptureExecutor, _ *trackedSearchResponseBody) { executor.httpErr = errors.New("upstream unavailable") }, wantStatus: http.StatusBadGateway, }, { name: "response body close", configure: func(executor *codexSearchCaptureExecutor, body *trackedSearchResponseBody) { executor.responseBody = body }, wantStatus: http.StatusOK, wantClosed: true, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { server := newTestServer(t) dispatcher := &codexSearchHomeDispatcher{} registry := executionregistry.New() server.handlers.AuthManager.SetConfig(&proxyconfig.Config{Home: proxyconfig.HomeConfig{Enabled: true}}) server.handlers.AuthManager.PublishHomeDispatch(dispatcher, registry, 1) body := &trackedSearchResponseBody{Reader: strings.NewReader(`{"results":[]}`)} executor := &codexSearchCaptureExecutor{} test.configure(executor, body) server.handlers.AuthManager.RegisterExecutor(executor) req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(`{"id":"home-search-session","model":"gpt-5-codex","query":"test"}`)) req.Header.Set("Authorization", "Bearer test-key") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != test.wantStatus { t.Fatalf("status = %d, want %d; body=%s", rr.Code, test.wantStatus, rr.Body.String()) } if got := dispatcher.calls.Load(); got != 1 { t.Fatalf("Home RPOP calls = %d, want 1", got) } if got, _ := dispatcher.policy.Load().(string); got != auth.CredentialPolicyCodexAlphaSearchV1 { t.Fatalf("Home credential policy = %q, want %q", got, auth.CredentialPolicyCodexAlphaSearchV1) } if got := body.closed.Load(); got != test.wantClosed { t.Fatalf("response body closed = %t, want %t", got, test.wantClosed) } if errDrain := registry.Drain(context.Background()); errDrain != nil { t.Fatalf("Drain() error = %v", errDrain) } }) } } func newTestServer(t *testing.T) *Server { t.Helper() return newTestServerWithOptions(t) } func newTestServerWithOptions(t *testing.T, opts ...ServerOption) *Server { t.Helper() gin.SetMode(gin.TestMode) tmpDir := t.TempDir() authDir := filepath.Join(tmpDir, "auth") if err := os.MkdirAll(authDir, 0o700); err != nil { t.Fatalf("failed to create auth dir: %v", err) } cfg := &proxyconfig.Config{ SDKConfig: sdkconfig.SDKConfig{ APIKeys: []string{"test-key"}, }, Port: 0, AuthDir: authDir, Debug: true, LoggingToFile: false, UsageStatisticsEnabled: false, } authManager := auth.NewManager(nil, nil, nil) accessManager := sdkaccess.NewManager() configPath := filepath.Join(tmpDir, "config.yaml") return NewServer(cfg, authManager, accessManager, configPath, opts...) } func TestHealthz(t *testing.T) { server := newTestServer(t) t.Run("GET", func(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/healthz", nil) rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("unexpected status code: got %d want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) } var resp struct { Status string `json:"status"` } if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil { t.Fatalf("failed to parse response JSON: %v; body=%s", err, rr.Body.String()) } if resp.Status != "ok" { t.Fatalf("unexpected response status: got %q want %q", resp.Status, "ok") } }) t.Run("HEAD", func(t *testing.T) { req := httptest.NewRequest(http.MethodHead, "/healthz", nil) rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("unexpected status code: got %d want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) } if rr.Body.Len() != 0 { t.Fatalf("expected empty body for HEAD request, got %q", rr.Body.String()) } }) } func TestHealthzAccessLogging(t *testing.T) { server := newTestServer(t) previousHome := home.Current() home.ClearCurrent() t.Cleanup(func() { home.SetCurrent(previousHome) }) logger := log.StandardLogger() previousHooks := logger.ReplaceHooks(make(log.LevelHooks)) previousLevel := logger.GetLevel() hook := logtest.NewLocal(logger) logger.SetLevel(log.InfoLevel) t.Cleanup(func() { logger.ReplaceHooks(previousHooks); logger.SetLevel(previousLevel) }) for _, tc := range []struct { name string homeEnabled bool status int }{ {"healthy", false, http.StatusOK}, {"home_unavailable", true, http.StatusServiceUnavailable}, } { t.Run(tc.name, func(t *testing.T) { server.cfg.Home.Enabled = tc.homeEnabled for _, method := range []string{http.MethodGet, http.MethodHead} { t.Run(method, func(t *testing.T) { hook.Reset() recorder := httptest.NewRecorder() server.engine.ServeHTTP(recorder, httptest.NewRequest(method, "/healthz", nil)) if recorder.Code != tc.status { t.Fatalf("status = %d, want %d", recorder.Code, tc.status) } count := 0 for _, entry := range hook.AllEntries() { if _, ok := entry.Data["request_id"]; ok && strings.Contains(entry.Message, `"/healthz"`) { count++ if tc.homeEnabled && entry.Level != log.ErrorLevel { t.Errorf("failed probe log level = %v, want error", entry.Level) } } } wantCount := 0 if tc.homeEnabled { wantCount = 1 } if count != wantCount { t.Errorf("probe access logs = %d, want %d", count, wantCount) } hook.Reset() server.engine.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/healthz-access-log-control", nil)) for _, entry := range hook.AllEntries() { if _, ok := entry.Data["request_id"]; ok && strings.Contains(entry.Message, `"/healthz-access-log-control"`) { return } } t.Error("ordinary request did not emit an access log after health probe") }) } }) } } func TestCodexLiveRoutesRequireAuthAndAreRegistered(t *testing.T) { server := newTestServer(t) for _, path := range []string{"/v1/live", "/v1/realtime/calls"} { unauthorized := httptest.NewRequest(http.MethodPost, path, nil) unauthorizedRecorder := httptest.NewRecorder() server.engine.ServeHTTP(unauthorizedRecorder, unauthorized) if unauthorizedRecorder.Code != http.StatusUnauthorized { t.Fatalf("%s unauthorized status = %d, want %d", path, unauthorizedRecorder.Code, http.StatusUnauthorized) } authorized := httptest.NewRequest(http.MethodPost, path, nil) authorized.Header.Set("Authorization", "Bearer test-key") authorizedRecorder := httptest.NewRecorder() server.engine.ServeHTTP(authorizedRecorder, authorized) if authorizedRecorder.Code != http.StatusServiceUnavailable { t.Fatalf("%s authorized status = %d, want %d; body=%s", path, authorizedRecorder.Code, http.StatusServiceUnavailable, authorizedRecorder.Body.String()) } } for _, path := range []string{"/v1/live/call-123", "/v1/realtime/calls/call-123", "/v1/realtime?call_id=call-123"} { unauthorized := httptest.NewRequest(http.MethodGet, path, nil) unauthorized.Header.Set("Upgrade", "websocket") unauthorized.Header.Set("Connection", "Upgrade") unauthorizedRecorder := httptest.NewRecorder() server.engine.ServeHTTP(unauthorizedRecorder, unauthorized) if unauthorizedRecorder.Code != http.StatusUnauthorized { t.Fatalf("%s unauthorized status = %d, want %d", path, unauthorizedRecorder.Code, http.StatusUnauthorized) } authorized := httptest.NewRequest(http.MethodGet, path, nil) authorized.Header.Set("Authorization", "Bearer test-key") authorizedRecorder := httptest.NewRecorder() server.engine.ServeHTTP(authorizedRecorder, authorized) if authorizedRecorder.Code != http.StatusUpgradeRequired { t.Fatalf("%s authorized status = %d, want %d; body=%s", path, authorizedRecorder.Code, http.StatusUpgradeRequired, authorizedRecorder.Body.String()) } } } func TestRealtimeStandardRoutesAndClientSecretAuth(t *testing.T) { server := newTestServer(t) unauthorizedSecret := httptest.NewRequest(http.MethodPost, "/v1/realtime/client_secrets", strings.NewReader(`{"session":{"type":"realtime","model":"gpt-realtime"}}`)) unauthorizedSecretRecorder := httptest.NewRecorder() server.engine.ServeHTTP(unauthorizedSecretRecorder, unauthorizedSecret) if unauthorizedSecretRecorder.Code != http.StatusUnauthorized { t.Fatalf("client_secrets unauthorized status = %d, want %d", unauthorizedSecretRecorder.Code, http.StatusUnauthorized) } var unauthorizedResponse struct { Error struct { Type string `json:"type"` Code string `json:"code"` } `json:"error"` } if errUnmarshal := json.Unmarshal(unauthorizedSecretRecorder.Body.Bytes(), &unauthorizedResponse); errUnmarshal != nil { t.Fatalf("unmarshal unauthorized response: %v", errUnmarshal) } if unauthorizedResponse.Error.Type != "authentication_error" || unauthorizedResponse.Error.Code != "invalid_api_key" { t.Fatalf("unauthorized error = %+v", unauthorizedResponse.Error) } secretRequest := httptest.NewRequest(http.MethodPost, "/v1/realtime/client_secrets", strings.NewReader(`{"session":{"type":"realtime","model":"gpt-realtime"}}`)) secretRequest.Header.Set("Authorization", "Bearer test-key") secretRecorder := httptest.NewRecorder() server.engine.ServeHTTP(secretRecorder, secretRequest) if secretRecorder.Code != http.StatusOK { t.Fatalf("client_secrets status = %d, want %d; body=%s", secretRecorder.Code, http.StatusOK, secretRecorder.Body.String()) } var secretResponse struct { Value string `json:"value"` } if errUnmarshal := json.Unmarshal(secretRecorder.Body.Bytes(), &secretResponse); errUnmarshal != nil { t.Fatalf("unmarshal client secret: %v", errUnmarshal) } if secretResponse.Value == "" { t.Fatal("client secret is empty") } callRequest := httptest.NewRequest(http.MethodPost, "/v1/realtime/calls", strings.NewReader("v=0\r\n")) callRequest.Header.Set("Authorization", "Bearer "+secretResponse.Value) callRequest.Header.Set("Content-Type", "application/sdp") callRecorder := httptest.NewRecorder() server.engine.ServeHTTP(callRecorder, callRequest) if callRecorder.Code != http.StatusServiceUnavailable { t.Fatalf("ephemeral call status = %d, want %d; body=%s", callRecorder.Code, http.StatusServiceUnavailable, callRecorder.Body.String()) } for _, testCase := range []struct { method string path string status int }{ {method: http.MethodGet, path: "/v1/realtime?model=gpt-realtime", status: http.StatusUpgradeRequired}, {method: http.MethodPost, path: "/v1/realtime", status: http.StatusServiceUnavailable}, {method: http.MethodPost, path: "/v1/realtime/sessions", status: http.StatusOK}, {method: http.MethodPost, path: "/v1/realtime/transcription_sessions", status: http.StatusNotImplemented}, {method: http.MethodGet, path: "/v1/realtime/translations", status: http.StatusNotImplemented}, {method: http.MethodPost, path: "/v1/realtime/translations", status: http.StatusNotImplemented}, {method: http.MethodPost, path: "/v1/realtime/translations/client_secrets", status: http.StatusNotImplemented}, {method: http.MethodPost, path: "/v1/realtime/calls/call-123/accept", status: http.StatusNotImplemented}, {method: http.MethodPost, path: "/v1/realtime/calls/call-123/reject", status: http.StatusNotImplemented}, {method: http.MethodPost, path: "/v1/realtime/calls/call-123/refer", status: http.StatusNotImplemented}, {method: http.MethodPost, path: "/v1/realtime/calls/call-123/hangup", status: http.StatusNotFound}, } { request := httptest.NewRequest(testCase.method, testCase.path, nil) request.Header.Set("Authorization", "Bearer test-key") recorder := httptest.NewRecorder() server.engine.ServeHTTP(recorder, request) if recorder.Code != testCase.status { t.Errorf("%s %s status = %d, want %d; body=%s", testCase.method, testCase.path, recorder.Code, testCase.status, recorder.Body.String()) } if testCase.method == http.MethodGet && testCase.path == "/v1/realtime?model=gpt-realtime" && recorder.Header().Get("Upgrade") != "websocket" { t.Errorf("Upgrade header = %q, want websocket", recorder.Header().Get("Upgrade")) } } } func TestCodexAlphaSearchForwardsRequest(t *testing.T) { server := newTestServer(t) executor := &codexSearchCaptureExecutor{} server.handlers.AuthManager.RegisterExecutor(executor) credential := &auth.Auth{ ID: "codex-auth", Provider: "codex", Status: auth.StatusActive, Metadata: map[string]any{"access_token": "codex-token", "account_id": "account-123"}, } if _, err := server.handlers.AuthManager.Register(context.Background(), credential); err != nil { t.Fatalf("register Codex auth: %v", err) } req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(`{"query":"GPT-5.6"}`)) req.Header.Set("Authorization", "Bearer test-key") req.Header.Set("Content-Type", "application/json") req.Header.Set("Session_id", "session-123") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) } if executor.request == nil { t.Fatal("Codex executor did not receive a request") } if got, want := executor.request.URL.String(), "https://chatgpt.com/backend-api/codex/alpha/search"; got != want { t.Fatalf("upstream URL = %q, want %q", got, want) } if got, want := string(executor.body), `{"query":"GPT-5.6"}`; got != want { t.Fatalf("upstream body = %q, want %q", got, want) } if got := executor.request.Header.Get("Authorization"); got != "Bearer codex-token" { t.Fatalf("Authorization = %q", got) } if got := executor.request.Header.Get("Chatgpt-Account-Id"); got != "account-123" { t.Fatalf("Chatgpt-Account-Id = %q", got) } if got := executor.request.Header.Get("Session_id"); got != "session-123" { t.Fatalf("Session_id = %q", got) } if got := rr.Header().Get("Content-Type"); got != "application/json" { t.Fatalf("response Content-Type = %q", got) } traceID := rr.Header().Get(internallogging.CPATraceIDHeader) parts := strings.Split(traceID, "-") if len(parts) != 3 || parts[1] != credential.Index || len(parts[2]) != 8 { t.Fatalf("trace ID = %q, want timestamp-%s-requestID", traceID, credential.Index) } if _, errParse := time.Parse("20060102150405", parts[0]); errParse != nil { t.Fatalf("trace timestamp = %q: %v", parts[0], errParse) } } func TestCodexAlphaSearchUsesPluginProviderTargetModel(t *testing.T) { server := newTestServer(t) executor := &codexSearchCaptureExecutor{} server.handlers.AuthManager.RegisterExecutor(executor) router := &codexSearchModelRouter{ response: pluginapi.ModelRouteResponse{ Handled: true, TargetKind: pluginapi.ModelRouteTargetProvider, Target: "codex", TargetModel: "team-b/gpt-5.6-sol", }, handled: true, } server.handlers.SetModelRouterHost(router) for _, credential := range []*auth.Auth{ { ID: "codex-team-a", Provider: "codex", Prefix: "team-a", Status: auth.StatusActive, Metadata: map[string]any{"access_token": "token-a"}, }, { ID: "codex-team-b", Provider: "codex", Prefix: "team-b", Status: auth.StatusActive, Metadata: map[string]any{"access_token": "token-b"}, }, } { if _, errRegister := server.handlers.AuthManager.Register(context.Background(), credential); errRegister != nil { t.Fatalf("register Codex auth %s: %v", credential.ID, errRegister) } registry.GetGlobalRegistry().RegisterClient(credential.ID, credential.Provider, []*registry.ModelInfo{{ID: credential.Prefix + "/gpt-5.6-sol"}}) t.Cleanup(func() { registry.GetGlobalRegistry().UnregisterClient(credential.ID) }) } payload := `{"id":"session-123","model":"gpt-5.6-sol","commands":{"search_query":[{"q":"golang"}]}}` paths := []string{"/v1/alpha/search?key=test-key", "/backend-api/codex/alpha/search?key=test-key"} for _, path := range paths { req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(payload)) req.Header.Set("Authorization", "Bearer test-key") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("%s status = %d, want %d; body=%s", path, rr.Code, http.StatusOK, rr.Body.String()) } } if got, want := executor.authIDs, []string{"codex-team-b", "codex-team-b"}; len(got) != len(want) || got[0] != want[0] || got[1] != want[1] { t.Fatalf("selected auth IDs = %v, want %v", got, want) } if got := string(executor.body); got != payload { t.Fatalf("upstream body = %q, want original unprefixed body %q", got, payload) } if got, want := len(router.requests), 2; got != want { t.Fatalf("model router requests = %d, want %d", got, want) } for index, routeReq := range router.requests { if routeReq.SourceFormat != "codex-alpha-search" { t.Fatalf("model router source format = %q", routeReq.SourceFormat) } if routeReq.RequestedModel != "gpt-5.6-sol" { t.Fatalf("model router requested model = %q", routeReq.RequestedModel) } if got := routeReq.Headers.Get("Authorization"); got != "Bearer test-key" { t.Fatalf("model router Authorization = %q", got) } if got := routeReq.Query.Get("key"); got != "test-key" { t.Fatalf("model router query key = %q", got) } if got, want := routeReq.Metadata[coreexecutor.RequestPathMetadataKey], strings.SplitN(paths[index], "?", 2)[0]; got != want { t.Fatalf("model router request path = %#v, want %q", got, want) } if got := string(routeReq.Body); got != payload { t.Fatalf("model router body = %q, want %q", got, payload) } } } func TestCodexAlphaSearchFallsBackWhenPluginDoesNotHandleRoute(t *testing.T) { server := newTestServer(t) executor := &codexSearchCaptureExecutor{} server.handlers.AuthManager.RegisterExecutor(executor) credential := &auth.Auth{ ID: "codex-auth", Provider: "codex", Status: auth.StatusActive, Metadata: map[string]any{"access_token": "codex-token"}, } if _, errRegister := server.handlers.AuthManager.Register(context.Background(), credential); errRegister != nil { t.Fatalf("register Codex auth: %v", errRegister) } registry.GetGlobalRegistry().RegisterClient(credential.ID, credential.Provider, []*registry.ModelInfo{{ID: "gpt-5.6-sol"}}) t.Cleanup(func() { registry.GetGlobalRegistry().UnregisterClient(credential.ID) }) router := &codexSearchModelRouter{} server.handlers.SetModelRouterHost(router) payload := `{"model":"gpt-5.6-sol"}` req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(payload)) req.Header.Set("Authorization", "Bearer test-key") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) } if got := executor.authIDs; len(got) != 1 || got[0] != credential.ID { t.Fatalf("selected auth IDs = %v, want [%s]", got, credential.ID) } if got := string(executor.body); got != payload { t.Fatalf("upstream body = %q, want %q", got, payload) } if got := len(router.requests); got != 1 { t.Fatalf("model router requests = %d, want 1", got) } } func TestCodexAlphaSearchRejectsUnsupportedPluginRouteTarget(t *testing.T) { server := newTestServer(t) executor := &codexSearchCaptureExecutor{} server.handlers.AuthManager.RegisterExecutor(executor) credential := &auth.Auth{ ID: "codex-auth", Provider: "codex", Status: auth.StatusActive, Metadata: map[string]any{"access_token": "codex-token"}, } if _, errRegister := server.handlers.AuthManager.Register(context.Background(), credential); errRegister != nil { t.Fatalf("register Codex auth: %v", errRegister) } registry.GetGlobalRegistry().RegisterClient(credential.ID, credential.Provider, []*registry.ModelInfo{{ID: "gpt-5.6-sol"}}) t.Cleanup(func() { registry.GetGlobalRegistry().UnregisterClient(credential.ID) }) server.handlers.SetModelRouterHost(&codexSearchModelRouter{ response: pluginapi.ModelRouteResponse{ Handled: true, TargetKind: pluginapi.ModelRouteTargetSelf, Target: "user-routing", }, handled: true, }) req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(`{"model":"gpt-5.6-sol"}`)) req.Header.Set("Authorization", "Bearer test-key") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusServiceUnavailable { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusServiceUnavailable, rr.Body.String()) } if executor.request != nil { t.Fatal("unsupported plugin route sent an upstream request") } } func TestCodexAlphaSearchSanitizesResponsesOnlyFields(t *testing.T) { server := newTestServer(t) executor := &codexSearchCaptureExecutor{} server.handlers.AuthManager.RegisterExecutor(executor) credential := &auth.Auth{ ID: "codex-auth", Provider: "codex", Status: auth.StatusActive, Metadata: map[string]any{"access_token": "codex-token"}, } if _, errRegister := server.handlers.AuthManager.Register(context.Background(), credential); errRegister != nil { t.Fatalf("register Codex auth: %v", errRegister) } registry.GetGlobalRegistry().RegisterClient(credential.ID, credential.Provider, []*registry.ModelInfo{{ID: "gpt-5.6-sol"}}) t.Cleanup(func() { registry.GetGlobalRegistry().UnregisterClient(credential.ID) }) payload := `{"id":"session-123","model":"gpt-5.6-sol","commands":{"search_query":[{"q":"golang channels"}]},"prompt_cache_key":"cache-123","prompt_cache_retention":"24h"}` for _, path := range []string{"/v1/alpha/search", "/backend-api/codex/alpha/search"} { t.Run(path, func(t *testing.T) { req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(payload)) req.Header.Set("Authorization", "Bearer test-key") req.Header.Set("Content-Type", "application/json") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) } var upstreamBody map[string]json.RawMessage if errUnmarshal := json.Unmarshal(executor.body, &upstreamBody); errUnmarshal != nil { t.Fatalf("unmarshal upstream body: %v; body=%s", errUnmarshal, executor.body) } if _, exists := upstreamBody["prompt_cache_key"]; exists { t.Fatalf("upstream body contains prompt_cache_key: %s", executor.body) } if _, exists := upstreamBody["prompt_cache_retention"]; exists { t.Fatalf("upstream body contains prompt_cache_retention: %s", executor.body) } for _, field := range []string{"id", "model", "commands"} { if _, exists := upstreamBody[field]; !exists { t.Fatalf("upstream body missing %s: %s", field, executor.body) } } }) } } func TestCodexAlphaSearchCredentialPolicy(t *testing.T) { newServer := func(t *testing.T, credentials ...*auth.Auth) (*Server, *codexSearchCaptureExecutor) { t.Helper() server := newTestServer(t) server.handlers.AuthManager.SetSelector(&codexSearchAPIKeyFirstSelector{}) executor := &codexSearchCaptureExecutor{} server.handlers.AuthManager.RegisterExecutor(executor) for _, credential := range credentials { if _, errRegister := server.handlers.AuthManager.Register(context.Background(), credential); errRegister != nil { t.Fatalf("register Codex auth %s: %v", credential.ID, errRegister) } } return server, executor } apiKeyCredential := func() *auth.Auth { return &auth.Auth{ ID: "codex-api-key", Provider: "codex", Status: auth.StatusActive, Attributes: map[string]string{auth.AttributeAPIKey: "codex-key"}, } } oauthCredential := func() *auth.Auth { return &auth.Auth{ ID: "codex-oauth", Provider: "codex", Status: auth.StatusActive, Metadata: map[string]any{"access_token": "codex-token"}, } } t.Run("mixed credentials", func(t *testing.T) { server, executor := newServer(t, apiKeyCredential(), oauthCredential()) req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(`{"query":"GPT-5.6"}`)) req.Header.Set("Authorization", "Bearer test-key") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) } if got := executor.authIDs; len(got) != 1 || got[0] != "codex-oauth" { t.Fatalf("selected auth IDs = %v, want [codex-oauth]", got) } }) t.Run("ordinary API key only", func(t *testing.T) { server, executor := newServer(t, apiKeyCredential()) req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(`{"query":"GPT-5.6"}`)) req.Header.Set("Authorization", "Bearer test-key") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusServiceUnavailable { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusServiceUnavailable, rr.Body.String()) } if len(executor.authIDs) != 0 { t.Fatalf("selected auth IDs = %v, want none", executor.authIDs) } }) } func TestCodexAlphaSearchOptInAPIKeyUsesConfiguredEndpoint(t *testing.T) { server := newTestServer(t) executor := &codexSearchCaptureExecutor{} server.handlers.AuthManager.RegisterExecutor(executor) credential := &auth.Auth{ ID: "codex-alpha-api-key", Provider: "codex", Status: auth.StatusActive, Attributes: map[string]string{ auth.AttributeAPIKey: "codex-alpha-key", auth.AttributeCodexAlphaSearch: "true", "base_url": "https://codex.example.com/v1/", }, } if _, errRegister := server.handlers.AuthManager.Register(context.Background(), credential); errRegister != nil { t.Fatalf("register Codex API key: %v", errRegister) } payload := `{"query":"golang","prompt_cache_key":"cache","prompt_cache_retention":"24h"}` req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(payload)) req.Header.Set("Authorization", "Bearer test-key") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) } if executor.request == nil { t.Fatal("Codex executor did not receive a request") } if got, want := executor.request.URL.String(), "https://codex.example.com/v1/alpha/search"; got != want { t.Fatalf("upstream URL = %q, want %q", got, want) } if got := executor.request.Header.Get("Authorization"); got != "Bearer codex-alpha-key" { t.Fatalf("Authorization = %q, want API key bearer", got) } var upstreamBody map[string]json.RawMessage if errUnmarshal := json.Unmarshal(executor.body, &upstreamBody); errUnmarshal != nil { t.Fatalf("unmarshal upstream body: %v", errUnmarshal) } for _, field := range []string{"prompt_cache_key", "prompt_cache_retention"} { if _, exists := upstreamBody[field]; exists { t.Fatalf("upstream body contains %s: %s", field, executor.body) } } } func TestCodexAlphaSearchOptInAPIKeyStripsCredentialPrefix(t *testing.T) { server := newTestServer(t) executor := &codexSearchCaptureExecutor{} server.handlers.AuthManager.RegisterExecutor(executor) credential := &auth.Auth{ ID: "codex-alpha-api-key-prefix", Provider: "codex", Prefix: "vendor", Status: auth.StatusActive, Attributes: map[string]string{ auth.AttributeAPIKey: "codex-alpha-key", auth.AttributeCodexAlphaSearch: "true", "base_url": "https://codex.example.com/v1", }, } if _, errRegister := server.handlers.AuthManager.Register(context.Background(), credential); errRegister != nil { t.Fatalf("register Codex API key: %v", errRegister) } registry.GetGlobalRegistry().RegisterClient(credential.ID, credential.Provider, []*registry.ModelInfo{{ID: "vendor/gpt-5.6-sol"}}) t.Cleanup(func() { registry.GetGlobalRegistry().UnregisterClient(credential.ID) }) payload := `{"id":"00000000-0000-4000-8000-000000000003","model":"vendor/gpt-5.6-sol","commands":{"search_query":[{"q":"Go programming language official website"}]}}` req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(payload)) req.Header.Set("Authorization", "Bearer test-key") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) } if executor.request == nil { t.Fatal("Codex executor did not receive a request") } var upstreamBody map[string]json.RawMessage if errUnmarshal := json.Unmarshal(executor.body, &upstreamBody); errUnmarshal != nil { t.Fatalf("unmarshal upstream body: %v", errUnmarshal) } var upstreamModel string if errUnmarshal := json.Unmarshal(upstreamBody["model"], &upstreamModel); errUnmarshal != nil { t.Fatalf("unmarshal upstream model: %v", errUnmarshal) } if upstreamModel != "gpt-5.6-sol" { t.Fatalf("upstream model = %q, want gpt-5.6-sol", upstreamModel) } } func TestCodexAlphaSearchOptInAPIKeyResolvesModelAlias(t *testing.T) { server := newTestServer(t) executor := &codexSearchCaptureExecutor{} server.handlers.AuthManager.RegisterExecutor(executor) server.handlers.AuthManager.SetConfig(&proxyconfig.Config{ CodexKey: []proxyconfig.CodexKey{{ APIKey: "codex-alpha-key", Prefix: "vendor", BaseURL: "https://codex.example.com/v1", AlphaSearch: true, Models: []proxyconfig.CodexModel{{ Name: "gpt-5.6-sol", Alias: "sol-alias", }}, }}, }) credential := &auth.Auth{ ID: "codex-alpha-api-key-alias", Provider: "codex", Prefix: "vendor", Status: auth.StatusActive, Attributes: map[string]string{ auth.AttributeAPIKey: "codex-alpha-key", auth.AttributeCodexAlphaSearch: "true", "base_url": "https://codex.example.com/v1", }, } if _, errRegister := server.handlers.AuthManager.Register(context.Background(), credential); errRegister != nil { t.Fatalf("register Codex API key: %v", errRegister) } registry.GetGlobalRegistry().RegisterClient(credential.ID, credential.Provider, []*registry.ModelInfo{{ID: "vendor/sol-alias"}}) t.Cleanup(func() { registry.GetGlobalRegistry().UnregisterClient(credential.ID) }) payload := `{"model":"vendor/sol-alias","commands":{"search_query":[{"q":"golang"}]}}` req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(payload)) req.Header.Set("Authorization", "Bearer test-key") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) } if executor.request == nil { t.Fatal("Codex executor did not receive a request") } var upstreamBody map[string]json.RawMessage if errUnmarshal := json.Unmarshal(executor.body, &upstreamBody); errUnmarshal != nil { t.Fatalf("unmarshal upstream body: %v", errUnmarshal) } var upstreamModel string if errUnmarshal := json.Unmarshal(upstreamBody["model"], &upstreamModel); errUnmarshal != nil { t.Fatalf("unmarshal upstream model: %v", errUnmarshal) } if upstreamModel != "gpt-5.6-sol" { t.Fatalf("upstream model = %q, want gpt-5.6-sol", upstreamModel) } } func TestRewriteCodexAlphaSearchModel(t *testing.T) { original := []byte(`{"id":"search-1","model":"vendor/gpt-5.6-sol","commands":{"search_query":[{"q":"golang"}]}}`) rewritten := rewriteCodexAlphaSearchModel(original, "gpt-5.6-sol") var payload map[string]json.RawMessage if errUnmarshal := json.Unmarshal(rewritten, &payload); errUnmarshal != nil { t.Fatalf("unmarshal rewritten body: %v", errUnmarshal) } var model string if errUnmarshal := json.Unmarshal(payload["model"], &model); errUnmarshal != nil { t.Fatalf("unmarshal rewritten model: %v", errUnmarshal) } if model != "gpt-5.6-sol" { t.Fatalf("model = %q, want gpt-5.6-sol", model) } if _, exists := payload["commands"]; !exists { t.Fatal("commands field was dropped") } if string(rewriteCodexAlphaSearchModel([]byte(`{"query":"x"}`), "gpt-5.6-sol")) != `{"query":"x"}` { t.Fatal("body without model should remain unchanged") } } func TestCodexAlphaSearchOptInAPIKeyWithoutBaseURLFailsClosed(t *testing.T) { server := newTestServer(t) executor := &codexSearchCaptureExecutor{} server.handlers.AuthManager.RegisterExecutor(executor) if _, errRegister := server.handlers.AuthManager.Register(context.Background(), &auth.Auth{ ID: "codex-alpha-api-key", Provider: "codex", Status: auth.StatusActive, Attributes: map[string]string{ auth.AttributeAPIKey: "codex-alpha-key", auth.AttributeCodexAlphaSearch: "true", }, }); errRegister != nil { t.Fatalf("register Codex API key: %v", errRegister) } req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(`{"query":"GPT-5.6"}`)) req.Header.Set("Authorization", "Bearer test-key") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusServiceUnavailable { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusServiceUnavailable, rr.Body.String()) } if executor.request != nil { t.Fatal("request was sent without an API key base URL") } } func TestCodexAlphaSearchPassesGinContextToAuthSelection(t *testing.T) { server := newTestServer(t) selector := &codexSearchGinContextSelector{} server.handlers.AuthManager.SetSelector(selector) executor := &codexSearchCaptureExecutor{} server.handlers.AuthManager.RegisterExecutor(executor) credential := &auth.Auth{ ID: "codex-auth", Provider: "codex", Status: auth.StatusActive, Metadata: map[string]any{"access_token": "codex-token"}, } if _, errRegister := server.handlers.AuthManager.Register(context.Background(), credential); errRegister != nil { t.Fatalf("register Codex auth: %v", errRegister) } req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search?key=home-query-key", strings.NewReader(`{"query":"GPT-5.6"}`)) req.Header.Set("Authorization", "Bearer test-key") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) } if selector.ginContext == nil { t.Fatal("auth selection did not receive the Gin context required by Home scheduling") } if got := selector.ginContext.Query("key"); got != "home-query-key" { t.Fatalf("Gin query key = %q, want %q", got, "home-query-key") } } func TestCodexAlphaSearchUsesRequestIDForSessionAffinity(t *testing.T) { server := newTestServer(t) server.handlers.AuthManager.SetSelector(auth.NewSessionAffinitySelector(&auth.RoundRobinSelector{})) executor := &codexSearchCaptureExecutor{} server.handlers.AuthManager.RegisterExecutor(executor) for _, id := range []string{"codex-auth-a", "codex-auth-b"} { registry.GetGlobalRegistry().RegisterClient(id, "codex", []*registry.ModelInfo{{ID: "gpt-5.6-luna"}}) t.Cleanup(func() { registry.GetGlobalRegistry().UnregisterClient(id) }) credential := &auth.Auth{ ID: id, Provider: "codex", Status: auth.StatusActive, Metadata: map[string]any{"access_token": id}, } if _, errRegister := server.handlers.AuthManager.Register(context.Background(), credential); errRegister != nil { t.Fatalf("register Codex auth: %v", errRegister) } } for _, payload := range []string{ `{"id":"session-a","model":"gpt-5.6-luna"}`, `{"id":"session-b","model":"gpt-5.6-luna"}`, `{"id":"session-a","model":"gpt-5.6-luna"}`, } { req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(payload)) req.Header.Set("Authorization", "Bearer test-key") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) } } if got, want := len(executor.authIDs), 3; got != want { t.Fatalf("selected auth count = %d, want %d", got, want) } if executor.authIDs[0] == executor.authIDs[1] { t.Fatalf("different sessions selected the same auth %q", executor.authIDs[0]) } if got, want := executor.authIDs[2], executor.authIDs[0]; got != want { t.Fatalf("session-affinity auth = %q, want %q", got, want) } } func TestCodexAlphaSearchRecordsRequestLog(t *testing.T) { server := newTestServer(t) server.cfg.RequestLog = true executor := &codexSearchCaptureExecutor{} server.handlers.AuthManager.RegisterExecutor(executor) credential := &auth.Auth{ ID: "codex-auth", Provider: "codex", Status: auth.StatusActive, Metadata: map[string]any{"access_token": "codex-token", "account_id": "account-123"}, } if _, err := server.handlers.AuthManager.Register(context.Background(), credential); err != nil { t.Fatalf("register Codex auth: %v", err) } rr := httptest.NewRecorder() c, _ := gin.CreateTestContext(rr) req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(`{"query":"GPT-5.6"}`)) req.Header.Set("Authorization", "Bearer test-key") req.Header.Set("Content-Type", "application/json") c.Request = req server.codexAlphaSearch(c) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d; body=%s", rr.Code, http.StatusOK, rr.Body.String()) } rawAPIRequest, okRequest := c.Get("API_REQUEST") if !okRequest { t.Fatal("API_REQUEST was not captured") } apiRequest, _ := rawAPIRequest.([]byte) if !strings.Contains(string(apiRequest), "=== API REQUEST 1 ===") { t.Fatalf("API_REQUEST missing request header section: %q", apiRequest) } if !strings.Contains(string(apiRequest), "https://chatgpt.com/backend-api/codex/alpha/search") { t.Fatalf("API_REQUEST missing upstream URL: %q", apiRequest) } if !strings.Contains(string(apiRequest), `{"query":"GPT-5.6"}`) { t.Fatalf("API_REQUEST missing body: %q", apiRequest) } rawAPIResponse, okResponse := c.Get("API_RESPONSE") if !okResponse { t.Fatal("API_RESPONSE was not captured") } apiResponse, _ := rawAPIResponse.([]byte) if !strings.Contains(string(apiResponse), "=== API RESPONSE 1 ===") { t.Fatalf("API_RESPONSE missing response header section: %q", apiResponse) } if !strings.Contains(string(apiResponse), `{"results":[{"url":"https://example.com"}]}`) { t.Fatalf("API_RESPONSE missing body: %q", apiResponse) } } func TestManagementResponseExposesPluginSupportHeaderForCORS(t *testing.T) { t.Setenv("MANAGEMENT_PASSWORD", "test-management-key") server := newTestServer(t) req := httptest.NewRequest(http.MethodGet, "/v0/management/config", nil) req.Header.Set("Origin", "http://127.0.0.1:5173") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusUnauthorized { t.Fatalf("status = %d, want %d body=%s", rr.Code, http.StatusUnauthorized, rr.Body.String()) } if got := rr.Header().Get("X-CPA-SUPPORT-PLUGIN"); got != pluginhost.SupportPluginHeaderValue() { t.Fatalf("X-CPA-SUPPORT-PLUGIN = %q, want %q", got, pluginhost.SupportPluginHeaderValue()) } exposedHeaders := make(map[string]struct{}) for _, headerName := range strings.Split(rr.Header().Get("Access-Control-Expose-Headers"), ",") { headerName = strings.ToLower(strings.TrimSpace(headerName)) if headerName != "" { exposedHeaders[headerName] = struct{}{} } } for _, headerName := range corsExposedResponseHeaders { if _, ok := exposedHeaders[strings.ToLower(headerName)]; !ok { t.Fatalf("Access-Control-Expose-Headers missing %s: %q", headerName, rr.Header().Get("Access-Control-Expose-Headers")) } } } func TestOAuthCallbackRouteSkipsManagementKeyMiddleware(t *testing.T) { t.Setenv("MANAGEMENT_PASSWORD", "test-management-key") server := newTestServer(t) state := "server-plugin-oauth-state" if errRegister := managementHandlers.RegisterPluginOAuthSession(state, "gemini-cli", nil); errRegister != nil { t.Fatalf("register plugin oauth session: %v", errRegister) } defer managementHandlers.CompleteOAuthSession(state) req := httptest.NewRequest(http.MethodGet, "/v0/management/oauth-callback?state="+state+"&code=test-code", nil) rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d body=%s", rr.Code, http.StatusOK, rr.Body.String()) } callbackPath := filepath.Join(server.cfg.AuthDir, ".oauth-gemini-cli-"+state+".oauth") if _, errRead := os.ReadFile(callbackPath); errRead != nil { t.Fatalf("expected callback file to be written without management key: %v", errRead) } } func TestNewServerWithPluginHostInjectsHandlerInterceptors(t *testing.T) { host := pluginhost.New() server := newTestServerWithOptions(t, WithPluginHost(host)) if server.handlers == nil { t.Fatal("server handlers = nil") } got, ok := server.handlers.PluginHost.(*pluginhost.Host) if !ok || got != host { t.Fatalf("handler plugin host = %#v, want configured host", server.handlers.PluginHost) } } func TestNewServerWithoutPluginHostLeavesHandlerInterceptorsDisabled(t *testing.T) { server := newTestServer(t) if server.handlers == nil { t.Fatal("server handlers = nil") } if server.handlers.PluginHost != nil { t.Fatalf("handler plugin host = %#v, want nil", server.handlers.PluginHost) } } func TestManagementUsageRequiresManagementAuthAndPopsArray(t *testing.T) { t.Setenv("MANAGEMENT_PASSWORD", "test-management-key") prevQueueEnabled := redisqueue.Enabled() redisqueue.SetEnabled(false) t.Cleanup(func() { redisqueue.SetEnabled(false) redisqueue.SetEnabled(prevQueueEnabled) }) server := newTestServer(t) redisqueue.Enqueue([]byte(`{"id":1}`)) redisqueue.Enqueue([]byte(`{"id":2}`)) missingKeyReq := httptest.NewRequest(http.MethodGet, "/v0/management/usage-queue?count=2", nil) missingKeyRR := httptest.NewRecorder() server.engine.ServeHTTP(missingKeyRR, missingKeyReq) if missingKeyRR.Code != http.StatusUnauthorized { t.Fatalf("missing key status = %d, want %d body=%s", missingKeyRR.Code, http.StatusUnauthorized, missingKeyRR.Body.String()) } legacyReq := httptest.NewRequest(http.MethodGet, "/v0/management/usage?count=2", nil) legacyReq.Header.Set("Authorization", "Bearer test-management-key") legacyRR := httptest.NewRecorder() server.engine.ServeHTTP(legacyRR, legacyReq) if legacyRR.Code != http.StatusNotFound { t.Fatalf("legacy usage status = %d, want %d body=%s", legacyRR.Code, http.StatusNotFound, legacyRR.Body.String()) } authReq := httptest.NewRequest(http.MethodGet, "/v0/management/usage-queue?count=2", nil) authReq.Header.Set("Authorization", "Bearer test-management-key") authRR := httptest.NewRecorder() server.engine.ServeHTTP(authRR, authReq) if authRR.Code != http.StatusOK { t.Fatalf("authenticated status = %d, want %d body=%s", authRR.Code, http.StatusOK, authRR.Body.String()) } var payload []json.RawMessage if errUnmarshal := json.Unmarshal(authRR.Body.Bytes(), &payload); errUnmarshal != nil { t.Fatalf("unmarshal response: %v body=%s", errUnmarshal, authRR.Body.String()) } if len(payload) != 2 { t.Fatalf("response records = %d, want 2", len(payload)) } for i, raw := range payload { var record struct { ID int `json:"id"` } if errUnmarshal := json.Unmarshal(raw, &record); errUnmarshal != nil { t.Fatalf("unmarshal record %d: %v", i, errUnmarshal) } if record.ID != i+1 { t.Fatalf("record %d id = %d, want %d", i, record.ID, i+1) } } if remaining := redisqueue.PopOldest(1); len(remaining) != 0 { t.Fatalf("remaining queue = %q, want empty", remaining) } } func TestManagementPluginsRouteRegistered(t *testing.T) { t.Setenv("MANAGEMENT_PASSWORD", "test-management-key") server := newTestServer(t) enabled := true server.cfg.Plugins.Configs = map[string]proxyconfig.PluginInstanceConfig{ "sample": {Enabled: &enabled, Priority: 4}, } if errWrite := os.WriteFile(server.configFilePath, []byte("{}\n"), 0o600); errWrite != nil { t.Fatalf("failed to write config file: %v", errWrite) } req := httptest.NewRequest(http.MethodGet, "/v0/management/plugins", nil) req.Header.Set("Authorization", "Bearer test-management-key") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d body=%s", rr.Code, http.StatusOK, rr.Body.String()) } var payload struct { PluginsEnabled bool `json:"plugins_enabled"` Plugins []any `json:"plugins"` } if errUnmarshal := json.Unmarshal(rr.Body.Bytes(), &payload); errUnmarshal != nil { t.Fatalf("unmarshal response: %v body=%s", errUnmarshal, rr.Body.String()) } if payload.Plugins == nil { t.Fatalf("plugins field = nil, want array; body=%s", rr.Body.String()) } req = httptest.NewRequest(http.MethodGet, "/v0/management/plugins/sample/config", nil) req.Header.Set("Authorization", "Bearer test-management-key") rr = httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("config status = %d, want %d body=%s", rr.Code, http.StatusOK, rr.Body.String()) } var configPayload struct { Enabled bool `json:"enabled"` Priority int `json:"priority"` } if errUnmarshal := json.Unmarshal(rr.Body.Bytes(), &configPayload); errUnmarshal != nil { t.Fatalf("unmarshal config response: %v body=%s", errUnmarshal, rr.Body.String()) } if !configPayload.Enabled || configPayload.Priority != 4 { t.Fatalf("plugin config = %#v, want enabled true priority 4", configPayload) } req = httptest.NewRequest(http.MethodDelete, "/v0/management/plugins/sample", nil) req.Header.Set("Authorization", "Bearer test-management-key") rr = httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("delete status = %d, want %d body=%s", rr.Code, http.StatusOK, rr.Body.String()) } } func TestVideosRoutesKeepXAINativeAndExposeOpenAIPrefix(t *testing.T) { server := newTestServer(t) nativeReq := httptest.NewRequest(http.MethodPost, "/v1/videos", strings.NewReader(`{"model":"sora-2","prompt":"make a video"}`)) nativeReq.Header.Set("Authorization", "Bearer test-key") nativeReq.Header.Set("Content-Type", "application/json") nativeRR := httptest.NewRecorder() server.engine.ServeHTTP(nativeRR, nativeReq) if nativeRR.Code != http.StatusBadRequest { t.Fatalf("native status = %d, want %d body=%s", nativeRR.Code, http.StatusBadRequest, nativeRR.Body.String()) } if !strings.Contains(nativeRR.Body.String(), "/v1/videos/generations") { t.Fatalf("expected /v1/videos to keep xAI native validation, body=%s", nativeRR.Body.String()) } openAIReq := httptest.NewRequest(http.MethodPost, "/openai/v1/videos", strings.NewReader(`{"model":`)) openAIReq.Header.Set("Authorization", "Bearer test-key") openAIReq.Header.Set("Content-Type", "application/json") openAIRR := httptest.NewRecorder() server.engine.ServeHTTP(openAIRR, openAIReq) if openAIRR.Code != http.StatusBadRequest { t.Fatalf("openai create status = %d, want %d body=%s", openAIRR.Code, http.StatusBadRequest, openAIRR.Body.String()) } if !strings.Contains(openAIRR.Body.String(), "body must be valid JSON") { t.Fatalf("expected /openai/v1/videos create handler, body=%s", openAIRR.Body.String()) } contentReq := httptest.NewRequest(http.MethodGet, "/openai/v1/videos/video_123/content?variant=thumbnail", nil) contentReq.Header.Set("Authorization", "Bearer test-key") contentRR := httptest.NewRecorder() server.engine.ServeHTTP(contentRR, contentReq) if contentRR.Code != http.StatusBadRequest { t.Fatalf("content status = %d, want %d body=%s", contentRR.Code, http.StatusBadRequest, contentRR.Body.String()) } if !strings.Contains(contentRR.Body.String(), "variant") { t.Fatalf("expected /openai/v1/videos content handler, body=%s", contentRR.Body.String()) } } func TestHomeEnabledHidesManagementEndpointsAndControlPanel(t *testing.T) { t.Setenv("MANAGEMENT_PASSWORD", "test-management-key") server := newTestServer(t) server.cfg.Home.Enabled = true t.Run("management endpoints return 404", func(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/v0/management/config", nil) req.Header.Set("Authorization", "Bearer test-management-key") rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusNotFound { t.Fatalf("status = %d, want %d body=%s", rr.Code, http.StatusNotFound, rr.Body.String()) } }) t.Run("management control panel returns 404", func(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/management.html", nil) rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusNotFound { t.Fatalf("status = %d, want %d body=%s", rr.Code, http.StatusNotFound, rr.Body.String()) } }) } func TestExampleAPIKeySafeModeShowsWarningAndKeepsManagement(t *testing.T) { t.Setenv("MANAGEMENT_PASSWORD", "test-management-key") staticDir := t.TempDir() t.Setenv("MANAGEMENT_STATIC_PATH", staticDir) if err := os.WriteFile(filepath.Join(staticDir, "management.html"), []byte("management app"), 0o600); err != nil { t.Fatalf("failed to write management asset: %v", err) } server := newTestServerWithOptions(t, WithExampleAPIKeySafeMode()) cfg := *server.cfg cfg.APIKeys = []string{"your-api-key-1"} server.UpdateClients(&cfg) t.Run("root warning page includes management link", func(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/", nil) rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d body=%s", rr.Code, http.StatusOK, rr.Body.String()) } body := rr.Body.String() for _, want := range []string{"Example API key detected", "Open Management", `href="/management.html?safe-mode=configure"`} { if !strings.Contains(body, want) { t.Fatalf("warning page missing %q: %s", want, body) } } }) t.Run("management html defaults to warning page", func(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/management.html", nil) rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d body=%s", rr.Code, http.StatusOK, rr.Body.String()) } if !strings.Contains(rr.Body.String(), "Example API key detected") { t.Fatalf("management.html did not show warning page: %s", rr.Body.String()) } }) t.Run("management html head stops at warning page", func(t *testing.T) { req := httptest.NewRequest(http.MethodHead, "/management.html", nil) rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d body=%s", rr.Code, http.StatusOK, rr.Body.String()) } if rr.Body.Len() != 0 { t.Fatalf("HEAD body length = %d, want 0", rr.Body.Len()) } if got := rr.Header().Get("Cache-Control"); got != "no-store" { t.Fatalf("Cache-Control = %q, want no-store", got) } }) t.Run("management button query opens control panel", func(t *testing.T) { req := httptest.NewRequest(http.MethodGet, "/management.html?safe-mode=configure", nil) rr := httptest.NewRecorder() server.engine.ServeHTTP(rr, req) if rr.Code != http.StatusOK { t.Fatalf("status = %d, want %d body=%s", rr.Code, http.StatusOK, rr.Body.String()) } if !strings.Contains(rr.Body.String(), "