package router import ( "net/http" "net/http/httptest" "path/filepath" "strings" "testing" "gitea.mynodes.xyz/ilker/llmgw/internal/config" ) func docsRouter(t *testing.T, signIn bool) http.Handler { t.Helper() cfg := baseConfig(config.Endpoint{ID: "a", URL: "http://127.0.0.1:1", Models: []string{"m"}}) cfg.Auth.Enabled = true cfg.Auth.Store = filepath.Join(t.TempDir(), "store.db") cfg.Landing.DocsSignIn = signIn return newTestRouter(t, cfg).Handler() } // The docs are public by default on a gateway with keys: the client guide is // for the person who has none yet, so asking for one first defeats it. func TestTheDocsAreServedWithoutAKey(t *testing.T) { h := docsRouter(t, false) for _, path := range []string{"/docs", "/docs/clients"} { w := httptest.NewRecorder() h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil)) if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "llmgw docs") { t.Errorf("%s: %d %.200s", path, w.Code, w.Body.String()) } } } func TestDocsSignInSendsAVisitorToSignIn(t *testing.T) { h := docsRouter(t, true) for _, path := range []string{"/docs", "/docs/install"} { w := httptest.NewRecorder() h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil)) // The session guard, not the key check: a browser has no key. if strings.Contains(w.Body.String(), "api key") { t.Errorf("%s asked a browser for an API key", path) } if w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/login" { t.Errorf("%s: %d to %q, want a redirect to /login", path, w.Code, w.Header().Get("Location")) } } }