package web
import (
"net/http/httptest"
"strings"
"testing"
)
func get(t *testing.T, path string) (int, string) {
t.Helper()
rec := httptest.NewRecorder()
Handler(testRegistry(t), nil, nil).ServeHTTP(rec, httptest.NewRequest("GET", path, nil))
return rec.Code, rec.Body.String()
}
// A page reached because a submit did NOT land says so, and loads the
// script that acts on it. draft.js restores a draft only after seeing
// this hook — the client cannot tell "came back after a bounce" from
// "came back after a successful post to the same page", and guessing
// the second as the first puts a posted comment back in the box.
func TestAFailurePageMarksTheDraftKept(t *testing.T) {
code, body := get(t, "/r/test/no-such-page-at-all")
if code < 400 {
t.Fatalf("fixture: status %d, want a failure", code)
}
if !strings.Contains(body, "data-draft-keep") {
t.Fatal("FAIL: a failure page does not mark the draft kept")
}
if !strings.Contains(body, `src="`+draftURL+`"`) {
t.Fatal("FAIL: a failure page does not load draft.js, so nothing reads the mark")
}
t.Logf("ok: status %d carries data-draft-keep and loads draft.js", code)
}
// A page that is neither a composer nor a failure loads neither. The
// home page's two-script rule is the principle this respects: a script
// is on a page only where it has work to do.
func TestAnOrdinaryPageCarriesNoDraftMachinery(t *testing.T) {
_, body := get(t, "/")
if strings.Contains(body, "data-draft-keep") || strings.Contains(body, draftURL) {
t.Fatal("FAIL: the home page carries draft machinery it has no use for")
}
t.Log("ok: the home page has neither the hook nor the script")
}