package main // flags_test.go - what each command-line flag actually changes. The flags // are parsed in main(), which no test can call, so these exercise the state // the flags set and the behaviour that depends on it: the routes an // instance serves, the URLs it writes, the warnings it prints. import ( "encoding/json" "net/http" "path/filepath" "strings" "testing" ) // -no-api: every /api/ path answers a JSON 403, and the UI stops offering // the API. The kill switch is one registration, so a future endpoint // inherits it - that is the property worth testing. func TestFlagNoAPI(t *testing.T) { buildFixture(t) prev := apiEnabled t.Cleanup(func() { apiEnabled = prev; fixtureMux = withBaseHeaders(newMux()) }) apiEnabled = false mux := withBaseHeaders(newMux()) for _, p := range []string{"/api/docs", "/api/doc/prism-slides", "/api/search?q=x", "/api/facets", "/api/something-added-next-year"} { w := record(mux, httptest_NewGet(p)) if w.Code != http.StatusForbidden { t.Errorf("%s with -no-api: status %d, want 403", p, w.Code) } var body map[string]string if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil || body["error"] == "" { t.Errorf("%s: body %q is not a JSON error", p, w.Body.String()) } if ct := w.Header().Get("Content-Type"); !strings.HasPrefix(ct, "application/json") { t.Errorf("%s: Content-Type %q", p, ct) } } // The visitor's own export is NOT part of the API and stays available. if w := record(mux, httptest_NewGet("/pinned/export")); w.Code != 200 { t.Errorf("/pinned/export with -no-api: %d", w.Code) } } // -base: the absolute URLs in robots.txt and the sitemap follow // the canonical host instead of the request's Host header. func TestFlagBase(t *testing.T) { buildFixture(t) prev, prevMap := canonicalBase, corpus().canonicalSitemap t.Cleanup(func() { canonicalBase, corpus().canonicalSitemap = prev, prevMap corpus().buildCanonicalSitemap() }) canonicalBase = "https://archive.example.org" corpus().buildCanonicalSitemap() for _, c := range []struct{ path, want string }{ {"/robots.txt", "Sitemap: https://archive.example.org/sitemap.xml"}, {"/sitemap.xml", "https://archive.example.org/doc/prism-slides"}, } { if body := get(t, c.path).Body.String(); !strings.Contains(body, c.want) { t.Errorf("%s does not carry %q", c.path, c.want) } } // A hostile Host header cannot reach a response once -base is set. r := httptest_NewGet("/robots.txt") r.Host = "evil.example.net" if body := record(fixtureMux, r).Body.String(); strings.Contains(body, "evil") { t.Errorf("the Host header leaked into robots.txt: %s", body) } } // -base is validated, not trusted: anything but scheme://host is refused, // so the flag cannot produce URLs that do not resolve. func TestFlagBaseValidation(t *testing.T) { for _, ok := range []string{"https://a.org", "http://a.org", "https://a.org:8443", "https://a.org/"} { if _, err := parseCanonicalBase(ok); err != nil { t.Errorf("%s rejected: %v", ok, err) } } for _, bad := range []string{"", "a.org", "ftp://a.org", "https://", "https://a.org/path", "https://a.org?q=1", "https://a.org#f", "https://user@a.org", "://x"} { if _, err := parseCanonicalBase(bad); err == nil { t.Errorf("%q accepted", bad) } } } // -check counts what it reports: the fixture holds one document the loader // refuses (no PDF) and one directory that looks like a document. func TestFlagCheckCounts(t *testing.T) { buildFixture(t) if loadErrors == 0 { t.Error("the fixture's PDF-less document should be an error") } if loadErrors+loadWarnings == 0 { t.Error("-check would exit 0 on a corpus with problems") } } // -listen warns for anything but loopback, because binding elsewhere puts // the archive in front of the proxy instead of behind it. func TestFlagListenWarning(t *testing.T) { before := loadWarnings for _, quiet := range []string{"127.0.0.1:8080", "localhost:8080", "[::1]:8080", "127.0.0.53:80"} { warnNonLoopback(quiet) if loadWarnings != before { t.Errorf("%s warned", quiet) before = loadWarnings } } for _, loud := range []string{"0.0.0.0:8080", ":8080", "192.168.1.10:8080", "[::]:8080"} { warnNonLoopback(loud) if loadWarnings == before { t.Errorf("%s did not warn", loud) } before = loadWarnings } } // -dev: templates come from disk and nothing is pre-rendered, so every page // must still render - the fallback path the pre-rendered blocks rely on. func TestFlagDevMode(t *testing.T) { buildFixture(t) prevDev, prevPre, prevTmpl := devMode, corpus().prerendered, tmplSet t.Cleanup(func() { devMode, corpus().prerendered, tmplSet = prevDev, prevPre, prevTmpl initAssets() }) devMode = true initAssets() // templates/ and static/ from the working directory corpus().prerenderPages() if len(corpus().prerendered) != 0 { t.Error("dev mode pre-rendered pages; it must render them live") } for _, p := range []string{"/", "/browse", "/browse/program", "/explore/ids", "/glossary", "/articles", "/explore/terms", "/doc/prism-slides"} { if w := get(t, p); w.Code != 200 { t.Errorf("%s in dev mode: %d", p, w.Code) } } } // -root: the documents are read from its data/ and the files about the // archive from its project/. func TestRootDir(t *testing.T) { buildFixture(t) prevData, prevProject := dataDir, projectDir t.Cleanup(func() { dataDir, projectDir = prevData, prevProject loadData() }) root := t.TempDir() writeFixtureFile(t, filepath.Join(root, "data", "doc-in-root", "metadata.json"), `{"title": "In the root"}`) writeFixtureFile(t, filepath.Join(root, "data", "doc-in-root", "doc-in-root.pdf"), "%PDF-1.4\n") writeFixtureFile(t, filepath.Join(root, "project", "glossary.json"), `[{"term": "INPROJECT", "definition": "Read from project/."}]`) setRoot(root) loadData() if corpus().docIndex["doc-in-root"] == nil { t.Error("the document in data/ was not loaded") } if corpus().glossaryLookup("INPROJECT") == nil { t.Error("glossary.json in project/ was not loaded") } }