package main // http_test.go - protocol behaviour the browser and the proxy rely on: // HEAD, byte ranges, conditional requests, and method handling. net/http // implements most of it, but only if the handlers hand it the right thing - // and a download path that loses Range support breaks resuming a 120 MB PDF // and the page-by-page fetches a PDF viewer makes. import ( "fmt" "net/http" "net/http/httptest" "path/filepath" "strings" "testing" ) func TestHTTPHead(t *testing.T) { buildFixture(t) for _, p := range []string{"/", "/browse", "/doc/prism-slides", "/files/prism-slides/prism-slides.pdf", "/api/facets", "/static/style.css"} { r := httptest.NewRequest("HEAD", p, nil) w := httptest.NewRecorder() fixtureMux.ServeHTTP(w, r) if w.Code != 200 { t.Errorf("HEAD %s: %d", p, w.Code) continue } // net/http drops the body for HEAD at the transport layer, so the // recorder still sees one; the headers are what matters here. if w.Header().Get("Content-Type") == "" { t.Errorf("HEAD %s: no Content-Type", p) } } } // Range on a document file. http.ServeFile implements it - this test is // here so a future change to the download path (a buffered writer, a // compressing wrapper, a manual io.Copy) cannot quietly remove it. func TestHTTPRangeOnFiles(t *testing.T) { buildFixture(t) const path = "/files/prism-slides/prism-slides.pdf" full := get(t, path) body := full.Body.String() if full.Header().Get("Accept-Ranges") != "bytes" { t.Fatalf("no Accept-Ranges on a document file: %v", full.Header()) } req := func(rangeHdr string) *httptest.ResponseRecorder { r := httptest.NewRequest("GET", path, nil) r.Header.Set("Range", rangeHdr) w := httptest.NewRecorder() fixtureMux.ServeHTTP(w, r) return w } w := req("bytes=0-3") if w.Code != http.StatusPartialContent { t.Fatalf("Range bytes=0-3: status %d, want 206", w.Code) } if got, want := w.Body.String(), body[:4]; got != want { t.Errorf("range body %q, want %q", got, want) } if cr := w.Header().Get("Content-Range"); cr != fmt.Sprintf("bytes 0-3/%d", len(body)) { t.Errorf("Content-Range %q", cr) } // A suffix range: what a resumed download asks for. w = req(fmt.Sprintf("bytes=%d-", len(body)-4)) if w.Code != http.StatusPartialContent || w.Body.String() != body[len(body)-4:] { t.Errorf("suffix range: %d %q", w.Code, w.Body.String()) } // Past the end is a 416 with the length, not a 200 of the whole file. w = req(fmt.Sprintf("bytes=%d-%d", len(body)+10, len(body)+20)) if w.Code != http.StatusRequestedRangeNotSatisfiable { t.Errorf("unsatisfiable range: status %d, want 416", w.Code) } // A malformed Range is refused with 416 - net/http's choice, not this // server's; it is recorded here so a change in behaviour is noticed. if w = req("bytes=abc"); w.Code != http.StatusRequestedRangeNotSatisfiable { t.Errorf("malformed Range: status %d, want 416", w.Code) } // An unknown range unit is refused the same way. RFC 7233 says to // ignore it and serve the whole representation; net/http does not, and // nothing this archive serves sends one. Recorded, not worked around. if w = req("pages=1-2"); w.Code != http.StatusRequestedRangeNotSatisfiable { t.Errorf("unknown range unit: status %d, want net/http's 416", w.Code) } } // Conditional requests: a document file and a static asset both carry // validators, so a reader's second visit is a 304 instead of a re-download. func TestHTTPConditional(t *testing.T) { buildFixture(t) // A document file comes off disk and carries a modification time. const doc = "/files/prism-slides/prism-slides.pdf" lm := get(t, doc).Header().Get("Last-Modified") if lm == "" { t.Fatalf("%s: no Last-Modified", doc) } r := httptest.NewRequest("GET", doc, nil) r.Header.Set("If-Modified-Since", lm) w := httptest.NewRecorder() fixtureMux.ServeHTTP(w, r) if w.Code != http.StatusNotModified || w.Body.Len() != 0 { t.Errorf("%s with If-Modified-Since: status %d, %d bytes", doc, w.Code, w.Body.Len()) } // An embedded asset has no modification time - embed.FS zeroes it - so // it carries a content ETag instead. Without one, every page view // re-downloads the stylesheet on an unstamped build. for _, asset := range []string{"/static/style.css", "/static/main.js"} { first := get(t, asset) tag := first.Header().Get("ETag") if tag == "" { t.Errorf("%s: no ETag", asset) continue } r := httptest.NewRequest("GET", asset, nil) r.Header.Set("If-None-Match", tag) w := httptest.NewRecorder() fixtureMux.ServeHTTP(w, r) if w.Code != http.StatusNotModified || w.Body.Len() != 0 { t.Errorf("%s with If-None-Match: status %d, %d bytes (want an empty 304)", asset, w.Code, w.Body.Len()) } // A different asset's tag must not satisfy it. r = httptest.NewRequest("GET", asset, nil) r.Header.Set("If-None-Match", `"0000000000000000"`) w = httptest.NewRecorder() fixtureMux.ServeHTTP(w, r) if w.Code != 200 { t.Errorf("%s with a stale ETag: status %d, want the asset", asset, w.Code) } } } // Method handling: the routes declare GET or POST, so anything else is a // 405 from the mux rather than a handler running on the wrong verb. func TestHTTPMethods(t *testing.T) { buildFixture(t) for _, c := range []struct { method, path string want int }{ // Methods the archive never answers stop at the wrapper. {"PUT", "/api/docs", http.StatusMethodNotAllowed}, {"DELETE", "/doc/prism-slides", http.StatusMethodNotAllowed}, {"OPTIONS", "/", http.StatusMethodNotAllowed}, {"TRACE", "/browse", http.StatusMethodNotAllowed}, // A supported method on the wrong route is a 404: the catch-all // route claims every path, which is what keeps unknown paths on the // archive's own 404 page, and it claims them for POST too. {"POST", "/browse", http.StatusNotFound}, {"GET", "/preferences/save", http.StatusNotFound}, {"GET", "/pin/prism-slides", http.StatusNotFound}, } { r := httptest.NewRequest(c.method, c.path, nil) w := httptest.NewRecorder() fixtureMux.ServeHTTP(w, r) if w.Code != c.want { t.Errorf("%s %s: status %d, want %d", c.method, c.path, w.Code, c.want) } } } // Odd but legal requests must not break anything: repeated parameters, // unknown parameters, a very long path, an empty query. func TestHTTPOddRequests(t *testing.T) { buildFixture(t) for _, c := range []struct { path string want int }{ {"/browse?sort=title&sort=date", 200}, {"/browse?unknown=1&another=2", 200}, {"/search?q=a&q=b&do_search=1", 200}, {"/browse/program/" + strings.Repeat("x", 2000), 200}, // an empty page, not a crash {"/doc/" + strings.Repeat("x", 2000), 404}, {"/browse?", 200}, {"/search?do_search=1&fqv=&fqv=&fqv=", 200}, } { if w := get(t, c.path); w.Code != c.want { t.Errorf("%.60s: status %d, want %d", c.path, w.Code, c.want) } } } // With -accel-files the file is nginx's to send: the handler answers only // the files it would have served itself, with the redirect and no body. func TestAccelFiles(t *testing.T) { buildFixture(t) accelPrefix = "/_files/" defer func() { accelPrefix = "" }() w := get(t, "/files/prism-slides/prism-slides.pdf") rel, err := filepath.Rel(dataDir, filepath.Join(corpus().docIndex["prism-slides"].Dir, "prism-slides.pdf")) if err != nil { t.Fatal(err) } if got, want := w.Header().Get("X-Accel-Redirect"), "/_files/"+filepath.ToSlash(rel); got != want { t.Errorf("X-Accel-Redirect %q, want %q", got, want) } if w.Code != http.StatusOK || w.Body.Len() != 0 { t.Errorf("accel answer: status %d, %d body bytes, want an empty 200", w.Code, w.Body.Len()) } if w.Header().Get("Cache-Control") == "" { t.Error("accel answer carries no Cache-Control for nginx to pass on") } for _, p := range []string{"/files/prism-slides/zz-stray.pdf", "/files/prism-slides/../glossary.json", "/files/no-such-doc/x.pdf"} { w := get(t, p) if w.Header().Get("X-Accel-Redirect") != "" || w.Code == http.StatusOK { t.Errorf("%s: status %d, X-Accel-Redirect %q - a file the server does not serve was handed to nginx", p, w.Code, w.Header().Get("X-Accel-Redirect")) } } } func TestAccelPathEscapes(t *testing.T) { old, oldPrefix := dataDir, accelPrefix dataDir, accelPrefix = "/srv/data", "/_files/" defer func() { dataDir, accelPrefix = old, oldPrefix }() if got, ok := accelPath("/srv/data/FOIA/a doc/50%?.pdf"); !ok || got != "/_files/FOIA/a%20doc/50%25%3F.pdf" { t.Errorf("accelPath = %q, %v", got, ok) } if got, ok := accelPath("/srv/other/x.pdf"); ok { t.Errorf("a file outside the data directory mapped to %q", got) } }