122 lines
3.8 KiB
Go
122 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestAPIPageSize(t *testing.T) {
|
|
for raw, want := range map[string]int{
|
|
"": apiPerPageDefault,
|
|
"25": 25,
|
|
"500": 500,
|
|
"1000": apiMaxPerPage,
|
|
"99999999": apiMaxPerPage,
|
|
"0": apiPerPageDefault, // not a page size; fall back
|
|
"-5": apiPerPageDefault,
|
|
"lots": apiPerPageDefault,
|
|
"all": 0,
|
|
} {
|
|
v := url.Values{}
|
|
if raw != "" {
|
|
v.Set("per_page", raw)
|
|
}
|
|
if got := apiPageSize(v); got != want {
|
|
t.Errorf("apiPageSize(per_page=%q) = %d, want %d", raw, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAPIRespPerPage(t *testing.T) {
|
|
for _, c := range []struct{ perPage, total, want int }{
|
|
{50, 3106, 50},
|
|
{0, 3106, 3106},
|
|
{0, 0, 1},
|
|
} {
|
|
if got := apiRespPerPage(c.perPage, c.total); got != c.want {
|
|
t.Errorf("apiRespPerPage(%d, %d) = %d, want %d", c.perPage, c.total, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAPIVersionsShape(t *testing.T) {
|
|
root := t.TempDir()
|
|
writeTestFile(t, filepath.Join(root, "doc-a", "metadata.json"), `{
|
|
"title": "A", "type": "REPORTED",
|
|
"publication": [{"publisher": "Le Monde", "version": "entier"}],
|
|
"versions": {"entier": {"changed_pages": [1]}}
|
|
}`)
|
|
writeTestFile(t, filepath.Join(root, "doc-a", "a.pdf"), "%PDF-1.4")
|
|
writeTestFile(t, filepath.Join(root, "doc-a", "versions", "entier", "entier.pdf"), "%PDF-1.4")
|
|
withCorpus(t, root)
|
|
|
|
type versionsObj struct {
|
|
Versions map[string]VersionInfo `json:"versions"`
|
|
}
|
|
var detail struct {
|
|
versionsObj
|
|
VersionCopies []apiVersion `json:"version_copies"`
|
|
}
|
|
decode := func(path string, v interface{}) {
|
|
t.Helper()
|
|
w := get(t, path)
|
|
if w.Code != 200 {
|
|
t.Fatalf("%s: %d", path, w.Code)
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), v); err != nil {
|
|
t.Fatalf("%s: %v", path, err)
|
|
}
|
|
}
|
|
decode("/api/doc/doc-a", &detail)
|
|
if len(detail.Versions["entier"].ChangedPages) != 1 {
|
|
t.Errorf("/api/doc versions = %+v, want the metadata object", detail.Versions)
|
|
}
|
|
if len(detail.VersionCopies) != 1 || detail.VersionCopies[0].Slug != "entier" {
|
|
t.Errorf("/api/doc version_copies = %+v, want the entier copy", detail.VersionCopies)
|
|
}
|
|
var list struct{ Docs []versionsObj }
|
|
decode("/api/docs", &list)
|
|
var search struct{ Results []versionsObj }
|
|
decode("/api/search?q=A", &search)
|
|
for name, docs := range map[string][]versionsObj{"/api/docs": list.Docs, "/api/search": search.Results} {
|
|
if len(docs) != 1 || len(docs[0].Versions["entier"].ChangedPages) != 1 {
|
|
t.Errorf("%s versions = %+v, want the metadata object", name, docs)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAPIFacetsRevalidates(t *testing.T) {
|
|
w := get(t, "/api/facets")
|
|
tag := w.Header().Get("ETag")
|
|
if w.Code != 200 || tag == "" || w.Header().Get("Access-Control-Allow-Origin") != "*" {
|
|
t.Fatalf("/api/facets: %d, ETag %q, CORS %q", w.Code, tag, w.Header().Get("Access-Control-Allow-Origin"))
|
|
}
|
|
mux, _ := buildFixture(t)
|
|
r := httptest.NewRequest("GET", "/api/facets", nil)
|
|
r.Header.Set("If-None-Match", tag)
|
|
if w := record(mux, r); w.Code != http.StatusNotModified || w.Body.Len() != 0 {
|
|
t.Errorf("revalidation: %d with %d bytes, want 304 and none", w.Code, w.Body.Len())
|
|
}
|
|
}
|
|
|
|
// /api/docs takes coll= as /api/search and the pages do.
|
|
func TestAPIDocsCollections(t *testing.T) {
|
|
total := func(query string) int {
|
|
var body struct{ Total int }
|
|
if err := json.Unmarshal(get(t, "/api/docs?"+query).Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("%s: %v", query, err)
|
|
}
|
|
return body.Total
|
|
}
|
|
all, news, foia := total(""), total("coll=news"), total("coll=foia")
|
|
if news == 0 || foia == 0 || news+foia >= all {
|
|
t.Fatalf("fixture totals: %d documents, %d news, %d foia", all, news, foia)
|
|
}
|
|
if got := total("coll=news&coll=foia"); got != news+foia {
|
|
t.Errorf("coll=news&coll=foia lists %d, want %d", got, news+foia)
|
|
}
|
|
}
|