package main import ( "net/http" "net/http/httptest" "net/url" "strings" "testing" ) func reqWithCookies(pairs ...string) *http.Request { r := httptest.NewRequest("GET", "/", nil) for i := 0; i+1 < len(pairs); i += 2 { r.AddCookie(&http.Cookie{Name: pairs[i], Value: pairs[i+1]}) } return r } // Defaults apply when a cookie is absent, and a value the preference does // not accept is a value it does not take. func TestReadPrefsValues(t *testing.T) { p := readPrefs(reqWithCookies()) if p.DocView != "images" || p.Theme != "auto" || p.PerPage != 20 || p.InfoSidebar { t.Errorf("defaults wrong: %+v", p) } if !p.ShowThumbs || !p.ShowClassification || p.TrackRecent { t.Errorf("boolean defaults wrong: %+v", p) } p = readPrefs(reqWithCookies( "pref_doc_view", "split", "pref_theme", "dark", "pref_per_page", "all", "pref_info_position", "beside", "pref_hide_thumbs", "1", "pref_track_recent", "1", )) if p.DocView != "split" || p.Theme != "dark" || p.PerPage != 0 || !p.InfoSidebar { t.Errorf("cookie values not applied: %+v", p) } if p.ShowThumbs || !p.TrackRecent { t.Errorf("boolean cookies not applied: %+v", p) } if p.ThemeAttr() != "dark" { t.Errorf("ThemeAttr = %q", p.ThemeAttr()) } // Junk in the jar: every value pref falls back to its default. p = readPrefs(reqWithCookies( "pref_doc_view", "sideways", "pref_theme", "neon", "pref_per_page", "7", "pref_info_position", "under", )) if p.DocView != "images" || p.Theme != "auto" || p.PerPage != 20 || p.InfoSidebar { t.Errorf("junk cookies not rejected: %+v", p) } if p.ThemeAttr() != "" { t.Errorf("auto must render no data-theme, got %q", p.ThemeAttr()) } } // Saving writes the cookies a non-default choice needs and clears the rest, // so a default-settings visitor stays cookieless (and their pages stay // publicly cacheable). func TestPreferencesSaveRoundTrip(t *testing.T) { form := url.Values{ "doc_view": {"text"}, "theme": {"light"}, "per_page": {"100"}, "info_position": {"beside"}, "hide_thumbs": {"1"}, } r := httptest.NewRequest("POST", "/preferences/save", strings.NewReader(form.Encode())) r.Header.Set("Content-Type", "application/x-www-form-urlencoded") r.Header.Set("Sec-Fetch-Site", "same-origin") w := httptest.NewRecorder() handlePreferencesSave(w, r) set := map[string]string{} for _, c := range w.Result().Cookies() { set[c.Name] = c.Value if c.MaxAge < 0 { set[c.Name] = "" // deleted } } for name, want := range map[string]string{ "pref_doc_view": "text", "pref_theme": "light", "pref_per_page": "100", "pref_info_position": "beside", "pref_hide_thumbs": "1", "pref_track_recent": "", // unchecked: deleted } { if got, ok := set[name]; !ok || got != want { t.Errorf("%s = %q (present %v), want %q", name, got, ok, want) } } // Defaults delete rather than store: nothing in the jar means nothing // to vary the cache on. form = url.Values{"doc_view": {"images"}, "theme": {"auto"}, "per_page": {"20"}, "info_position": {"above"}} r = httptest.NewRequest("POST", "/preferences/save", strings.NewReader(form.Encode())) r.Header.Set("Content-Type", "application/x-www-form-urlencoded") r.Header.Set("Sec-Fetch-Site", "same-origin") w = httptest.NewRecorder() handlePreferencesSave(w, r) for _, c := range w.Result().Cookies() { if c.MaxAge >= 0 { t.Errorf("default settings wrote cookie %s=%q", c.Name, c.Value) } } } // "All" per page is every card up to maxCardsPerPage: a list longer than // that is paged, however large the reader's page size. func TestCardsPerPage(t *testing.T) { for perPage, want := range map[int]int{0: maxCardsPerPage, 20: 20, 100: 100, maxCardsPerPage + 1: maxCardsPerPage} { if got := (Prefs{PerPage: perPage}).cardsPerPage(); got != want { t.Errorf("PerPage %d: %d cards a page, want %d", perPage, got, want) } } }