package app import ( "bytes" "context" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "testing" ) func TestIsPDFURLIgnoresQueryAndFragment(t *testing.T) { for _, tc := range []struct { raw string want bool }{ {"https://example.test/report.pdf?download=1", true}, {"https://example.test/report.PDF", true}, {"https://example.test/a/b/Report.Pdf#page=2", true}, {"https://example.test/report.pdf.html", false}, {"https://example.test/pdf", false}, {"https://example.test/", false}, } { if got := isPDFURL(tc.raw); got != tc.want { t.Errorf("isPDFURL(%q) = %v, want %v", tc.raw, got, tc.want) } } } func TestLooksLikePDFSkipsLeadingWhitespaceAndBOM(t *testing.T) { for _, tc := range []struct { body []byte want bool }{ {[]byte("%PDF-1.7"), true}, {[]byte(" \n\t%PDF-1.4"), true}, {[]byte("\xef\xbb\xbf%PDF-1.3"), true}, {[]byte(" \xef\xbb\xbf\n%PDF-1.5"), true}, {[]byte("not a PDF"), false}, {[]byte("%PDX-1.7"), false}, {[]byte("%PDF"), false}, {[]byte(""), false}, } { if got := looksLikePDF(tc.body); got != tc.want { t.Errorf("looksLikePDF(%q) = %v, want %v", tc.body, got, tc.want) } } } func TestIsPDFContentType(t *testing.T) { for _, tc := range []struct { contentType string want bool }{ {"application/pdf; charset=binary", true}, {"APPLICATION/PDF", true}, {"text/html", false}, {"", false}, } { if got := isPDFContentType(tc.contentType); got != tc.want { t.Errorf("isPDFContentType(%q) = %v, want %v", tc.contentType, got, tc.want) } } } // PDF text is extracted in-process. The deployment host has no pdftotext, so // shelling out turns every PDF request into a hard failure. func TestExtractPDFTextReadsRealDocument(t *testing.T) { text, err := extractPDFText(fixturePDF(t, "dummy.pdf")) if err != nil { t.Fatal(err) } if !strings.Contains(text, "Dummy PDF file") { t.Fatalf("unexpected extraction: %.120q", text) } } // Malformed input must surface as an error, never as a panic: this runs on // arbitrary caller-supplied URLs. func TestExtractPDFTextRejectsMalformedInput(t *testing.T) { for _, body := range [][]byte{ nil, []byte("not a pdf at all"), []byte("%PDF-1.7\ntruncated"), bytes.Repeat([]byte("x"), 4096), } { if _, err := extractPDFText(body); err == nil { t.Errorf("malformed pdf of %d bytes was accepted", len(body)) } } } // PDFs legitimately exceed the HTML page cap. Before the separate PDF limit a // document larger than the page cap was rejected outright with a size error. // The page cap is set below the fixture's real size to cross the boundary. func TestFetchStaticAcceptsPDFLargerThanPageCap(t *testing.T) { pdf := fixturePDF(t, "report.pdf") if len(pdf) <= pageCapForTest { t.Fatalf("fixture is %d bytes, must exceed the %d byte test cap", len(pdf), pageCapForTest) } srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/pdf") w.Write(pdf) })) defer srv.Close() n := newNetwork(true) defer n.transport.CloseIdleConnections() f := &Fetcher{net: n, maxBytes: pageCapForTest} d, err := f.fetchStatic(context.Background(), srv.URL+"/large.pdf", nil) if err != nil { t.Fatalf("large pdf rejected: %v", err) } if !strings.Contains(d.Markdown, "evidence") { t.Fatalf("unexpected extraction: %.120q", d.Markdown) } } // The larger limit is specific to PDFs; HTML over the page cap is still // rejected. func TestFetchStaticStillRejectsOversizedHTML(t *testing.T) { body := append([]byte("