package app
import (
"html/template"
"net/http/httptest"
"skunkyart/static"
"strings"
"sync"
"testing"
"github.com/krazywarez/devianter"
)
var loadTemplatesOnce sync.Once
// loadTemplates makes static.Templates usable from a test. The non-embed build
// reads the repository's static/ directory; the embed build already has it.
func loadTemplates() {
loadTemplatesOnce.Do(func() {
static.StaticPath = "../static"
static.CopyTemplatesToMemory()
LoadLanguages()
ParseTemplates()
})
}
// markup is the payload every escaping test injects. It closes an attribute,
// closes a tag and opens a new element, which is what an injection needs to do.
const markup = `">x`
// TestEveryPageTemplateRenders pins down that the switch to html/template
// parses and executes every page: html/template rejects some constructs
// text/template accepts, and a failure here would be a 500 on every request.
func TestEveryPageTemplateRenders(t *testing.T) {
loadTemplates()
for _, page := range []string{"about.htm", "daily.htm", "deviantion.htm", "gruser.htm", "search.htm"} {
rec := httptest.NewRecorder()
s := skunkyart{Writer: rec, Host: "http://localhost", BasePath: "/"}
s.ExecuteTemplate(page, "html", &s)
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "