1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
package render
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestRenderPartial(t *testing.T) {
render := New(Options{
Directory: "testdata/partials",
Layout: "layout",
})
var renErr error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
renErr = render.HTML(w, http.StatusOK, "content", "gophers")
})
res := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
if err != nil {
t.Fatalf("couldn't create a request. err = %s", err)
}
h.ServeHTTP(res, req)
expectNil(t, renErr)
expect(t, res.Body.String(), "before gophers\n<h1>during</h1>\nafter gophers\n")
}
func TestRenderPartialRequirePartialsOff(t *testing.T) {
render := New(Options{
Directory: "testdata/partials",
Layout: "layout",
RequirePartials: false,
})
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = render.HTML(w, http.StatusOK, "content-partial", "gophers")
})
res := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
if err != nil {
t.Fatalf("couldn't create a request. err = %s", err)
}
h.ServeHTTP(res, req)
expect(t, res.Body.String(), "\n<h1>during</h1>\nafter gophers\n")
}
func TestRenderPartialRequirePartialsOn(t *testing.T) {
render := New(Options{
Directory: "testdata/partials",
Layout: "layout",
RequirePartials: true,
})
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = render.HTML(w, http.StatusOK, "content-partial", "gophers")
})
res := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, "GET", "/foo", nil)
if err != nil {
t.Fatalf("couldn't create a request. err = %s", err)
}
h.ServeHTTP(res, req)
expect(t, res.Body.String(), "template: layout:1:3: executing \"layout\" at <partial \"before\">: error calling partial: html/template: \"before-content-partial\" is undefined\n")
}
|